10 ms·
Sqlfluff the SQL Linter for Humans
- Aeolun 5y agoI’ve been using this for a while and I’ve much enjoyed both the autoformatting as well as the codebase (when submitting PRs).
- ErunamoJAZZ 5y agoI 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$ ;
- munro 5y agoThe code looks really great [1], very declarative, looks like it should be pretty easy to add better PostgreSQL support. I also wonder if how easy it would be to plug it into VS Code, and build in an auto formatter (♥ω♥ ). At a glance it looks like it's parsing AST, which is really hard to find for SQL, so auto formatter could be possible to build from this. I notice SQLite could use some improving too, seems like a great project to coalesce around. [1] https://github.com/sqlfluff/sqlfluff/blob/main/src/sqlfluff/dialects/dialect_postgres.py https://github.com/sqlfluff/sqlfluff/blob/main/src/sqlfluff/...
- amshank 5y agoAuto-formatter is already built in for some simple violations: https://docs.sqlfluff.com/en/stable/cli.html#sqlfluff-fix https://docs.sqlfluff.com/en/stable/cli.html#sqlfluff-fix
- tunetheweb 5y agoThere already is a VS Code extension for this: https://marketplace.visualstudio.com/items?itemName=dorzey.vscode-sqlfluff https://marketplace.visualstudio.com/items?itemName=dorzey.v... https://github.com/sqlfluff/vscode-sqlfluff https://github.com/sqlfluff/vscode-sqlfluff
- munk-a 5y agoI 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.
- tunetheweb 5y agoLooks like we didn't support SETOF. Added this here: https://github.com/sqlfluff/sqlfluff/pull/1522 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! :-)
- ThePadawan 5y agoThis is a linter which complains about spaces and newlines, but lets "WHERE col = NULL" pass (which can never be meaningful) just fine? Obviously this depends on the rules configuration, but just judging that first smell test, it seems very ill thought out.
- justusw 5y agoI 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?
- ThePadawan 5y agoA linter is supposed to tell you "you are probably making a mistake." "= NULL" is as syntactically correct in SQL as "if (x = null)" in C. If it wasn't, code highlighting or compiling would break, both of which are much much more obvious.
- deleted 5y ago[deleted]
- danachow 5y agoThe compiler checks syntax - if that’s all a linter did it wouldn’t be very useful.
- munk-a 5y ago`= NULL` is perfectly legal syntax - but it isn't doing what you think it is.
- ThePadawan 5y agoThat's what parent is saying. He is contrasting a compiler (which this isn't) to a linter (which this arguably also isn't).
- 5y ago
- KronisLV 5y agoThis is a nice idea, but my problem with most of these tools is that they work with pure SQL/JS/whatever files, which isn't always the case. For example, if you use something like myBatis, you'll probably build the SQL dynamically, which could be stored in a mapper XML file: https://mybatis.org/mybatis-3/dynamic-sql.html https://mybatis.org/mybatis-3/dynamic-sql.html Not only that, but certain frameworks have you embedding SQL inside of the main application code, when you're not using a DSL like HQL: https://www.tutorialspoint.com/hibernate/hibernate_query_language.htm https://www.tutorialspoint.com/hibernate/hibernate_query_lan... and https://www.tutorialspoint.com/hibernate/hibernate_native_sql.htm https://www.tutorialspoint.com/hibernate/hibernate_native_sq... Furthermore, there seems to be this odd separation between "regular SQL" and "procedural SQL" in some DBVSes, such as in Oracle - there you might have to use "DECLARE ... BEGIN ... END;" in certain contexts, or maybe "EXECUTE IMMEDIATE". Then again, Oracle isn't among the supported dialects, which is perfectly understandable, because of both how the DBVS is positioned, as well as because of how large an undertaking supporting it would be: https://docs.sqlfluff.com/en/stable/dialects.html https://docs.sqlfluff.com/en/stable/dialects.html In short, the only passable tools that i've found for checking SQL are those that are integrated within the IDE, or using a specialized IDE with any plugins for specific technologies, like JetBrains DataGrip (commercial project): https://www.jetbrains.com/datagrip/ https://www.jetbrains.com/datagrip/ But then you run into the issue of code style preferences: some companies have style guides where you have to use reserved words in uppercase with the dev created content in lowercase, like: SELECT some_column, another_column, yet_another_column AS one_more FROM some_table WHERE some_column > SYSDATE while certain tools (like SQL Developer) are more than happy to use autocomplete which doesn't work well with that: SELECT (start typing in "som") FROM some_table ... Oracle will autocomplete to: SOME_COLUMN And then you run into people's personal preferences, for example some format the columns that they want to select like this: SELECT some_column , another_column , yet_another_column FROM ... which makes sense when you want to edit single lines, but could have been avoided by the SQL standard allowing dangling commas, like some other languages do, for example: SELECT some_column, another_column, yet_another_column, FROM ... but as it currently stands, that's not possible and the other way of formatting code also tends to break linters. And since the actual formatting of SQL doesn't matter as much as it would in Python, we end up with every IDE and other tool having their own preferences. I guess the lesson here is to avoid complexity as much as possible and just settle on whatever works.
- tomhallett 5y agoI am spec’ing a data pipeline right now and the team is so excited to try this out. Here’s a great intro video about the tool and their motivations: https://m.youtube.com/watch?v=veYB9uh0RCM&t=1105s https://m.youtube.com/watch?v=veYB9uh0RCM&t=1105s My goal is to use it with a Rockset database, so I’ll see how far I can get with one of the existing dialects, or how hard it is to extend an existing one to make one for Rockset
- deleted 5y ago[deleted]
- epberry 5y agoFascinating to see the focus on SQL and continuous integration, https://docs.sqlfluff.com/en/stable/inthewild.html#inthewildref https://docs.sqlfluff.com/en/stable/inthewild.html#inthewild.... DBT has really changed things in that regard. @tomhallett's YT video seems to describe using this with a pre-release build of DBT to tightly couple it to sqlfluff.
- snidane 5y agoThere 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
- forgetfulness 5y agoLeading comma is configurable, but I wouldn't know if the linter allows for that layout. Can't say I've seen it before, is it specific to a particular development environment?
- laumars 5y agoIt’s an informal way some people write SQL if they’re developing a script. I’ve done it this way before because it was quicker to edit, and and remove lines. But it’s not a format I’d particularly like to share with others because it looks weird.
- mrbungie 5y agoI've seen it a lot in Data Science/Analytics environments, as it allows for faster query iterations than trailing commas.
- forgetfulness 5y agoYes 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 both those conventions are very commonly used. I meant more the the "center" layout that the commenter above was describing, it'd seem quite laborious to maintain by hand in most environments, if not using a particular one that supports it out of the box.
- jdub 5y agoI've been using sqlfluff with dbt for a while now, and not only has it been a delight to use, the maintainers are incredibly responsive.
- mixmastamyk 5y agoYeah, we can see that in this post today. I saw two issues implemented and fixed while reading it.
- tptacek 5y agoI 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".
- laumars 5y agoMakes you wonder if this tool would be better a code formatter rather than linter?
- forgetfulness 5y agoIt seems to have a formatter option in the `fix` argument you can give it. The authors should probably be selling it on that point since it seems to be its strength, it doesn't have many rules along the lines of the static analysis that people look for in linters. Some people (tech leads, CTOs) care a lot about formatting but I wouldn't make teammates go through a linter for that, it's a rather surface concern.
- amshank 5y agoThe 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.
- forgetfulness 5y agoYes 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 seamlessly added to the development environment, it makes sense to use it. It's just that if there's no tool support, such things are of little contribution.
- nycjay 5y agoSorry - didn't get a chance to go through the documentation much yet... A problem we are looking to catch early in the development process is for things we don't want folks to do, even if they are technically correct. For example, dropping a column or altering the datatype/nullability in such a way that the table goes into reorg pending. I know we can write our own regex to look for that specific syntax, but I've not yet found a tool with those kinds of rules either build out already, or easy to add/maintain.
- amshank 5y agoIt's pretty new functionality in sqlfluff, but it now supports user defined plugins for org-specific rules if you want to forbid something more obscure. Documentation is sketchy, but you can see the proof of concept here: https://github.com/sqlfluff/sqlfluff/tree/main/plugins/sqlfluff-plugin-example https://github.com/sqlfluff/sqlfluff/tree/main/plugins/sqlfl...
- mixmastamyk 5y agoNice, need to forbid things like grants and setting owners that are being handled by a centralized routine.
- nycjay 5y agoThanks, I'll take a closer look there...
- sails 5y agoI think this is primarily aimed at analytics and data modelling SQL, the type of SQL in dbt [], though I'd be happily corrected by the team. As far as I understand, this has been quite a long time coming, and is far from trivial due to the diversity of SQL dialect. My use case would be to have this attached to a dbt project that ensures the dbt SQL files all confirm to a passable degree of consistency when working across a team. Especially as a CICD automated bot action thing on github. [] https://www.getdbt.com/ https://www.getdbt.com/
- twoodfin 5y agoMost of the rules seem reasonable, but what's the justification for this? Rule_L031: Avoid table aliases in from clauses and join conditions. I can't see what makes aliases clarifying in `select` or `where` but not in `join`. Also, inlined subqueries in a join must be given an alias, so there are cases where this rule can't be applied consistently.
- mulmen 5y agoAliases rarely add clarity in my experience. An alias is just an extra lookup when I need context. It’s a waste of mental swap space. Inlined subqueries are creating entirely new projections so they need descriptive names. If you are doing this you should consider a WITH anyway.
- pwildenhain 5y agoI think it depends on if your table names are readable or not. If you table names are "sales" and "orders" then I would argue that makes the sql a lot more readable i.e sales.amount orders.due_date vs s.amount ord.due_date In the second example I have to flip back and forth between the join statement in order to know where the columns are coming from. If you have awful table names though like qq123xc then I think aliasing would be appropriate
- cryptonector 5y agoThe moment you have the same table used more than once as a table source (in a JOIN) then this goes out the window. Moreover, there's nothing wrong with saying `SELECT ... FROM sales sales ...`.
- Orou 5y agoWholeheartedly agree with this. The problem isn't aliasing, it's lazy aliasing which is more I think what GP was referring to (e.g. emp.id instead of employee.id).
- cryptonector 5y agoThis rule is no good.
- slimebwoy 5y agoWe use this at my company to lint dbt sql code within our shared repositories. We are able to easily craft a set of rules that fit our style guides and ignore the rest.
- crooked-v 5y agoAfter relying on Prettier for a while, it feels very strange to me to have a linter that complains over minor formatting differences instead of just a tool that automatically fixes them.
- jdub 5y agoIt does both. Linting and fixing are done at different times.
- fuckthemachine 5y agoIs there a vim plugin for this yet? :)
- tunetheweb 5y agohttps://github.com/dense-analysis/ale/pull/3927 https://github.com/dense-analysis/ale/pull/3927
- synergy20 5y agoI used `pip install sqlformat` in the past but it seems no longer working for python3.8+, could not even find the executable anymore.