-
-
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
Improve formatting for chained boolean operators with complex expressions #189
Comments
How would you sketch a solution for this bug? I've been browsing through the code a bit, getting set up with a dev environment, running all the tests, etc. I'm diving into the code/data now but I'm having a tough time seeing where I would have to change what to make this fix. This bug is the one big thing that is stopping our team from using sqlfmt everywhere so I'm eager to get this fixed :) |
That's great to hear! I'm not quite sure what a solution will look like just yet. The changes will be in I think
Finally, right now, Sorry I can't be more helpful here. |
Thinking about this a little more. If we change your query to: select *
from historic_orders h
where 1 = 1
and order_type = 'clothing'
and category = "pants"
and is_test is false
and dt_utc > date('2020-10-01') -- moved this to the end sqlfmt does what you want. The easiest/least destructive way to change this behavior is by changing select
*
from
historic_orders h
where
1
= 1
and order_type
= 'clothing'
and category
= "pants"
and dt_utc
> date(
'2019-10-01'
)
and is_test
is false We get the "bad" formatting because we merge the lines up to and including |
@Rainymood did you want to try to contribute to this? Otherwise I might be able to pick it up next week |
No no go ahead! Would love to see this fixed :) Thanks for fixing the other issue so fast btw! Awesome! |
@Rainymood This is merged to main now, but 0.10.0 may not be released for a while yet. If you want, you can install this patch with: python -m pip install "shandy-sqlfmt[jinjafmt] @ git+https://github.com/tconbeer/sqlfmt.git@6efbf29510fc93b3d0d1bef0d8d6b2a310b9690a" If you get that working, I'd love your feedback on the new style! |
Yes! It works perfectly for me. I'm wondering though: is there a toggle to turn this behavior on/off or is this behavior now enabled by default? For the queries that I tested so far it looks really promising but I'll try to find more edge cases :) P.S. Would it be possible to change the examples in the original post to this? (I'll delete this message after you edited the OP). I tried to message you privately on twitter but that seems disabled. Let me know! To Reproduce select *
from historic_orders h
where
1 = 1
and order_type = 'clothing'
and category = "pants"
and dt_utc > date('2019-10-01') Expected behavior select *
from historic_orders h
where
1 = 1
and order_type = 'clothing'
and category = "pants"
and dt_utc > date('2019-10-01') Actual behavior select *
from historic_orders h
where
1 = 1 and order_type = 'clothing' and category = "pants" and dt_utc > date(
'2020-10-01'
) |
This is the default and only style from now on, although I opened a couple of issues for small tweaks I'd like to make to build on this style and improve it. Code above updated. |
There's also another reference in the comments around halfway this thread :). The changes seem great already, this change will be huge in support of my team adopting this :). |
Got it this time! |
Describe the bug
Reported by @Rainymood on discourse.
We're not very smart about building a hierarchy of chained boolean expressions; mixing simple expressions with complex expressions can produce unexpected and hard-to-read formatting.
To Reproduce
Expected behavior
Actual behavior
Additional context
What is the output of
sqlfmt --version
?0.8.0
The text was updated successfully, but these errors were encountered: