New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comma is removed when fixing query with multiple CTEs #939
Comments
🤔 Weird -- I'm currently working on comma stuff in #934 so I'll take this on and hopefully solve it in that issue |
Also tagging #1085 since it's definitely related -- and shows that the issue stands for trailing and leading commas. |
I don't have a solid answer to this, but I've been playing around a little bit in the debugger, and have something to show for it. I thought the issue would be with the rule itself. But maybe it's with the underlying fix engine? Here is the state of the fix while we're still crawling looking for fixes.
Looks like the leading comma is in there as expected 👍🏻 But by the time we get to the linter, it seems like we've lost it 😢
Tagging @alanmcruickshank or @barrywhart to see if they have any thoughts on how the fix would get lost in translation. As @plazar pointed out, it likely has something to do with the jinja templating, since this isn't a problem when we drop the jinja templating (which I can confirm). sqlfluff parse output
|
NOTE: Updated the SQL example in the description to address other linting issues, leaving only the L019 issue. This may make it easier to investigate the issue. |
@pwildenhain: I have a PR now which fixes this. I've noticed with other rules that certain kinds of fixes cause the file to be corrupted in various ways. In this case, an "edit" fix was replacing a segment with a list of segments that included itself. This works sometimes but not others. TBH, this should probably be flagged as a "bad fix" by the fix engine, rather than allowing unpredictable behavior. My PR only addresses the specific rule issue, not the deeper issue. With this fix, I don't think #1085 is related after all, but I'm not sure because I don't use dbt, nor have I worked on the dbt template. |
When fixing a query with multiple CTEs I find that the comma separating them is occasionally removed, resulting in invalid SQL. I'm using
dialect = snowflake
and a simple example query is:I've done some detective work and the comma is removed only when I set
comma_style=leading
for rule L019.Interestingly, if I manually replace the jinja macro
{{ source('schema', 'table') }}
withschema_table
then the leading comma is correctly inserted and the fixed query is valid!Expected Behaviour
I'm expect that
sqlfluff fix ...
correctly removes the trailing command after the first CTE in the query above and places a leading comma before the second CTE.Observed Behaviour
The trailing comma is removed and no leading comma is inserted.
Steps to Reproduce
.sqlfluff
file (see below) in the same directorysqlfluff fix bug.sql
-> trailing comma is removed, no leading comma is insertedAnd to reproduce some of the odd behaviour I saw (in case it's helpful to locate the issue):
Replace
{{ source('schema', 'table') }}
withschema_table
in the queryReset
{{ source('schema', 'table') }}
, changecomma_style = leading
withcomma_style = trailing
in.sqlfluff
Version
Configuration
Extra info
Output from running
sqlfluff fix
:Resulting
.sql
file (notice the comma missing between the first and second CTE):Finally
I'd be willing to try to fix the code and open a PR. However, I'm very new to
sqlfluff
and I don't know if this is an easy fix or a hard one. Also, I would need some guidance about where to look and what to investigate.The text was updated successfully, but these errors were encountered: