Skip to content

Commit dc0cf94

Browse files
authored
fix(redshift): cast the children of aliases (#1809)
1 parent 2d8ae82 commit dc0cf94

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

sqlmesh/core/engine_adapter/redshift.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ def _create_table_exp(
107107
if resdom["restype"] == "1043" and resdom["restypmod"] == "- 1":
108108
select = statement.selects[i]
109109

110+
if isinstance(select, exp.Alias):
111+
select = select.this
112+
110113
if not isinstance(select, exp.Cast):
111114
select.replace(
112115
exp.cast(select.copy(), "VARCHAR(MAX)", dialect=self.dialect)

tests/core/engine_adapter/test_redshift.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,26 @@ def test_create_table_from_query_exists_no_if_not_exists(
6161
"restypmod": "- 1",
6262
},
6363
},
64+
{
65+
"name": "TARGETENTRY",
66+
"resdom": {
67+
"restype": "1043",
68+
"restypmod": "- 1",
69+
},
70+
},
6471
]
6572
},
6673
)
6774

6875
adapter.ctas(
6976
table_name="test_table",
70-
query_or_df=parse_one("SELECT a, b, CAST(c AS VARCHAR) FROM table"),
77+
query_or_df=parse_one("SELECT a, b, CAST(c AS VARCHAR), d AS d FROM table"),
7178
exists=False,
7279
)
7380

7481
assert to_sql_calls(adapter) == [
75-
'EXPLAIN VERBOSE CREATE TABLE "test_table" AS SELECT "a", "b", CAST("c" AS VARCHAR) FROM "table"',
76-
'CREATE TABLE "test_table" AS SELECT CAST("a" AS VARCHAR(MAX)), "b", CAST("c" AS VARCHAR) FROM "table"',
82+
'EXPLAIN VERBOSE CREATE TABLE "test_table" AS SELECT "a", "b", CAST("c" AS VARCHAR), "d" AS "d" FROM "table"',
83+
'CREATE TABLE "test_table" AS SELECT CAST("a" AS VARCHAR(MAX)), "b", CAST("c" AS VARCHAR), CAST("d" AS VARCHAR(MAX)) AS "d" FROM "table"',
7784
]
7885

7986

0 commit comments

Comments
 (0)