quarylabs / sqruff

Fast SQL formatter/linter
https://playground.quary.dev/?secondary=Format
Apache License 2.0
480 stars 17 forks source link

unexpected `layout.spacing` rule application on `grant` statements #924

Open demhadais opened 1 month ago

demhadais commented 1 month ago

I wouldn't necessarily classify this as a bug, but it seems unexpected to me that the following line of SQL:

grant some_role to a_user;

triggers the layout.spacing rule, causing sqruff to reformat to:

grant some_role to a_user ; -- there is now a space between 'a_user' and ';'

Is this behavior desired/expected? For reference, I'm running sqruff v0.20.2, and my .sqruff file is just

[.sqruff]
dialect = postgres
gvozdvmozgu commented 1 month ago

Strange, I can't reproduce that behavior in the playground.

gvozdvmozgu commented 1 month ago
PS C:\Users\gvozdvmozgu\Projects\sqruff> cat .\.sqlfluff
[.sqruff]
dialect = postgres
PS C:\Users\gvozdvmozgu\Projects\sqruff> cat .\pg.sql   
grant some_role to a_user;
PS C:\Users\gvozdvmozgu\Projects\sqruff> cargo r --quiet -- lint .\pg.sql
The linter processed 1 file(s).
All Finished 📜 🎉
PS C:\Users\gvozdvmozgu\Projects\sqruff> 
demhadais commented 1 month ago

My apologies - I didn't test the case where it's the only line in the file, in which case the bug doesn't occur. I'm trying to figure out what triggers it in my actual script, I'll reply with what I find shortly.

gvozdvmozgu commented 1 month ago

then most likely the error is due to incorrect parsing of the SQL file

demhadais commented 1 month ago

Okay, so it seems that

grant some_role to a_user with admin true;
grant some_role to a_user;

gets reformatted to

grant some_role to a_user with admin true ;
grant some_role to a_user ;

meaning that adding with [OPTION] [true/false] triggers it, and then sqruff just wants to apply that spacing to the rest of the statements in the file, including create table foo (bar text primary key);

gvozdvmozgu commented 1 month ago

The part "with admin" breaks the parser.

file:
- statement:
  - access_statement:
    - keyword: grant
    - whitespace: ' '
    - object_reference:
      - naked_identifier: some_role
    - whitespace: ' '
    - keyword: to
    - whitespace: ' '
    - role_reference:
      - naked_identifier: a_user
- whitespace: ' '
- file:
  - word: with
  - whitespace: ' '
  - word: admin
  - whitespace: ' '
  - word: 'true'
  - semicolon: ;
  - newline: |2+

  - word: grant
  - whitespace: ' '
  - word: some_role
  - whitespace: ' '
  - word: to
  - whitespace: ' '
  - word: a_user
  - semicolon: ;
- end_of_file: ''