Skip to content

Commit

Permalink
Grammar: Tidy up transaction statements in Snowflake dialect (#2196)
Browse files Browse the repository at this point in the history
Co-authored-by: Barry Pollard <barry@tunetheweb.com>
  • Loading branch information
jpy-git and tunetheweb committed Dec 26, 2021
1 parent 909bd57 commit 212432f
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/sqlfluff/dialects/dialect_snowflake.py
Expand Up @@ -3347,6 +3347,33 @@ class DescribeStatementSegment(BaseSegment):
)


@snowflake_dialect.segment(replace=True)
class TransactionStatementSegment(BaseSegment):
"""`BEGIN`, `START TRANSACTION`, `COMMIT`, AND `ROLLBACK` statement grammar.
Overwrites ANSI to match correct Snowflake grammar.
https://docs.snowflake.com/en/sql-reference/sql/begin.html
https://docs.snowflake.com/en/sql-reference/sql/commit.html
https://docs.snowflake.com/en/sql-reference/sql/rollback.html
"""

match_grammar = OneOf(
Sequence(
"BEGIN",
OneOf("WORK", "TRANSACTION", optional=True),
Sequence("NAME", Ref("ObjectReferenceSegment"), optional=True),
),
Sequence(
"START",
"TRANSACTION",
Sequence("NAME", Ref("ObjectReferenceSegment"), optional=True),
),
"COMMIT",
"ROLLBACK",
)


@snowflake_dialect.segment(replace=True)
class TruncateStatementSegment(BaseSegment):
"""`TRUNCATE TABLE` statement.
Expand Down
10 changes: 10 additions & 0 deletions test/fixtures/dialects/snowflake/snowflake_transactions.sql
@@ -0,0 +1,10 @@
begin;
begin work;
begin transaction;
begin name t4;
begin work name t4;
begin transaction name t4;
start transaction;
start transaction name t4;
rollback;
commit;
65 changes: 65 additions & 0 deletions test/fixtures/dialects/snowflake/snowflake_transactions.yml
@@ -0,0 +1,65 @@
# 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: edb9a85fc338df4d07be8e6d5881efc1fa5f83ff480d508ee17409dddbb99093
file:
- statement:
base:
keyword: begin
- statement_terminator: ;
- statement:
base:
- keyword: begin
- keyword: work
- statement_terminator: ;
- statement:
base:
- keyword: begin
- keyword: transaction
- statement_terminator: ;
- statement:
base:
- keyword: begin
- keyword: name
- object_reference:
identifier: t4
- statement_terminator: ;
- statement:
base:
- keyword: begin
- keyword: work
- keyword: name
- object_reference:
identifier: t4
- statement_terminator: ;
- statement:
base:
- keyword: begin
- keyword: transaction
- keyword: name
- object_reference:
identifier: t4
- statement_terminator: ;
- statement:
base:
- keyword: start
- keyword: transaction
- statement_terminator: ;
- statement:
base:
- keyword: start
- keyword: transaction
- keyword: name
- object_reference:
identifier: t4
- statement_terminator: ;
- statement:
base:
keyword: rollback
- statement_terminator: ;
- statement:
base:
keyword: commit
- statement_terminator: ;

0 comments on commit 212432f

Please sign in to comment.