Skip to content

Commit

Permalink
Allow optional keywords in create table unique constraints (#2077)
Browse files Browse the repository at this point in the history
* Add optional keywords to `create table` unique constraint

* More tests

* Move [INDEX | KEY] [index_name] to MySql dialect

* Linter

* Remove files moved
  • Loading branch information
kayman-mk committed Dec 11, 2021
1 parent 982aef1 commit 31c1b1f
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/sqlfluff/dialects/dialect_mysql.py
Expand Up @@ -231,6 +231,47 @@ class CreateTableStatementSegment(
)


@mysql_dialect.segment(replace=True)
class TableConstraintSegment(BaseSegment):
"""A table constraint, e.g. for CREATE TABLE."""

type = "table_constraint_segment"
# Later add support for CHECK constraint, others?
# e.g. CONSTRAINT constraint_1 PRIMARY KEY(column_1)
match_grammar = Sequence(
Sequence( # [ CONSTRAINT <Constraint name> ]
"CONSTRAINT", Ref("ObjectReferenceSegment"), optional=True
),
OneOf(
Sequence( # UNIQUE [INDEX | KEY] [index_name] ( column_name [, ... ] )
"UNIQUE",
OneOf("INDEX", "KEY", optional=True),
Ref("ObjectReferenceSegment", optional=True),
Ref("BracketedColumnReferenceListGrammar"),
# Later add support for index_parameters?
),
Sequence( # PRIMARY KEY ( column_name [, ... ] ) index_parameters
Ref("PrimaryKeyGrammar"),
# Columns making up PRIMARY KEY constraint
Ref("BracketedColumnReferenceListGrammar"),
# Later add support for index_parameters?
),
Sequence( # FOREIGN KEY ( column_name [, ... ] )
# REFERENCES reftable [ ( refcolumn [, ... ] ) ]
Ref("ForeignKeyGrammar"),
# Local columns making up FOREIGN KEY constraint
Ref("BracketedColumnReferenceListGrammar"),
"REFERENCES",
Ref("ColumnReferenceSegment"),
# Foreign columns making up FOREIGN KEY constraint
Ref("BracketedColumnReferenceListGrammar"),
# Later add support for [MATCH FULL/PARTIAL/SIMPLE] ?
# Later add support for [ ON DELETE/UPDATE action ] ?
),
),
)


mysql_dialect.add(
DoubleForwardSlashSegment=StringParser(
"//", SymbolSegment, name="doubleforwardslash", type="statement_terminator"
Expand Down
@@ -0,0 +1,9 @@
CREATE TABLE a(
a INT NOT NULL,
UNIQUE (a),
UNIQUE idx_c(a),
UNIQUE KEY (a),
UNIQUE KEY idx_a(a),
UNIQUE INDEX (a),
UNIQUE INDEX idx_b(a)
)
81 changes: 81 additions & 0 deletions test/fixtures/dialects/mysql/create_table_constraint_unique.yml
@@ -0,0 +1,81 @@
# 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: 89f5acbd9b7f7b2a7f14cc5faa9666129d97246fbc7eb70c4d34e8c193eec5ab
file:
statement:
create_table_statement:
- keyword: CREATE
- keyword: TABLE
- table_reference:
identifier: a
- bracketed:
- start_bracket: (
- column_definition:
identifier: a
data_type:
data_type_identifier: INT
column_constraint_segment:
- keyword: NOT
- keyword: 'NULL'
- comma: ','
- table_constraint_segment:
keyword: UNIQUE
bracketed:
start_bracket: (
column_reference:
identifier: a
end_bracket: )
- comma: ','
- table_constraint_segment:
keyword: UNIQUE
object_reference:
identifier: idx_c
bracketed:
start_bracket: (
column_reference:
identifier: a
end_bracket: )
- comma: ','
- table_constraint_segment:
- keyword: UNIQUE
- keyword: KEY
- bracketed:
start_bracket: (
column_reference:
identifier: a
end_bracket: )
- comma: ','
- table_constraint_segment:
- keyword: UNIQUE
- keyword: KEY
- object_reference:
identifier: idx_a
- bracketed:
start_bracket: (
column_reference:
identifier: a
end_bracket: )
- comma: ','
- table_constraint_segment:
- keyword: UNIQUE
- keyword: INDEX
- bracketed:
start_bracket: (
column_reference:
identifier: a
end_bracket: )
- comma: ','
- table_constraint_segment:
- keyword: UNIQUE
- keyword: INDEX
- object_reference:
identifier: idx_b
- bracketed:
start_bracket: (
column_reference:
identifier: a
end_bracket: )
- end_bracket: )

0 comments on commit 31c1b1f

Please sign in to comment.