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: Split out CREATE VIEW into its own segment #2217

Merged
merged 7 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
55 changes: 51 additions & 4 deletions src/sqlfluff/dialects/dialect_snowflake.py
Expand Up @@ -1761,7 +1761,7 @@ class CreateStatementSegment(BaseSegment):

match_grammar = Sequence(
"CREATE",
Sequence("OR", "REPLACE", optional=True),
Ref("OrReplaceGrammar", optional=True),
OneOf(
Sequence("NETWORK", "POLICY"),
Sequence("RESOURCE", "MONITOR"),
Expand All @@ -1772,9 +1772,7 @@ class CreateStatementSegment(BaseSegment):
Sequence("NOTIFICATION", "INTEGRATION"),
Sequence("SECURITY", "INTEGRATION"),
Sequence("STORAGE", "INTEGRATION"),
"VIEW",
Sequence("MATERIALIZED", "VIEW"),
Sequence("SECURE", "VIEW"),
Sequence("MASKING", "POLICY"),
"PIPE",
Sequence("EXTERNAL", "FUNCTION"),
Expand All @@ -1784,7 +1782,7 @@ class CreateStatementSegment(BaseSegment):
Sequence("FILE", "FORMAT"),
"STREAM",
),
Sequence("IF", "NOT", "EXISTS", optional=True),
Ref("IfNotExistsGrammar", optional=True),
Ref("ObjectReferenceSegment"),
# Next set are Pipe statements https://docs.snowflake.com/en/sql-reference/sql/create-pipe.html
Sequence(
Expand Down Expand Up @@ -1842,6 +1840,55 @@ class CreateStatementSegment(BaseSegment):
)


@snowflake_dialect.segment(replace=True)
class CreateViewStatementSegment(BaseSegment):
"""A `CREATE VIEW` statement, specifically for Snowflake's dialect.

https://docs.snowflake.com/en/sql-reference/sql/create-view.html
wong-codaio marked this conversation as resolved.
Show resolved Hide resolved
"""

type = "create_view_statement"

match_grammar = Sequence(
"CREATE",
Ref("OrReplaceGrammar", optional=True),
AnyNumberOf(
"SECURE",
"RECURSIVE",
),
"VIEW",
Ref("IfNotExistsGrammar", optional=True),
Ref("TableReferenceSegment"),
AnyNumberOf(
Bracketed(
Delimited(
Sequence(
Ref("ColumnReferenceSegment"),
Ref("CommentClauseSegment", optional=True),
),
),
),
Sequence(
Ref.keyword("WITH", optional=True),
"ROW",
"ACCESS",
"POLICY",
Ref("NakedIdentifierSegment"),
"ON",
Bracketed(
Delimited(Ref("ColumnReferenceSegment")),
),
),
Ref("TagBracketedEqualsSegment"),
Sequence("COPY", "GRANTS"),
Ref("CreateStatementCommentSegment"),
# @TODO: Support column-level masking policy & tagging.
),
"AS",
Ref("SelectableGrammar"),
)


@snowflake_dialect.segment()
class FileFormatSegment(BaseSegment):
"""A Snowflake FILE_FORMAT Segment.
Expand Down
42 changes: 42 additions & 0 deletions test/fixtures/dialects/snowflake/snowflake_create_view.sql
@@ -0,0 +1,42 @@

create view another_view comment = 'a great description' as select col_1, col_2 from other_table;

CREATE VIEW basic_view AS SELECT col1, col2 FROM src_table;

CREATE VIEW view_with_comments
COMMENT = 'my comment' AS SELECT col1, col2 FROM src_table;

CREATE OR REPLACE VIEW view_with_replace_and_comment
COMMENT = 'my comment' AS SELECT col1, col2 FROM src_table;

CREATE OR REPLACE SECURE RECURSIVE VIEW IF NOT EXISTS secure_recursive_view_with_comment
COMMENT = 'my comment' AS SELECT col1, col2 FROM src_table;

CREATE OR REPLACE VIEW view_with_comment_and_copy_grants
COMMENT = 'my comment'
COPY GRANTS
AS SELECT col1, col2 FROM src_table;

CREATE OR REPLACE VIEW view_with_tags_and_copy_grants
WITH TAG (foo = 'bar', hello = 'world')
COPY GRANTS
AS SELECT col1, col2 FROM src_table;

CREATE OR REPLACE VIEW view_with_column_comment
(
col1,
col2 COMMENT 'some comment'
)
AS SELECT col1, col2 FROM src_table;

CREATE OR REPLACE SECURE RECURSIVE VIEW IF NOT EXISTS view_with_all_implemented_features
COMMENT = 'table-level comment'
(
col1,
col2 COMMENT 'some comment'
)
jpy-git marked this conversation as resolved.
Show resolved Hide resolved
AS
WITH cte AS (SELECT col1 FROM table_1)
SELECT col1, col2
FROM table_2
INNER JOIN my_cte ON table_1.pk = table_2.pk;