From 5afea259a6b7e85f3e80914e64c346682fb1c097 Mon Sep 17 00:00:00 2001 From: Sam Lader Date: Mon, 11 Oct 2021 23:41:58 +0100 Subject: [PATCH] Postgres dialect: Fix parse error for "on delete", "on update" clauses in column constraints (#1586) * Issue #1554: Postgres dialect: Parse error for "on delete" in column constraint * Fix for UPDATE keyword and update tests Co-authored-by: Alan Cruickshank Co-authored-by: Barry Pollard --- src/sqlfluff/dialects/dialect_postgres.py | 6 ++ .../parser/postgres/postgres_create_table.sql | 10 +++ .../parser/postgres/postgres_create_table.yml | 84 ++++++++++++++++++- 3 files changed, 99 insertions(+), 1 deletion(-) diff --git a/src/sqlfluff/dialects/dialect_postgres.py b/src/sqlfluff/dialects/dialect_postgres.py index b5ccaaa4a7b..875b5581290 100644 --- a/src/sqlfluff/dialects/dialect_postgres.py +++ b/src/sqlfluff/dialects/dialect_postgres.py @@ -1029,6 +1029,12 @@ class ColumnConstraintSegment(BaseSegment): Ref("ColumnReferenceSegment"), # Foreign columns making up FOREIGN KEY constraint Ref("BracketedColumnReferenceListGrammar", optional=True), + Sequence( + "ON", + OneOf("DELETE", "UPDATE"), + Ref("ReferentialActionSegment"), + optional=True, + ), ), ), OneOf("DEFERRABLE", Sequence("NOT", "DEFERRABLE"), optional=True), diff --git a/test/fixtures/parser/postgres/postgres_create_table.sql b/test/fixtures/parser/postgres/postgres_create_table.sql index dd6dbcc2967..94642b8970c 100644 --- a/test/fixtures/parser/postgres/postgres_create_table.sql +++ b/test/fixtures/parser/postgres/postgres_create_table.sql @@ -212,3 +212,13 @@ operation_id int4 NOT NULL DEFAULT '-1'::integer CREATE TABLE main.test_table ( "col1" character varying(40) NOT NULL ); + +CREATE TABLE groups ( + group_id INTEGER PRIMARY KEY generated BY DEFAULT AS IDENTITY +); + +CREATE TABLE users ( + user_id INTEGER PRIMARY KEY generated BY DEFAULT AS IDENTITY, + group_id INTEGER REFERENCES groups (group_id) ON DELETE CASCADE, + domain_id INTEGER REFERENCES groups (group_id) ON UPDATE RESTRICT +); diff --git a/test/fixtures/parser/postgres/postgres_create_table.yml b/test/fixtures/parser/postgres/postgres_create_table.yml index 584441490e0..f0175784587 100644 --- a/test/fixtures/parser/postgres/postgres_create_table.yml +++ b/test/fixtures/parser/postgres/postgres_create_table.yml @@ -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: 0ef79bbd602bc4202e929d6764458043407d864a13ed41260627d3ad66420b22 +_hash: 463feff7c6b4deebbfc0bdd82d953eb4544d40a5e359724a4c64a6631ad6c37a file: - statement: create_table_statement: @@ -1233,3 +1233,85 @@ file: - keyword: 'NULL' end_bracket: ) - statement_terminator: ; +- statement: + create_table_statement: + - keyword: CREATE + - keyword: TABLE + - table_reference: + identifier: groups + - bracketed: + - start_bracket: ( + - column_reference: + identifier: group_id + - data_type: + data_type_identifier: INTEGER + - column_constraint_segment: + - keyword: PRIMARY + - keyword: KEY + - column_constraint_segment: + - keyword: generated + - keyword: BY + - keyword: DEFAULT + - keyword: AS + - keyword: IDENTITY + - end_bracket: ) +- statement_terminator: ; +- statement: + create_table_statement: + - keyword: CREATE + - keyword: TABLE + - table_reference: + identifier: users + - bracketed: + - start_bracket: ( + - column_reference: + identifier: user_id + - data_type: + data_type_identifier: INTEGER + - column_constraint_segment: + - keyword: PRIMARY + - keyword: KEY + - column_constraint_segment: + - keyword: generated + - keyword: BY + - keyword: DEFAULT + - keyword: AS + - keyword: IDENTITY + - comma: ',' + - column_reference: + identifier: group_id + - data_type: + data_type_identifier: INTEGER + - column_constraint_segment: + - keyword: REFERENCES + - column_reference: + identifier: groups + - bracketed: + start_bracket: ( + column_reference: + identifier: group_id + end_bracket: ) + - keyword: 'ON' + - keyword: DELETE + - referential_action: + keyword: CASCADE + - comma: ',' + - column_reference: + identifier: domain_id + - data_type: + data_type_identifier: INTEGER + - column_constraint_segment: + - keyword: REFERENCES + - column_reference: + identifier: groups + - bracketed: + start_bracket: ( + column_reference: + identifier: group_id + end_bracket: ) + - keyword: 'ON' + - keyword: UPDATE + - referential_action: + keyword: RESTRICT + - end_bracket: ) +- statement_terminator: ;