Skip to content

Commit

Permalink
Snowflake: Add implementation for CREATE TASK statement (sqlfluff#1597)
Browse files Browse the repository at this point in the history
  • Loading branch information
Joe Hutter committed Oct 12, 2021
1 parent 42961a6 commit 3cdf37a
Show file tree
Hide file tree
Showing 7 changed files with 245 additions and 1 deletion.
88 changes: 87 additions & 1 deletion src/sqlfluff/dialects/dialect_snowflake.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@
"TERSE",
"TABULAR",
"UNSET",
"USER_TASK_TIMEOUT_MS",
"USER_TASK_MANAGED_INITIAL_WAREHOUSE_SIZE",
"WAIT_FOR_COMPLETION",
"WAREHOUSE_SIZE",
]
Expand Down Expand Up @@ -449,6 +451,7 @@ class StatementSegment(ansi_dialect.get_segment("StatementSegment")): # type: i
insert=[
Ref("UseStatementSegment"),
Ref("CreateStatementSegment"),
Ref("CreateTaskSegment"),
Ref("CreateCloneStatementSegment"),
Ref("ShowStatementSegment"),
Ref("AlterUserSegment"),
Expand Down Expand Up @@ -1368,6 +1371,90 @@ class CreateTableStatementSegment(BaseSegment):
)


@snowflake_dialect.segment()
class CreateTaskSegment(BaseSegment):
"""A snowflake `CREATE TASK` statement.
https://docs.snowflake.com/en/sql-reference/sql/create-task.html
"""

type = "create_task_statement"

match_grammar = Sequence(
"CREATE",
Sequence("OR", "REPLACE", optional=True),
"TASK",
Sequence("IF", "NOT", "EXISTS", optional=True),
Ref("ObjectReferenceSegment"),
Indent,
Sequence(
"WAREHOUSE",
Ref("EqualsSegment"),
Ref("ObjectReferenceSegment"),
optional=True,
),
Sequence(
"SCHEDULE",
Ref("EqualsSegment"),
Ref("QuotedLiteralSegment"),
optional=True,
),
Sequence(
"ALLOW_OVERLAPPING_EXECUTION",
Ref("EqualsSegment"),
Ref("BooleanLiteralGrammar"),
optional=True,
),
Delimited(
Sequence(
Ref("ParameterNameSegment"),
Ref("EqualsSegment"),
OneOf(
Ref("BooleanLiteralGrammar"),
Ref("QuotedLiteralSegment"),
Ref("NumericLiteralSegment"),
),
),
delimiter=Ref("CommaSegment"),
optional=True,
),
Sequence(
"USER_TASK_TIMEOUT_MS",
Ref("EqualsSegment"),
Ref("NumericLiteralSegment"),
optional=True,
),
Sequence(
"USER_TASK_MANAGED_INITIAL_WAREHOUSE_SIZE",
Ref("EqualsSegment"),
Ref("QuotedLiteralSegment"),
optional=True,
),
Sequence(
"COPY",
"GRANTS",
optional=True,
),
Ref("CreateStatementCommentSegment", optional=True),
Sequence(
"AFTER",
Ref("QuotedLiteralSegment"),
optional=True,
),
Dedent,
"WHEN",
Indent,
Ref("ExpressionSegment"),
Dedent,
Sequence(
Ref.keyword("AS"),
Indent,
Ref("StatementSegment"),
Dedent,
),
)


@snowflake_dialect.segment()
class CreateStatementSegment(BaseSegment):
"""A snowflake `CREATE` statement.
Expand Down Expand Up @@ -1405,7 +1492,6 @@ class CreateStatementSegment(BaseSegment):
Sequence("FILE", "FORMAT"),
"STAGE",
"STREAM",
"TASK",
),
Sequence("IF", "NOT", "EXISTS", optional=True),
Ref("ObjectReferenceSegment"),
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/dialects/snowflake/snowflake_create_task.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CREATE TASK t1
SCHEDULE = 'USING CRON 0 9-17 * * SUN America/Los_Angeles'
TIMESTAMP_INPUT_FORMAT = 'YYYY-MM-DD HH24'
USER_TASK_MANAGED_INITIAL_WAREHOUSE_SIZE = 'XSMALL'
AS
INSERT INTO mytable(ts) VALUES(1);
41 changes: 41 additions & 0 deletions test/fixtures/dialects/snowflake/snowflake_create_task.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# 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: af2323a0483bae83a81490bc28c4d8acd8b0c793aaa661b9dc0ed6d2d85fa7de
file:
statement:
create_task_statement:
- keyword: CREATE
- keyword: TASK
- object_reference:
identifier: t1
- keyword: SCHEDULE
- comparison_operator: '='
- literal: "'USING CRON 0 9-17 * * SUN America/Los_Angeles'"
- parameter: TIMESTAMP_INPUT_FORMAT
- comparison_operator: '='
- literal: "'YYYY-MM-DD HH24'"
- keyword: USER_TASK_MANAGED_INITIAL_WAREHOUSE_SIZE
- comparison_operator: '='
- literal: "'XSMALL'"
- keyword: AS
- statement:
insert_statement:
- keyword: INSERT
- keyword: INTO
- table_reference:
identifier: mytable
- bracketed:
start_bracket: (
column_reference:
identifier: ts
end_bracket: )
- values_clause:
keyword: VALUES
bracketed:
start_bracket: (
literal: '1'
end_bracket: )
statement_terminator: ;
12 changes: 12 additions & 0 deletions test/fixtures/dialects/snowflake/snowflake_create_task_all.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
CREATE OR REPLACE TASK IF NOT EXISTS t1
WAREHOUSE = mywh
SCHEDULE = 'USING CRON 0 9-17 * * SUN America/Los_Angeles'
ALLOW_OVERLAPPING_EXECUTION = TRUE
TIMESTAMP_INPUT_FORMAT = 'YYYY-MM-DD HH24'
USER_TASK_TIMEOUT_MS = 25
USER_TASK_MANAGED_INITIAL_WAREHOUSE_SIZE = 'XSMALL'
COPY GRANTS
COMMENT = 'Hello world'
AFTER 'dependency_task'
AS
INSERT INTO mytable(ts) VALUES(1);
64 changes: 64 additions & 0 deletions test/fixtures/dialects/snowflake/snowflake_create_task_all.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# 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: 3a40628753cbb7ee12f31d7f3738a8a18959c01821cb3db3c542cd17a2a04aa3
file:
statement:
create_task_statement:
- keyword: CREATE
- binary_operator: OR
- keyword: REPLACE
- keyword: TASK
- keyword: IF
- keyword: NOT
- keyword: EXISTS
- object_reference:
identifier: t1
- keyword: WAREHOUSE
- comparison_operator: '='
- object_reference:
identifier: mywh
- keyword: SCHEDULE
- comparison_operator: '='
- literal: "'USING CRON 0 9-17 * * SUN America/Los_Angeles'"
- keyword: ALLOW_OVERLAPPING_EXECUTION
- comparison_operator: '='
- literal: 'TRUE'
- parameter: TIMESTAMP_INPUT_FORMAT
- comparison_operator: '='
- literal: "'YYYY-MM-DD HH24'"
- keyword: USER_TASK_TIMEOUT_MS
- comparison_operator: '='
- literal: '25'
- keyword: USER_TASK_MANAGED_INITIAL_WAREHOUSE_SIZE
- comparison_operator: '='
- literal: "'XSMALL'"
- keyword: COPY
- keyword: GRANTS
- snowflake_comment:
keyword: COMMENT
comparison_operator: '='
literal: "'Hello world'"
- keyword: AFTER
- literal: "'dependency_task'"
- keyword: AS
- statement:
insert_statement:
- keyword: INSERT
- keyword: INTO
- table_reference:
identifier: mytable
- bracketed:
start_bracket: (
column_reference:
identifier: ts
end_bracket: )
- values_clause:
keyword: VALUES
bracketed:
start_bracket: (
literal: '1'
end_bracket: )
statement_terminator: ;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CREATE TASK t1
AS
INSERT INTO mytable(ts) VALUES(1);
32 changes: 32 additions & 0 deletions test/fixtures/dialects/snowflake/snowflake_create_task_minimal.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# 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: 5e7d90046023eb14b11e0b6ee84b48e2160de04eae5298e041dbf4de00598a2c
file:
statement:
create_task_statement:
- keyword: CREATE
- keyword: TASK
- object_reference:
identifier: t1
- keyword: AS
- statement:
insert_statement:
- keyword: INSERT
- keyword: INTO
- table_reference:
identifier: mytable
- bracketed:
start_bracket: (
column_reference:
identifier: ts
end_bracket: )
- values_clause:
keyword: VALUES
bracketed:
start_bracket: (
literal: '1'
end_bracket: )
statement_terminator: ;

0 comments on commit 3cdf37a

Please sign in to comment.