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

Snowflake: Support TOP N select clause modifier #2222

Merged
merged 3 commits into from Jan 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/sqlfluff/dialects/dialect_snowflake.py
Expand Up @@ -877,6 +877,21 @@ class SelectStatementSegment(ansi_dialect.get_segment("SelectStatementSegment"))
)


@snowflake_dialect.segment(replace=True)
class SelectClauseModifierSegment(BaseSegment):
"""Things that come after SELECT but before the columns, specifically for Snowflake.

https://docs.snowflake.com/en/sql-reference/constructs.html
"""

type = "select_clause_modifier"
match_grammar = Sequence(
OneOf("DISTINCT", "ALL", optional=True),
# TOP N is unique to Snowflake, and we can optionally add DISTINCT/ALL in front of it.
Sequence("TOP", Ref("NumericLiteralSegment"), optional=True),
)


@snowflake_dialect.segment()
class AlterTableColumnStatementSegment(BaseSegment):
"""An `ALTER TABLE .. ALTER COLUMN` statement.
Expand Down
1 change: 1 addition & 0 deletions src/sqlfluff/dialects/dialect_snowflake_keywords.py
Expand Up @@ -378,6 +378,7 @@
TEXT
TIME
TIMESTAMP
TOP
TRANSACTION
TRANSACTIONS
TRANSIENT
Expand Down
@@ -0,0 +1,21 @@
SELECT *, col1, col2, my_table.col1, my_table.* FROM my_table;

SELECT DISTINCT * FROM my_table;

SELECT DISTINCT col1 FROM my_table;

SELECT ALL my_table.* FROM my_table;

SELECT TOP 1 * FROM my_table;

SELECT TOP 2 col1 FROM my_table;

SELECT TOP 3 col1, my_table.* FROM my_table;

SELECT ALL TOP 10 col1 FROM my_table;

SELECT DISTINCT TOP 20 my_table.col1 FROM my_table;

SELECT DISTINCT TOP 30 * FROM my_table;

SELECT DISTINCT TOP 40 col1, my_table.* FROM my_table;
247 changes: 247 additions & 0 deletions test/fixtures/dialects/snowflake/snowflake_select_clause_modifiers.yml
@@ -0,0 +1,247 @@
# YML test files are auto-generated from SQL files and should not be edited by
# hand. To help enforce this, the "hash" field in the file must match a hash
# computed by SQLFluff when running the tests. Please run
# `python test/generate_parse_fixture_yml.py` to generate them after adding or
# altering SQL files.
_hash: c2f7708096f526a51c7f5d0e0afe420cdbff71fc0cc1ef0d9605edc31577b578
file:
- statement:
select_statement:
select_clause:
- keyword: SELECT
- select_clause_element:
wildcard_expression:
wildcard_identifier:
star: '*'
- comma: ','
- select_clause_element:
column_reference:
identifier: col1
- comma: ','
- select_clause_element:
column_reference:
identifier: col2
- comma: ','
- select_clause_element:
column_reference:
- identifier: my_table
- dot: .
- identifier: col1
- comma: ','
- select_clause_element:
wildcard_expression:
wildcard_identifier:
identifier: my_table
dot: .
star: '*'
from_clause:
keyword: FROM
from_expression:
from_expression_element:
table_expression:
table_reference:
identifier: my_table
- statement_terminator: ;
- statement:
select_statement:
select_clause:
keyword: SELECT
select_clause_modifier:
keyword: DISTINCT
select_clause_element:
wildcard_expression:
wildcard_identifier:
star: '*'
from_clause:
keyword: FROM
from_expression:
from_expression_element:
table_expression:
table_reference:
identifier: my_table
- statement_terminator: ;
- statement:
select_statement:
select_clause:
keyword: SELECT
select_clause_modifier:
keyword: DISTINCT
select_clause_element:
column_reference:
identifier: col1
from_clause:
keyword: FROM
from_expression:
from_expression_element:
table_expression:
table_reference:
identifier: my_table
- statement_terminator: ;
- statement:
select_statement:
select_clause:
keyword: SELECT
select_clause_modifier:
keyword: ALL
select_clause_element:
wildcard_expression:
wildcard_identifier:
identifier: my_table
dot: .
star: '*'
from_clause:
keyword: FROM
from_expression:
from_expression_element:
table_expression:
table_reference:
identifier: my_table
- statement_terminator: ;
- statement:
select_statement:
select_clause:
keyword: SELECT
select_clause_modifier:
keyword: TOP
literal: '1'
select_clause_element:
wildcard_expression:
wildcard_identifier:
star: '*'
from_clause:
keyword: FROM
from_expression:
from_expression_element:
table_expression:
table_reference:
identifier: my_table
- statement_terminator: ;
- statement:
select_statement:
select_clause:
keyword: SELECT
select_clause_modifier:
keyword: TOP
literal: '2'
select_clause_element:
column_reference:
identifier: col1
from_clause:
keyword: FROM
from_expression:
from_expression_element:
table_expression:
table_reference:
identifier: my_table
- statement_terminator: ;
- statement:
select_statement:
select_clause:
- keyword: SELECT
- select_clause_modifier:
keyword: TOP
literal: '3'
- select_clause_element:
column_reference:
identifier: col1
- comma: ','
- select_clause_element:
wildcard_expression:
wildcard_identifier:
identifier: my_table
dot: .
star: '*'
from_clause:
keyword: FROM
from_expression:
from_expression_element:
table_expression:
table_reference:
identifier: my_table
- statement_terminator: ;
- statement:
select_statement:
select_clause:
keyword: SELECT
select_clause_modifier:
- keyword: ALL
- keyword: TOP
- literal: '10'
select_clause_element:
column_reference:
identifier: col1
from_clause:
keyword: FROM
from_expression:
from_expression_element:
table_expression:
table_reference:
identifier: my_table
- statement_terminator: ;
- statement:
select_statement:
select_clause:
keyword: SELECT
select_clause_modifier:
- keyword: DISTINCT
- keyword: TOP
- literal: '20'
select_clause_element:
column_reference:
- identifier: my_table
- dot: .
- identifier: col1
from_clause:
keyword: FROM
from_expression:
from_expression_element:
table_expression:
table_reference:
identifier: my_table
- statement_terminator: ;
- statement:
select_statement:
select_clause:
keyword: SELECT
select_clause_modifier:
- keyword: DISTINCT
- keyword: TOP
- literal: '30'
select_clause_element:
wildcard_expression:
wildcard_identifier:
star: '*'
from_clause:
keyword: FROM
from_expression:
from_expression_element:
table_expression:
table_reference:
identifier: my_table
- statement_terminator: ;
- statement:
select_statement:
select_clause:
- keyword: SELECT
- select_clause_modifier:
- keyword: DISTINCT
- keyword: TOP
- literal: '40'
- select_clause_element:
column_reference:
identifier: col1
- comma: ','
- select_clause_element:
wildcard_expression:
wildcard_identifier:
identifier: my_table
dot: .
star: '*'
from_clause:
keyword: FROM
from_expression:
from_expression_element:
table_expression:
table_reference:
identifier: my_table
- statement_terminator: ;