Skip to content
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

unexpected layout.spacing rule application on grant statements #924

Open
demhadais opened this issue Nov 2, 2024 · 6 comments
Open

unexpected layout.spacing rule application on grant statements #924

demhadais opened this issue Nov 2, 2024 · 6 comments
Labels
bug Something isn't working dialect

Comments

@demhadais
Copy link

demhadais commented Nov 2, 2024

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
Copy link
Collaborator

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

@gvozdvmozgu
Copy link
Collaborator

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
Copy link
Author

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
Copy link
Collaborator

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

@demhadais
Copy link
Author

demhadais commented Nov 2, 2024

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
Copy link
Collaborator

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: ''

@benfdking benfdking added bug Something isn't working dialect labels Nov 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working dialect
Projects
None yet
Development

No branches or pull requests

3 participants