Since becoming an analyst and learning SQL, I have formatted my SQL queries in every possible way. After meeting the folks at dbt Labs (nee Fishtown) four years ago, I was inspired by their SQL style guide; after spending more time writing python and using black, I desperately wanted an auto formatter like black for dbt SQL.
Over the past few months, I have been hacking away at that tool, and today it works with dbt jinja and creates well-formatted, modern-looking SQL. It’s not a linter (use SQLFluff for that) – it just does one thing and does it well.
You can try it at sqlfmt.com, or check out the repo on Github. Or if you like to live dangerously,
pipx install shandy-sqlfmt
It’s still really early innings. There are more features and integrations I’d like to build (and I’m sure plenty of bugs yet to be found). I would really, really value any contribution from this community, whether that’s feedback, an issue, or a PR. Thanks for checking it out; please let me know what you think!
Installed and started playing around with it over the weekend. Great work.
I love an opinionated formatter: we’ve been using
black for a while in our Python libraries, and it really reduces the cognitive (and conversational) load. So I’m excited for an equivalent for our SQL files. (And I like that it starts from the dbt Labs style guide.)
Is it possible to force the
ands to align here?
from historic_answers ha
subject = 'Math' and learningobjective = "Addition" and dt_utc > date(
) and testschool = false
from historic_answers ha
and subject = 'Math'
and learningobjective = "Addition"
and dt_utc > date('2020-10-01')
and testschool = false
Or does this go against some of the fundamental rules of your formatter?
I agree your solution is better. It’s been on my mind to improve the formatting of chained boolean operators with parentheses for some time. Can you open an issue for this?
Good that you mention it, should I open two issues:
- Force separate
where clauses on single lines (even if they don’t exceed max width)
- Improve formatting of chained boolean operators with parentheses
Or do you see these two issues as one?
P.S. it would be awesome if there was a way to change the line length in the interactive web version I use it a lot when showcasing it to people but the default line length is a bit short for most people
Sorry, my notifications for discourse started going to Spam! #2 is what I had in mind, it basically implies #1… the real issue today is the interaction between the parentheses-driven line splitting and merging and the boolean operator-driven line splitting and merging.
Re: line-length online, sure thing! I’ll create an issue for that one on the sqlfmt.com repo
Just wanted to say thanks for making this! I’m using it on on a project at work and so far it’s been great!
@janmeppe Line length config is now available on sqlfmt.com: click “Configure Formatting” next to the blue button for the available options.