-
-
Notifications
You must be signed in to change notification settings - Fork 18
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
Postgres bitwise xor (#
) and JSON path (#>
, #>>
, and #-
) operator formatting
#461
Comments
ugh, yeah, I didn't know that symbol had meaning in postgres. We parse it as a comment for MySql compatibility. I think we'll have to create a Postgres dialect that doesn't include that character in the comment rule (and includes it as an operator). That is a bit of a pain in the ass... |
Bitwise XOR isn't exactly a common one at least! Doesn't seem worth adding dialect differentiation just for this, which is also why I made a point to document those workarounds. (I'm a bit sad Postgres itself doesn't seem to have a built-in function version of this. It'd be a lot less opaque than an operator to those reading the code.) |
#
) and JSON path (#>
and #>>
) operator formatting
It was just brought to my attention that there are two other PG operators that start with In all of these cases, select
--fmt: off
foo #>> bar,
--fmt: on
baz But this does NOT: select
foo #>> bar, -- fmt: off
baz as it becomes: select
foo # >> bar, -- fmt: off
baz |
#
) and JSON path (#>
and #>>
) operator formatting#
) and JSON path (#>
, #>>
, and #-
) operator formatting
If it helps other people looking at this issue, I was able to workaround |
I can add here that the select *,
array[
( my_key || '_' || (blob #>> '{key,value}')
)
] as new_blob
from my_table adding e.g. the formatting rules to this doesn't work as follows: select *,
array[
-- fmt: off
( my_key || '_' || (blob #>> '{key,value}')
-- fmt: on
)
] as new_blob
from my_table this still results in:
only if you add the format comment on this: select *,
array[
( my_key || '_' || (
-- fmt: off
blob #>> '{key,value}'
-- fmt: on
)
)
] as new_blob
from my_table it works but that is just bizare. So, there is no plan to support this yet? |
The standalone XOR is harder, but we could fix the JSON operators with a tweak to the lexer easily enough. |
Describe the bug
The Postgres bitwise xor operator
#
appears to get recognized as a comment, rather than as an infix operator. This can produce some very strange results.To Reproduce
Simplified input:
Expected behavior
I expect this to be treated like any other infix operator:
Actual behavior
In this example, indentation gets pretty mangled for the rest of the file. It treats the closing paren as commented-out and so we get hanging indentation.
Workarounds
In some cases this can be worked around by pre-formatting things just right. For example, this works fine as an input (although, note the awkward double space:
#
).If things still won't format properly, we can create a function as a workaround.
Additional context
sqlfmt, version 0.19.2
and reproducible on https://sqlfmt.com/ in current state.The text was updated successfully, but these errors were encountered: