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.)
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
I guess it is expected? If you look at rule “Rule_ST06” in SQLfluff, that is exactly what is happening with sqlfmt as well… I assume those rules are also used by sqlfmt? Or how does sqlfmt use SQLfluff?
sqlfmt and sqlfluff are two completely different projects. This post is about sqlfmt.com, which is the pypi package shandy-sqlfmt. Unfortunately the PyPI package called sqlfmt is a separate, abandoned project that uses sqlfluff under the hood. Is it possible you installed sqlfmt instead of shandy-sqlfmt?