Skip to content

Commit 06a426b

Browse files
committed
Fix: only drop LIMIT 0 in VarcharSizeWorkaroundMixin table creation
1 parent 3a57ea2 commit 06a426b

2 files changed

Lines changed: 7 additions & 3 deletions

File tree

sqlmesh/core/engine_adapter/mixins.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,11 +297,15 @@ def _build_create_table_exp(
297297
and statement.expression.args["limit"].expression.this == "0"
298298
):
299299
assert not isinstance(table_name_or_schema, exp.Schema)
300+
300301
# redshift and mssql have a bug where CTAS statements have non determistic types. if a limit
301302
# is applied to a ctas statement, VARCHAR types default to 1 in some instances.
302303
select_statement = statement.expression.copy()
303304
for select_or_union in select_statement.find_all(exp.Select, exp.SetOperation):
304-
select_or_union.set("limit", None)
305+
limit = select_or_union.args.get("limit")
306+
if limit is not None and limit.expression.this == "0":
307+
limit.pop()
308+
305309
select_or_union.set("where", None)
306310

307311
temp_view_name = self._get_temp_table("ctas")

tests/core/engine_adapter/test_mssql.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -726,13 +726,13 @@ def test_create_table_from_query(make_mocked_engine_adapter: t.Callable, mocker:
726726
adapter.ctas(
727727
table_name="test_schema.test_table",
728728
query_or_df=parse_one(
729-
"SELECT a, b, x + 1 AS c, d AS d, e FROM (SELECT * FROM table WHERE FALSE LIMIT 0) WHERE d > 0 AND FALSE LIMIT 0"
729+
"SELECT a, b, x + 1 AS c, d AS d, e FROM (SELECT * FROM (SELECT * FROM table LIMIT 1) WHERE FALSE LIMIT 0) WHERE d > 0 AND FALSE LIMIT 0"
730730
),
731731
exists=False,
732732
)
733733

734734
assert to_sql_calls(adapter) == [
735-
"CREATE VIEW [__temp_ctas_test_random_id] AS SELECT [a], [b], [x] + 1 AS [c], [d] AS [d], [e] FROM (SELECT * FROM [table]);",
735+
"CREATE VIEW [__temp_ctas_test_random_id] AS SELECT [a], [b], [x] + 1 AS [c], [d] AS [d], [e] FROM (SELECT * FROM (SELECT TOP 1 * FROM [table]));",
736736
"DROP VIEW IF EXISTS [__temp_ctas_test_random_id];",
737737
"CREATE TABLE [test_schema].[test_table] ([a] VARCHAR(MAX), [b] VARCHAR(60), [c] VARCHAR(MAX), [d] VARCHAR(MAX), [e] DATETIME2);",
738738
]

0 commit comments

Comments
 (0)