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 - ALTER TABLE IF EXISTS and WHEN SYSTEM$STREAM_HAS_DATA() #3641

Merged
merged 5 commits into from Jul 21, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
31 changes: 30 additions & 1 deletion src/sqlfluff/dialects/dialect_snowflake.py
Expand Up @@ -347,6 +347,12 @@
name="integer_literal",
type="literal",
),
SystemFunctionName=RegexParser(
r"SYSTEM\$([A-z0-9]*)",
tunetheweb marked this conversation as resolved.
Show resolved Hide resolved
CodeSegment,
name="system_function_name",
type="system_function_name",
),
GroupByContentsGrammar=Delimited(
OneOf(
Ref("ColumnReferenceSegment"),
Expand Down Expand Up @@ -1464,6 +1470,7 @@ class AlterTableStatementSegment(ansi.AlterTableStatementSegment):
match_grammar = Sequence(
"ALTER",
"TABLE",
Ref("IfExistsGrammar", optional=True),
Ref("TableReferenceSegment"),
OneOf(
# Rename
Expand Down Expand Up @@ -2698,7 +2705,7 @@ class CreateTaskSegment(BaseSegment):
Sequence(
"WHEN",
Indent,
Ref("ExpressionSegment"),
Ref("TaskExpressionSegment"),
Dedent,
optional=True,
),
Expand All @@ -2711,6 +2718,28 @@ class CreateTaskSegment(BaseSegment):
)


class TaskExpressionSegment(BaseSegment):
"""Expressions for WHEN clause in TASK.

e.g. "SYSTEM$STREAM_HAS_DATA('MYSTREAM')"

"""

type = "snowflake_task_expression_segment"
match_grammar = Sequence(
Delimited(
OneOf(
Ref("ExpressionSegment"),
Sequence(
Ref("SystemFunctionName"),
Bracketed(Ref("QuotedLiteralSegment")),
),
),
delimiter=OneOf(Ref("BooleanBinaryOperatorGrammar")),
)
)


class CreateStatementSegment(BaseSegment):
"""A snowflake `CREATE` statement.

Expand Down
Expand Up @@ -65,3 +65,8 @@ alter table empl_info modify
ALTER TABLE empl_info DROP COLUMN my_column;
ALTER TABLE some_schema.empl_info DROP COLUMN my_column;
ALTER TABLE my_table DROP COLUMN column_1, column_2, column_3;

-- IF EXISTS
ALTER TABLE IF EXISTS my_table ADD COLUMN my_column INTEGER;
ALTER TABLE IF EXISTS empl_info DROP COLUMN my_column;
ALTER TABLE IF EXISTS empl_info RENAME COLUMN old_col_name TO new_col_name;
Expand Up @@ -3,7 +3,7 @@
# 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: 9c6169b3930295c33640ddc54adb221bf9df5ec14b9f39e401e0de5e53045a6d
_hash: 64007c88f4ec9aaa64e70b57eb300056d0f40a5df3b4207b954ea20759a00361
file:
- statement:
alter_table_statement:
Expand Down Expand Up @@ -544,3 +544,50 @@ file:
- column_reference:
identifier: column_3
- statement_terminator: ;
- statement:
alter_table_statement:
- keyword: ALTER
- keyword: TABLE
- keyword: IF
- keyword: EXISTS
- table_reference:
identifier: my_table
- alter_table_table_column_action:
- keyword: ADD
- keyword: COLUMN
- column_reference:
identifier: my_column
- data_type:
data_type_identifier: INTEGER
- statement_terminator: ;
- statement:
alter_table_statement:
- keyword: ALTER
- keyword: TABLE
- keyword: IF
- keyword: EXISTS
- table_reference:
identifier: empl_info
- alter_table_table_column_action:
- keyword: DROP
- keyword: COLUMN
- column_reference:
identifier: my_column
- statement_terminator: ;
- statement:
alter_table_statement:
- keyword: ALTER
- keyword: TABLE
- keyword: IF
- keyword: EXISTS
- table_reference:
identifier: empl_info
- alter_table_table_column_action:
- keyword: RENAME
- keyword: COLUMN
- column_reference:
identifier: old_col_name
- keyword: TO
- column_reference:
identifier: new_col_name
- statement_terminator: ;
10 changes: 10 additions & 0 deletions test/fixtures/dialects/snowflake/snowflake_create_task.sql
Expand Up @@ -83,3 +83,13 @@ CREATE OR REPLACE TASK insert__agg
SCHEDULE = 'USING CRON 15 7 2 * * UTC'
AS
CALL auto_device_insert();

CREATE OR REPLACE TASK SCH.MY_TASK
WAREHOUSE = MY_WH
SCHEDULE = 'USING CRON 15 7 2 * * UTC'
USER_TASK_TIMEOUT_MS = 10800000
WHEN
SYSTEM$STREAM_HAS_DATA('SCH.MY_STREAM')
AND 1=1
AS
CALL SCH.MY_SPROC();
51 changes: 50 additions & 1 deletion test/fixtures/dialects/snowflake/snowflake_create_task.yml
Expand Up @@ -3,7 +3,7 @@
# 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: ec9d42b15628824d95fd46d5486345a798957f38bdaa20137eb8bc76088bfecf
_hash: 83f68e6b6527766cec861ad292d0e24643ee6e4d95f8009241cdd6b5c3d99459
file:
- statement:
create_task_statement:
Expand Down Expand Up @@ -467,3 +467,52 @@ file:
start_bracket: (
end_bracket: )
- statement_terminator: ;
- statement:
create_task_statement:
- keyword: CREATE
- keyword: OR
- keyword: REPLACE
- keyword: TASK
- object_reference:
- identifier: SCH
- dot: .
- identifier: MY_TASK
- keyword: WAREHOUSE
- comparison_operator:
raw_comparison_operator: '='
- object_reference:
identifier: MY_WH
- keyword: SCHEDULE
- comparison_operator:
raw_comparison_operator: '='
- literal: "'USING CRON 15 7 2 * * UTC'"
- keyword: USER_TASK_TIMEOUT_MS
- comparison_operator:
raw_comparison_operator: '='
- literal: '10800000'
- keyword: WHEN
- snowflake_task_expression_segment:
system_function_name: SYSTEM$STREAM_HAS_DATA
bracketed:
start_bracket: (
literal: "'SCH.MY_STREAM'"
end_bracket: )
binary_operator: AND
expression:
- literal: '1'
- comparison_operator:
raw_comparison_operator: '='
- literal: '1'
- keyword: AS
- statement:
call_segment:
keyword: CALL
function:
function_name:
identifier: SCH
dot: .
function_name_identifier: MY_SPROC
bracketed:
start_bracket: (
end_bracket: )
- statement_terminator: ;