Skip to content

Commit 732548f

Browse files
authored
feat(postgresql): add support for table creation DDL that contains a primary key alongside the INCLUDE keyword (#5425)
* feat(postgresql): add support for table creation DDL that contains a primary key alongside the INCLUDE keyword * feat(postgresql): modify the code according to the PR review * feat(postgresql): annotate expression in primarykey_sql to be exp.PrimaryKey
1 parent 6a465be commit 732548f

File tree

4 files changed

+15
-4
lines changed

4 files changed

+15
-4
lines changed

sqlglot/expressions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2218,7 +2218,7 @@ class ColumnPrefix(Expression):
22182218

22192219

22202220
class PrimaryKey(Expression):
2221-
arg_types = {"expressions": True, "options": False}
2221+
arg_types = {"expressions": True, "options": False, "include": False}
22222222

22232223

22242224
# https://www.postgresql.org/docs/9.1/sql-selectinto.html

sqlglot/generator.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2995,11 +2995,13 @@ def foreignkey_sql(self, expression: exp.ForeignKey) -> str:
29952995
options = f" {options}" if options else ""
29962996
return f"FOREIGN KEY{expressions}{reference}{delete}{update}{options}"
29972997

2998-
def primarykey_sql(self, expression: exp.ForeignKey) -> str:
2998+
def primarykey_sql(self, expression: exp.PrimaryKey) -> str:
29992999
expressions = self.expressions(expression, flat=True)
3000+
include = self.expressions(expression, key="include", flat=True)
3001+
include = f" INCLUDE ({include})" if include else ""
30003002
options = self.expressions(expression, key="options", flat=True, sep=" ")
30013003
options = f" {options}" if options else ""
3002-
return f"PRIMARY KEY ({expressions}){options}"
3004+
return f"PRIMARY KEY ({expressions}){include}{options}"
30033005

30043006
def if_sql(self, expression: exp.If) -> str:
30053007
return self.case_sql(exp.Case(ifs=[expression], default=expression.args.get("false")))

sqlglot/parser.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6297,8 +6297,16 @@ def _parse_primary_key(
62976297
expressions = self._parse_wrapped_csv(
62986298
self._parse_primary_key_part, optional=wrapped_optional
62996299
)
6300+
6301+
index_params = None
6302+
if self._match_text_seq("INCLUDE", advance=False):
6303+
index_params = self._parse_index_params()
6304+
include = index_params.args.get("include") if index_params else None
6305+
63006306
options = self._parse_key_constraint_options()
6301-
return self.expression(exp.PrimaryKey, expressions=expressions, options=options)
6307+
return self.expression(
6308+
exp.PrimaryKey, expressions=expressions, include=include, options=options
6309+
)
63026310

63036311
def _parse_bracket_key_value(self, is_map: bool = False) -> t.Optional[exp.Expression]:
63046312
return self._parse_slice(self._parse_alias(self._parse_assignment(), explicit=True))

tests/dialects/test_postgres.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -987,6 +987,7 @@ def test_ddl(self):
987987
self.validate_identity(
988988
"CREATE TABLE t (vid INT NOT NULL, CONSTRAINT ht_vid_nid_fid_idx EXCLUDE (INT4RANGE(vid, nid) WITH &&, INT4RANGE(fid, fid, '[]') WITH &&))"
989989
)
990+
self.validate_identity("CREATE TABLE t (i INT, a TEXT, PRIMARY KEY (i) INCLUDE (a))")
990991
self.validate_identity(
991992
"CREATE TABLE t (i INT, PRIMARY KEY (i), EXCLUDE USING gist(col varchar_pattern_ops DESC NULLS LAST WITH &&) WITH (sp1=1, sp2=2))"
992993
)

0 commit comments

Comments
 (0)