sqlfmt icon indicating copy to clipboard operation
sqlfmt copied to clipboard

Column formatting breaks with unary operators

Open albertsgrc opened this issue 1 year ago • 3 comments

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/

albertsgrc avatar Jan 22 '24 14:01 albertsgrc

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

tconbeer avatar Jan 22 '24 16:01 tconbeer

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 avatar Jan 22 '24 22:01 tconbeer

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)

tconbeer avatar Jul 26 '24 19:07 tconbeer