Skip to content

Commit c93ef36

Browse files
committed
Fix: Fix columns-to-types when inserting by time partition if the type of the time column is unknown (#3168)
1 parent a571d52 commit c93ef36

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

sqlmesh/core/engine_adapter/base.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1198,7 +1198,8 @@ def insert_overwrite_by_time_partition(
11981198
source_queries, columns_to_types = self._get_source_queries_and_columns_to_types(
11991199
query_or_df, columns_to_types, target_table=table_name
12001200
)
1201-
columns_to_types = columns_to_types or self.columns(table_name)
1201+
if not columns_to_types or not columns_to_types_all_known(columns_to_types):
1202+
columns_to_types = self.columns(table_name)
12021203
low, high = [time_formatter(dt, columns_to_types) for dt in make_inclusive(start, end)]
12031204
if isinstance(time_column, TimeColumn):
12041205
time_column = time_column.column

tests/core/engine_adapter/test_base.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,34 @@ def test_insert_overwrite_by_time_partition(make_mocked_engine_adapter: t.Callab
184184
]
185185

186186

187+
def test_insert_overwrite_by_time_partition_missing_time_column_type(
188+
make_mocked_engine_adapter: t.Callable, mocker: MockerFixture
189+
):
190+
adapter = make_mocked_engine_adapter(EngineAdapter)
191+
192+
columns_mock = mocker.patch.object(adapter, "columns")
193+
columns_mock.return_value = {"a": exp.DataType.build("INT"), "b": exp.DataType.build("STRING")}
194+
195+
adapter.insert_overwrite_by_time_partition(
196+
"test_table",
197+
parse_one("SELECT a, b FROM tbl"),
198+
start="2022-01-01",
199+
end="2022-01-02",
200+
time_column="b",
201+
time_formatter=lambda x, _: exp.Literal.string(to_ds(x)),
202+
columns_to_types={"a": exp.DataType.build("INT"), "b": exp.DataType.build("UNKNOWN")},
203+
)
204+
205+
columns_mock.assert_called_once_with("test_table")
206+
adapter.cursor.begin.assert_called_once()
207+
adapter.cursor.commit.assert_called_once()
208+
209+
assert to_sql_calls(adapter) == [
210+
"""DELETE FROM "test_table" WHERE "b" BETWEEN '2022-01-01' AND '2022-01-02'""",
211+
"""INSERT INTO "test_table" ("a", "b") SELECT "a", "b" FROM (SELECT "a", "b" FROM "tbl") AS "_subquery" WHERE "b" BETWEEN '2022-01-01' AND '2022-01-02'""",
212+
]
213+
214+
187215
def test_insert_overwrite_by_time_partition_supports_insert_overwrite(
188216
make_mocked_engine_adapter: t.Callable,
189217
):

0 commit comments

Comments
 (0)