Skip to content

Commit

Permalink
feat(tsql): index on closes #3658
Browse files Browse the repository at this point in the history
  • Loading branch information
tobymao committed Jun 14, 2024
1 parent a06ee36 commit ff3dabc
Show file tree
Hide file tree
Showing 4 changed files with 14 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 @@ -2127,6 +2127,7 @@ class IndexParameters(Expression):
"partition_by": False,
"tablespace": False,
"where": False,
"on": False,
}


Expand Down
4 changes: 3 additions & 1 deletion sqlglot/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -1298,8 +1298,10 @@ def indexparameters_sql(self, expression: exp.IndexParameters) -> str:
with_storage = f" WITH ({with_storage})" if with_storage else ""
tablespace = self.sql(expression, "tablespace")
tablespace = f" USING INDEX TABLESPACE {tablespace}" if tablespace else ""
on = self.sql(expression, "on")
on = f" ON {on}" if on else ""

return f"{using}{columns}{include}{with_storage}{tablespace}{partition_by}{where}"
return f"{using}{columns}{include}{with_storage}{tablespace}{partition_by}{where}{on}"

def index_sql(self, expression: exp.Index) -> str:
unique = "UNIQUE " if expression.args.get("unique") else ""
Expand Down
3 changes: 3 additions & 0 deletions sqlglot/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3188,6 +3188,8 @@ def _parse_index_params(self) -> exp.IndexParameters:
)
where = self._parse_where()

on = self._parse_field() if self._match(TokenType.ON) else None

return self.expression(
exp.IndexParameters,
using=using,
Expand All @@ -3197,6 +3199,7 @@ def _parse_index_params(self) -> exp.IndexParameters:
where=where,
with_storage=with_storage,
tablespace=tablespace,
on=on,
)

def _parse_index(
Expand Down
7 changes: 7 additions & 0 deletions tests/dialects/test_tsql.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ class TestTSQL(Validator):
dialect = "tsql"

def test_tsql(self):
self.validate_identity(
"CREATE INDEX [x] ON [y]([z] ASC) WITH (allow_page_locks=on) ON X([y])"
)
self.validate_identity(
"CREATE INDEX [x] ON [y]([z] ASC) WITH (allow_page_locks=on) ON PRIMARY"
)

self.assertEqual(
annotate_types(self.validate_identity("SELECT 1 WHERE EXISTS(SELECT 1)")).sql("tsql"),
"SELECT 1 WHERE EXISTS(SELECT 1)",
Expand Down

0 comments on commit ff3dabc

Please sign in to comment.