-
Notifications
You must be signed in to change notification settings - Fork 412
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
User-defined functions and table-valued functions have an extra space #444
Comments
Yes, this is a known issue. We're planning to address this, but it's not a simple fix to make. Hard to say how long it will take. The formatter relies on various heuristics to do it's work. We're slowly evolving towards properly parsing the SQL language, but we're still far from that. |
Thanks for the quick reply. I'm sure this is a tricky problem and you likely don't want to ship like 58MB of grammars (and if so, allow tree shaking and only importing the grammar I need) https://www.npmjs.com/package/antlr4ts-sql For my use case I wonder if I could interface to the SQLite C API to tokenize the SQL, but it looks there is no high level API available. |
Hey, Maybe any update on this one behavior? Or maybe can you suggest any workarounds? |
For SQLite I can suggest a new SQL formatting library that I've been developing: prettier-plugin-sql-cst |
Edit: The problem I described below is resolved in Yeah this is also happening with PostgreSQL, the Input: SELECT
users.id,
users.username
FROM
users
INNER JOIN sessions ON (
sessions.token = 'abc123'
AND users.id = sessions.user_id
AND sessions.expiry_timestamp > now()
) Expected Output (no change): SELECT
users.id,
users.username
FROM
users
INNER JOIN sessions ON (
sessions.token = 'abc123'
AND users.id = sessions.user_id
AND sessions.expiry_timestamp > now()
) Actual Output (extra space between SELECT
users.id,
users.username
FROM
users
INNER JOIN sessions ON (
sessions.token = 'abc123'
AND users.id = sessions.user_id
AND sessions.expiry_timestamp > now ()
) |
@karlhorky did you set
|
Encountering the same issue, can we consider exhaustively listing scenarios where spaces are needed before parentheses for handling? In all other cases, remove spaces in front of parentheses.
|
Also seeing the same issue with postgresql: Inputcreate or replace function find_array_element(el anyelement, arr anyarray) returns integer as $$ declare i int;
begin
for i in 1..array_upper( arr, 1 ) loop
if( el = arr[i] ) then
return i;
end if;
end loop;
return 0;
end;
$$ language plpgsql; outputcreate
or replace function find_array_element (el anyelement, arr anyarray) returns integer as $$
declare
i int;
begin
for i in 1..array_upper( arr, 1 ) loop
if( el = arr[i] ) then
return i;
end if;
end loop;
return 0;
end;
$$ language plpgsql; expectedcreate
or replace function find_array_element(el anyelement, arr anyarray) returns integer as $$
declare
i int;
begin
for i in 1..array_upper( arr, 1 ) loop
if( el = arr[i] ) then
return i;
end if;
end loop;
return 0;
end;
$$ language plpgsql; prettier config: {
"trailingComma": "es5",
"proseWrap": "always",
"plugins": [
"prettier-plugin-sql"
],
"language": "postgresql",
"paramTypes": "{'named':[':']}"
} |
It appears there is an internal list of known SQLite functions and virtual tables used to format. I rely heavily on user-defined functions and table-valued functions. These custom functions have an extra space before the opening parenthesis.
Input data
Expected Output
Actual Output
Usage
=> https://sql-formatter-org.github.io/sql-formatter/ with SQLite language
I don't think any of the other formatting options matter in this case. The formatter should not need to keep a list of SQLite functions/tables but infer the formatting from the token (I have no idea how this library is actually implemented).
The text was updated successfully, but these errors were encountered: