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

fix(postgres): Fix UNIQUE consuming tokens #3775

Merged
merged 1 commit into from
Jul 18, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions sqlglot/dialects/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,9 @@ def _parse_date_part(self) -> exp.Expression:

return self.expression(exp.Extract, this=part, expression=value)

def _parse_unique_key(self) -> t.Optional[exp.Expression]:
return None

class Generator(generator.Generator):
SINGLE_STRING_INTERVAL = True
RENAME_TABLE_WITH_DB = False
Expand Down
2 changes: 1 addition & 1 deletion sqlglot/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1869,7 +1869,7 @@ class TitleColumnConstraint(ColumnConstraintKind):


class UniqueColumnConstraint(ColumnConstraintKind):
arg_types = {"this": False, "index_type": False, "on_conflict": False}
arg_types = {"this": False, "index_type": False, "on_conflict": False, "nulls": False}


class UppercaseColumnConstraint(ColumnConstraintKind):
Expand Down
3 changes: 2 additions & 1 deletion sqlglot/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -961,7 +961,8 @@ def uniquecolumnconstraint_sql(self, expression: exp.UniqueColumnConstraint) ->
index_type = f" USING {index_type}" if index_type else ""
on_conflict = self.sql(expression, "on_conflict")
on_conflict = f" {on_conflict}" if on_conflict else ""
return f"UNIQUE{this}{index_type}{on_conflict}"
nulls_sql = " NULLS NOT DISTINCT" if expression.args.get("nulls") else ""
return f"UNIQUE{nulls_sql}{this}{index_type}{on_conflict}"

def createable_sql(self, expression: exp.Create, locations: t.DefaultDict) -> str:
return self.sql(expression, "this")
Expand Down
6 changes: 5 additions & 1 deletion sqlglot/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -5179,11 +5179,15 @@ def _parse_unnamed_constraint(

return self.CONSTRAINT_PARSERS[constraint](self)

def _parse_unique_key(self) -> t.Optional[exp.Expression]:
return self._parse_id_var(any_token=False)

def _parse_unique(self) -> exp.UniqueColumnConstraint:
self._match_text_seq("KEY")
return self.expression(
exp.UniqueColumnConstraint,
this=self._parse_schema(self._parse_id_var(any_token=False)),
nulls=self._match_text_seq("NULLS", "NOT", "DISTINCT"),
this=self._parse_schema(self._parse_unique_key()),
index_type=self._match(TokenType.USING) and self._advance_any() and self._prev.text,
on_conflict=self._parse_on_conflict(),
)
Expand Down
4 changes: 4 additions & 0 deletions tests/dialects/test_postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,10 @@ def test_ddl(self):
},
)

self.validate_identity("CREATE TABLE tbl (col INT UNIQUE NULLS NOT DISTINCT DEFAULT 9.99)")
self.validate_identity("CREATE TABLE tbl (col UUID UNIQUE DEFAULT GEN_RANDOM_UUID())")
self.validate_identity("CREATE TABLE tbl (col UUID, UNIQUE NULLS NOT DISTINCT (col))")

with self.assertRaises(ParseError):
transpile("CREATE TABLE products (price DECIMAL CHECK price > 0)", read="postgres")
with self.assertRaises(ParseError):
Expand Down
Loading