Skip to content

Commit

Permalink
Fix: index using closes #1751
Browse files Browse the repository at this point in the history
  • Loading branch information
tobymao committed Jun 9, 2023
1 parent 661486b commit 68b9128
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 1 deletion.
1 change: 1 addition & 0 deletions sqlglot/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1500,6 +1500,7 @@ class Index(Expression):
arg_types = {
"this": False,
"table": False,
"using": False,
"where": False,
"columns": False,
"unique": False,
Expand Down
5 changes: 4 additions & 1 deletion sqlglot/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -885,11 +885,14 @@ def index_sql(self, expression: exp.Index) -> str:
name = f"{expression.name} " if expression.name else ""
table = self.sql(expression, "table")
table = f"{self.INDEX_ON} {table} " if table else ""
using = self.sql(expression, "using")
using = f"USING {using} " if using else ""
index = "INDEX " if not table else ""
columns = self.expressions(expression, key="columns", flat=True)
columns = f"({columns})" if columns else ""
partition_by = self.expressions(expression, key="partition_by", flat=True)
partition_by = f" PARTITION BY {partition_by}" if partition_by else ""
return f"{unique}{primary}{amp}{index}{name}{table}({columns}){partition_by}"
return f"{unique}{primary}{amp}{index}{name}{table}{using}{columns}{partition_by}"

def identifier_sql(self, expression: exp.Identifier) -> str:
text = expression.name
Expand Down
3 changes: 3 additions & 0 deletions sqlglot/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2248,6 +2248,8 @@ def _parse_index(
index = self._parse_id_var()
table = None

using = self._parse_field() if self._match(TokenType.USING) else None

if self._match(TokenType.L_PAREN, advance=False):
columns = self._parse_wrapped_csv(self._parse_ordered)
else:
Expand All @@ -2257,6 +2259,7 @@ def _parse_index(
exp.Index,
this=index,
table=table,
using=using,
columns=columns,
unique=unique,
primary=primary,
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/identity.sql
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,7 @@ CREATE FUNCTION a.b.c()
CREATE INDEX abc ON t (a)
CREATE INDEX abc ON t (a, b, b)
CREATE INDEX abc ON t (a NULLS LAST)
CREATE INDEX pointloc ON points USING GIST(BOX(location, location))
CREATE UNIQUE INDEX abc ON t (a, b, b)
CREATE UNIQUE INDEX IF NOT EXISTS my_idx ON tbl (a, b)
CREATE SCHEMA x
Expand Down

0 comments on commit 68b9128

Please sign in to comment.