Live data from Hacker News

Sqlfluff the SQL Linter for Humans

sqlfluff.com

51–60 of 95 posts

Re: Sqlfluff the SQL Linter for Humans

#51

Earlier quoted context omitted.

I hate you and and the awful layout you have just introduced me to... Also, that is how I will be formatting all my SQL from now on.

alt styling SELECT a, b, COUNT(*) as count FROM table WHERE cond1 AND cond2 GROUP BY a, b ORDER BY count DESC

Capitalized keywords and section declarations on their own lines helps with quick comprehension for me at least

Re: Sqlfluff the SQL Linter for Humans

#52

I wanted to use this linter, but they don't support many things that are used in real production code. By example, a simple postgres function like this one can't be parsed: CREATE OR REPLACE FUNCTION public.setof_test() RETURNS SETOF text LANGUAGE sql STABLE STRICT AS $function$ select unnest(array['hi', 'test']) $function$ ;

I really personally dislike how you fail to shout the keywords in the above function "that actually matter" - all the set dressing and obvious stuff is capitalized - but then the meat of the function is all in lowercase hiding in the middle.

Re: Sqlfluff the SQL Linter for Humans

#53
post #4

Earlier quoted context omitted.

I guess it depends on what you want out of a linter e.g. if you want it to check just syntax or semantics as well. Is “= NULL” syntactically correct?

The compiler checks syntax - if that’s all a linter did it wouldn’t be very useful.

`= NULL` is perfectly legal syntax - but it isn't doing what you think it is.

Re: Sqlfluff the SQL Linter for Humans

#54
post #37

Earlier quoted context omitted.

The reason that those people care about formatting is that it matters when the code is used to communicate with other people, not just with machines. Having a consistent style within the team, means the team can communicate more easily - even if the net effect for an individual working on a codebase alone is minimal.

Yes but realistically, save for the code that someone who's really inexperienced or sloppy would write, fixating over whether someone is writing select a, b, c from foo where bar = 'quux' rather than SELECT a, b, c FROM foo WHERE bar = 'quux' is petty and immaterial to what the code communicates. Worse if it's about details within the roughly the same style. Edit: but if there's a code formatter that can be seamlessl…

Code isn't meant to be read by computers - it's meant to be read by humans (including yourself in three years when you've forgotten everything). Code sugaring and style adherence are vital portions of retaining a maintainable and flexible codebase.

Re: Sqlfluff the SQL Linter for Humans

#55

Earlier quoted context omitted.

I hate you and and the awful layout you have just introduced me to... Also, that is how I will be formatting all my SQL from now on.

alt styling SELECT a, b, COUNT(*) as count FROM table WHERE cond1 AND cond2 GROUP BY a, b ORDER BY count DESC

I've seen this one in the wild and have objections to it.

  - why capitalize KEYWORDS. We're no longer in 1970s. We use colors
  - why waste so much space. Why not just put table on the same line with from. With joins this just bloats up
  - for complicated conditions they have to be put on their own lines. Do I give each AND a separate line and waste ever more space?

Re: Sqlfluff the SQL Linter for Humans

#56

I wanted to use this linter, but they don't support many things that are used in real production code. By example, a simple postgres function like this one can't be parsed: CREATE OR REPLACE FUNCTION public.setof_test() RETURNS SETOF text LANGUAGE sql STABLE STRICT AS $function$ select unnest(array['hi', 'test']) $function$ ;

Looks like we didn't support SETOF.

Added this here: https://github.com/sqlfluff/sqlfluff/pull/1522

Demonstrating how easy it is to add this sort of thing to the project thanks to how the code is structured! :-)

Re: Sqlfluff the SQL Linter for Humans

#57
post #53

Earlier quoted context omitted.

The compiler checks syntax - if that’s all a linter did it wouldn’t be very useful.

`= NULL` is perfectly legal syntax - but it isn't doing what you think it is.

That's what parent is saying. He is contrasting a compiler (which this isn't) to a linter (which this arguably also isn't).

Re: Sqlfluff the SQL Linter for Humans

#58

Earlier quoted context omitted.

I've seen it a lot in Data Science/Analytics environments, as it allows for faster query iterations than trailing commas.

Yes you can find sources advocating for leading commas and it's commonly used. I have "Google BigQuery: The Definitive Guide" on my desktop, it was written by senior Google employees involved in the steering and use of the product inside, for what it's worth, and it advocates for leading comma and SHOUTING THE KEYWORDS. I personally disobey both recommendations (I mean, the book is worth it for other reasons) but bot…

jetbrain IDEs have it configured by default for most situations, so its generally just an auto-format away if you're actually writing sql and not embedding sql queries in some other code.

Re: Sqlfluff the SQL Linter for Humans

#59
post #16

I wondered how much good you could do with an SQL linter that didn't have any access to the underlying schema, and the answer seems to be "mostly whitespace and capitalization issues".

I think a not-so-small-part of the success of JS/CSS linting was that large companies like AirBnB, GitHub, and Google made their rules public and helped with sane defaults. Almost every project I came across used one of these "defaults" and added custom rules as needed.

Sqlfluff seems to ship with 48 "built-in" rules and opinions on spacing and capitalization. If it catches on, I think the larger community will mostly benefit after other companies "doing work" with it release their own collection of rules.

Re: Sqlfluff the SQL Linter for Humans

#60
post #22
post #11

There surely is a need for a sql linter, but if it wants to be strict about syntax style, it should better familiarize itself with not-noob styles first. Eg. some people like to align the clauses vertically and use a leading, not trailing, comma. select a , b , count(*) as count from table where cond1 and cond2 group by a, b order by count desc

Yes! I write SQL this way. I grant the comma thing is more a matter of taste, but aligning clauses vertically and maximizing rivers is super useful to me and it forever mystifies me that it isn't standard. This is an extreme example, but I like to keep join clauses vertically aligned as well: select a , b , count(*) as count from table join table2 on table.key1 = table2.key1 and table.key2 = table2.key2 and DATEADD('…

This looks nearly the same, but it's much easier to write and generates less spurious diffs:

    select a
        , b
        , count(*) as count
    from table
    join table2
        on table.key1 = table2.key1
        and table.key2 = table2.key2
        and DATEADD('day, 1, table.key3) = table2.key3
    where cond1
        and cond2
    group by a, b
    order by count desc
The largest difference is that the equal operators aren't aligned anymore, but on my opinion, aligning them is really not worth the cost.
Post reply on HN