Introducing sqlfmt, an auto-formatter for dbt SQL

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!

8 Likes

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.)

1 Like

Thanks for contributing!

Hi,

Is it possible to force the ands to align here?

Autoformatted:

select *
from historic_answers ha
where
    subject = 'Math' and learningobjective = "Addition" and dt_utc > date(
        '2020-10-01'
    ) and testschool = false

Desired:

select *
from historic_answers ha
where 1=1
    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?

1 Like

Good that you mention it, should I open two issues:

  1. Force separate where clauses on single lines (even if they don’t exceed max width)
  2. 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 :slight_smile: I use it a lot when showcasing it to people but the default line length is a bit short for most people