Skip to content

Commit 6343d98

Browse files
committed
Fix: Only apply the CTAS workaround in redshift if LIMIT is set to 0 (#1898)
* Fix: Only apply the CTAS workaround in redshift if LIMIT is set to 0 * Fix tests
1 parent cb8dcb6 commit 6343d98

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

sqlmesh/core/engine_adapter/redshift.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,11 @@ def _create_table_exp(
9090
**kwargs,
9191
)
9292

93-
if statement.expression:
93+
if (
94+
statement.expression
95+
and statement.expression.args.get("limit") is not None
96+
and statement.expression.args["limit"].expression.this == "0"
97+
):
9498
# redshift has a bug where CTAS statements have non determistic types. if a limit
9599
# is applied to a ctas statement, VARCHAR types default to 1 in some instances.
96100
# this checks the explain plain from redshift and tries to detect when these optimizer

tests/core/engine_adapter/test_redshift.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,15 @@ def test_create_table_from_query_exists_no_if_not_exists(
7878

7979
adapter.ctas(
8080
table_name="test_table",
81-
query_or_df=parse_one("SELECT a, b, x + 1 AS c, d AS d FROM table"),
81+
query_or_df=parse_one("SELECT a, b, x + 1 AS c, d AS d FROM table LIMIT 0"),
8282
exists=False,
8383
)
8484

8585
assert to_sql_calls(adapter) == [
86-
'EXPLAIN VERBOSE CREATE TABLE "test_table" AS SELECT "a", "b", "x" + 1 AS "c", "d" AS "d" FROM "table"',
86+
'EXPLAIN VERBOSE CREATE TABLE "test_table" AS SELECT "a", "b", "x" + 1 AS "c", "d" AS "d" FROM "table" LIMIT 0',
8787
'CREATE TABLE "test_table" AS SELECT CAST(NULL AS VARCHAR(MAX)) AS "a", CAST(NULL AS VARCHAR(60)) AS "b", CAST(NULL '
8888
'AS VARCHAR(MAX)) AS "c", CAST(NULL AS VARCHAR(MAX)) AS "d" FROM (SELECT "a", "b", "x" + 1 '
89-
'AS "c", "d" AS "d" FROM "table") AS "_subquery"',
89+
'AS "c", "d" AS "d" FROM "table" LIMIT 0) AS "_subquery"',
9090
]
9191

9292

@@ -170,7 +170,6 @@ def test_replace_query_with_query(adapter: t.Callable, mocker: MockerFixture):
170170
adapter.replace_query(table_name="test_table", query_or_df=parse_one("SELECT cola FROM table"))
171171

172172
assert to_sql_calls(adapter) == [
173-
'EXPLAIN VERBOSE CREATE TABLE "test_table" AS SELECT "cola" FROM "table"',
174173
'CREATE TABLE "test_table" AS SELECT "cola" FROM "table"',
175174
]
176175

@@ -222,7 +221,6 @@ def test_replace_query_with_df_table_not_exists(adapter: t.Callable, mocker: Moc
222221
)
223222

224223
assert to_sql_calls(adapter) == [
225-
'EXPLAIN VERBOSE CREATE TABLE "test_table" AS SELECT CAST("a" AS INTEGER) AS "a", CAST("b" AS INTEGER) AS "b" FROM (SELECT 1 AS "a", 4 AS "b" UNION ALL SELECT 2, 5 UNION ALL SELECT 3, 6) AS "t"',
226224
'CREATE TABLE "test_table" AS SELECT CAST("a" AS INTEGER) AS "a", CAST("b" AS INTEGER) AS "b" FROM (SELECT 1 AS "a", 4 AS "b" UNION ALL SELECT 2, 5 UNION ALL SELECT 3, 6) AS "t"',
227225
]
228226

0 commit comments

Comments
 (0)