Skip to content

Commit dd1cdb0

Browse files
authored
Fix(redshift): generate proper syntax for column type alteration (#4698)
1 parent fcd4543 commit dd1cdb0

File tree

3 files changed

+6
-1
lines changed

3 files changed

+6
-1
lines changed

sqlglot/dialects/redshift.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ class Generator(Postgres.Generator):
158158
SUPPORTS_CONVERT_TIMEZONE = True
159159
EXCEPT_INTERSECT_SUPPORT_ALL_CLAUSE = False
160160
SUPPORTS_MEDIAN = True
161+
ALTER_SET_TYPE = "TYPE"
161162

162163
# Redshift doesn't have `WITH` as part of their with_properties so we remove it
163164
WITH_PROPERTIES_PREFIX = " "

sqlglot/generator.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,9 @@ class Generator(metaclass=_Generator):
451451
# The function name of the exp.ArraySize expression
452452
ARRAY_SIZE_NAME: str = "ARRAY_LENGTH"
453453

454+
# The syntax to use when altering the type of a column
455+
ALTER_SET_TYPE = "SET DATA TYPE"
456+
454457
# Whether exp.ArraySize should generate the dimension arg too (valid for Postgres & DuckDB)
455458
# None -> Doesn't support it at all
456459
# False (DuckDB) -> Has backwards-compatible support, but preferably generated without
@@ -3252,7 +3255,7 @@ def altercolumn_sql(self, expression: exp.AlterColumn) -> str:
32523255
collate = f" COLLATE {collate}" if collate else ""
32533256
using = self.sql(expression, "using")
32543257
using = f" USING {using}" if using else ""
3255-
return f"ALTER COLUMN {this} SET DATA TYPE {dtype}{collate}{using}"
3258+
return f"ALTER COLUMN {this} {self.ALTER_SET_TYPE} {dtype}{collate}{using}"
32563259

32573260
default = self.sql(expression, "default")
32583261
if default:

tests/dialects/test_redshift.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ def test_redshift(self):
320320
)
321321

322322
def test_identity(self):
323+
self.validate_identity("ALTER TABLE table_name ALTER COLUMN bla TYPE VARCHAR")
323324
self.validate_identity("SELECT CAST(value AS FLOAT(8))")
324325
self.validate_identity("1 div", "1 AS div")
325326
self.validate_identity("LISTAGG(DISTINCT foo, ', ')")

0 commit comments

Comments
 (0)