Skip to content

Commit

Permalink
Fix: teradata partition order (#1696)
Browse files Browse the repository at this point in the history
  • Loading branch information
tobymao authored May 26, 2023
1 parent 810522b commit 5f45e18
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 9 deletions.
2 changes: 1 addition & 1 deletion sqlglot/dialects/teradata.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class Generator(generator.Generator):
PROPERTIES_LOCATION = {
**generator.Generator.PROPERTIES_LOCATION,
exp.OnCommitProperty: exp.Properties.Location.POST_INDEX,
exp.PartitionedByProperty: exp.Properties.Location.POST_INDEX,
exp.PartitionedByProperty: exp.Properties.Location.POST_EXPRESSION,
exp.StabilityProperty: exp.Properties.Location.POST_CREATE,
}

Expand Down
1 change: 1 addition & 0 deletions sqlglot/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1510,6 +1510,7 @@ class Index(Expression):
"unique": False,
"primary": False,
"amp": False, # teradata
"partition_by": False, # teradata
}


Expand Down
4 changes: 3 additions & 1 deletion sqlglot/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -878,7 +878,9 @@ def index_sql(self, expression: exp.Index) -> str:
table = f"{self.INDEX_ON} {table} " if table else ""
index = "INDEX " if not table else ""
columns = self.expressions(expression, key="columns", flat=True)
return f"{unique}{primary}{amp}{index}{name}{table}({columns})"
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}"

def identifier_sql(self, expression: exp.Identifier) -> str:
text = expression.name
Expand Down
1 change: 1 addition & 0 deletions sqlglot/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2228,6 +2228,7 @@ def _parse_index(
unique=unique,
primary=primary,
amp=amp,
partition_by=self._parse_partition_by(),
)

def _parse_table_part(self, schema: bool = False) -> t.Optional[exp.Expression]:
Expand Down
10 changes: 3 additions & 7 deletions tests/dialects/test_teradata.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,8 @@ def test_update(self):
)

def test_create(self):
self.validate_all(
"CREATE TABLE x (y INT) PRIMARY INDEX (y) PARTITION BY y INDEX (y)",
write={
"teradata": "CREATE TABLE x (y INT) PRIMARY INDEX (y) INDEX (y) PARTITION BY y",
},
)
self.validate_identity("CREATE TABLE x (y INT) PRIMARY INDEX (y) PARTITION BY y INDEX (y)")
self.validate_identity("CREATE TABLE x (y INT) PARTITION BY y INDEX (y)")
self.validate_identity(
"CREATE MULTISET VOLATILE TABLE my_table (id INT) PRIMARY INDEX (id) ON COMMIT PRESERVE ROWS"
)
Expand All @@ -42,7 +38,7 @@ def test_create(self):
"CREATE TABLE a (b INT) PARTITION BY RANGE_N(b BETWEEN 0, 1 AND 2 EACH 1)"
)
self.validate_identity(
"CREATE TABLE a (b INT) INDEX (a) PARTITION BY RANGE_N(b BETWEEN *, 1 AND * EACH b)"
"CREATE TABLE a (b INT) PARTITION BY RANGE_N(b BETWEEN *, 1 AND * EACH b) INDEX (a)"
)

self.validate_all(
Expand Down

0 comments on commit 5f45e18

Please sign in to comment.