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

Grammar: Fix TRUNCATE statement in Postgres dialect #2185

Merged
merged 4 commits into from Dec 26, 2021
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
36 changes: 36 additions & 0 deletions src/sqlfluff/dialects/dialect_postgres.py
Expand Up @@ -3105,3 +3105,39 @@ class DiscardStatementSegment(BaseSegment):
"TEMP",
),
)


@postgres_dialect.segment(replace=True)
class TruncateStatementSegment(BaseSegment):
"""`TRUNCATE TABLE` statement.

https://www.postgresql.org/docs/14/sql-truncate.html
"""

type = "truncate_table"
match_grammar = Sequence(
"TRUNCATE",
Ref.keyword("TABLE", optional=True),
Delimited(
OneOf(
Sequence(
Ref.keyword("ONLY", optional=True),
Ref("TableReferenceSegment"),
),
Sequence(
Ref("TableReferenceSegment"),
Ref("StarSegment", optional=True),
),
),
),
Sequence(
OneOf("RESTART", "CONTINUE"),
"IDENTITY",
optional=True,
),
OneOf(
"CASCADE",
"RESTRICT",
optional=True,
),
)
20 changes: 20 additions & 0 deletions test/fixtures/dialects/postgres/postgres_truncate.sql
@@ -0,0 +1,20 @@
TRUNCATE bigtable;
TRUNCATE some_schema.bigtable;
TRUNCATE TABLE bigtable;
TRUNCATE ONLY bigtable;
TRUNCATE TABLE ONLY bigtable;
TRUNCATE bigtable *;
TRUNCATE TABLE bigtable *;
TRUNCATE bigtable, fattable;
TRUNCATE TABLE bigtable, fattable;
TRUNCATE ONLY bigtable, fattable *;
TRUNCATE bigtable RESTART IDENTITY;
TRUNCATE bigtable CONTINUE IDENTITY;
TRUNCATE bigtable CASCADE;
TRUNCATE bigtable RESTRICT;
TRUNCATE TABLE
ONLY bigtable,
fattable *,
ONLY slimtable
CONTINUE IDENTITY CASCADE;

137 changes: 137 additions & 0 deletions test/fixtures/dialects/postgres/postgres_truncate.yml
@@ -0,0 +1,137 @@
# 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: b48839242732786fa04eb942fffb75a3fc7f0e195dd2da275337d7fe481be2d8
file:
- statement:
truncate_table:
keyword: TRUNCATE
table_reference:
identifier: bigtable
- statement_terminator: ;
- statement:
truncate_table:
keyword: TRUNCATE
table_reference:
- identifier: some_schema
- dot: .
- identifier: bigtable
- statement_terminator: ;
- statement:
truncate_table:
- keyword: TRUNCATE
- keyword: TABLE
- table_reference:
identifier: bigtable
- statement_terminator: ;
- statement:
truncate_table:
- keyword: TRUNCATE
- keyword: ONLY
- table_reference:
identifier: bigtable
- statement_terminator: ;
- statement:
truncate_table:
- keyword: TRUNCATE
- keyword: TABLE
- keyword: ONLY
- table_reference:
identifier: bigtable
- statement_terminator: ;
- statement:
truncate_table:
keyword: TRUNCATE
table_reference:
identifier: bigtable
star: '*'
- statement_terminator: ;
- statement:
truncate_table:
- keyword: TRUNCATE
- keyword: TABLE
- table_reference:
identifier: bigtable
- star: '*'
- statement_terminator: ;
- statement:
truncate_table:
- keyword: TRUNCATE
- table_reference:
identifier: bigtable
- comma: ','
- table_reference:
identifier: fattable
- statement_terminator: ;
- statement:
truncate_table:
- keyword: TRUNCATE
- keyword: TABLE
- table_reference:
identifier: bigtable
- comma: ','
- table_reference:
identifier: fattable
- statement_terminator: ;
- statement:
truncate_table:
- keyword: TRUNCATE
- keyword: ONLY
- table_reference:
identifier: bigtable
- comma: ','
- table_reference:
identifier: fattable
- star: '*'
- statement_terminator: ;
- statement:
truncate_table:
- keyword: TRUNCATE
- table_reference:
identifier: bigtable
- keyword: RESTART
- keyword: IDENTITY
- statement_terminator: ;
- statement:
truncate_table:
- keyword: TRUNCATE
- table_reference:
identifier: bigtable
- keyword: CONTINUE
- keyword: IDENTITY
- statement_terminator: ;
- statement:
truncate_table:
- keyword: TRUNCATE
- table_reference:
identifier: bigtable
- keyword: CASCADE
- statement_terminator: ;
- statement:
truncate_table:
- keyword: TRUNCATE
- table_reference:
identifier: bigtable
- keyword: RESTRICT
- statement_terminator: ;
- statement:
truncate_table:
- keyword: TRUNCATE
- keyword: TABLE
- keyword: ONLY
- table_reference:
identifier: bigtable
- comma: ','
- table_reference:
identifier: fattable
- star: '*'
- comma: ','
- keyword: ONLY
- table_reference:
identifier: slimtable
- keyword: CONTINUE
- keyword: IDENTITY
- keyword: CASCADE
- statement_terminator: ;