tconbeer / sqlfmt

sqlfmt formats your dbt SQL files so you don't have to
https://sqlfmt.com
Apache License 2.0
373 stars 15 forks source link

Column formatting breaks with unary operators #550

Open albertsgrc opened 6 months ago

albertsgrc commented 6 months ago

Describe the bug When unary operators are used in column definition X, X is formatted to be in the same line as the previous column definition.

A space is also added in between the unary operator and the operand (not sure if this is expected).

To Reproduce

select
    amount_in_cents / 100.0 as amount, 
    -amount as charged_amount,
    resulting_balance_in_cents / 100.0 as resulting_balance
from mytable

Expected behavior I expect the code above to remain unchanged after formatting with a maximum line length of 88

Actual behavior This is sqlfmt's output:

select
    amount_in_cents / 100.0 as amount, - amount as charged_amount,
    resulting_balance_in_cents / 100.0 as resulting_balance
from mytable

Additional context This is reproducible from https://sqlfmt.com/

tconbeer commented 6 months ago

Thanks for the report -- this one should be an easy fix.

tconbeer commented 6 months ago

this one should be an easy fix

Famous last words. This requires changes in quite a few places. Right now we support unaries in front of numbers by lexing the operator as a part of the number, but that approach won't work generally, since we have to support any expression after the operator. So this will require a new token type and maybe a new lexing ruleset, and then a close look at the line merging logic.

tconbeer commented 3 days ago

This was partially fixed in 0.22.0, released yesterday. The current output from the example above is now:

select
    amount_in_cents / 100.0 as amount,
    - amount as charged_amount,
    resulting_balance_in_cents / 100.0 as resulting_balance
from mytable

(so there is still a space between - and amount, but it is no longer merged onto the previous line)