Skip to content

Commit

Permalink
Fix(redshift): generate proper syntax for column type alteration (#4698)
Browse files Browse the repository at this point in the history
  • Loading branch information
georgesittas authored Feb 3, 2025
1 parent fcd4543 commit dd1cdb0
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 1 deletion.
1 change: 1 addition & 0 deletions sqlglot/dialects/redshift.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ class Generator(Postgres.Generator):
SUPPORTS_CONVERT_TIMEZONE = True
EXCEPT_INTERSECT_SUPPORT_ALL_CLAUSE = False
SUPPORTS_MEDIAN = True
ALTER_SET_TYPE = "TYPE"

# Redshift doesn't have `WITH` as part of their with_properties so we remove it
WITH_PROPERTIES_PREFIX = " "
Expand Down
5 changes: 4 additions & 1 deletion sqlglot/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,9 @@ class Generator(metaclass=_Generator):
# The function name of the exp.ArraySize expression
ARRAY_SIZE_NAME: str = "ARRAY_LENGTH"

# The syntax to use when altering the type of a column
ALTER_SET_TYPE = "SET DATA TYPE"

# Whether exp.ArraySize should generate the dimension arg too (valid for Postgres & DuckDB)
# None -> Doesn't support it at all
# False (DuckDB) -> Has backwards-compatible support, but preferably generated without
Expand Down Expand Up @@ -3252,7 +3255,7 @@ def altercolumn_sql(self, expression: exp.AlterColumn) -> str:
collate = f" COLLATE {collate}" if collate else ""
using = self.sql(expression, "using")
using = f" USING {using}" if using else ""
return f"ALTER COLUMN {this} SET DATA TYPE {dtype}{collate}{using}"
return f"ALTER COLUMN {this} {self.ALTER_SET_TYPE} {dtype}{collate}{using}"

default = self.sql(expression, "default")
if default:
Expand Down
1 change: 1 addition & 0 deletions tests/dialects/test_redshift.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ def test_redshift(self):
)

def test_identity(self):
self.validate_identity("ALTER TABLE table_name ALTER COLUMN bla TYPE VARCHAR")
self.validate_identity("SELECT CAST(value AS FLOAT(8))")
self.validate_identity("1 div", "1 AS div")
self.validate_identity("LISTAGG(DISTINCT foo, ', ')")
Expand Down

0 comments on commit dd1cdb0

Please sign in to comment.