|
2 | 2 |
|
3 | 3 | import typing as t |
4 | 4 |
|
| 5 | +import pandas as pd # noqa: TID253 |
5 | 6 | import pytest |
6 | 7 | from pytest_mock import MockerFixture |
7 | 8 | from sqlglot import exp, parse_one |
@@ -143,3 +144,143 @@ def test_alter_table_direct_alteration(adapter: FabricEngineAdapter, mocker: Moc |
143 | 144 | ] |
144 | 145 |
|
145 | 146 | assert to_sql_calls(adapter) == expected_calls |
| 147 | +def test_merge_pandas( |
| 148 | + make_mocked_engine_adapter: t.Callable, mocker: MockerFixture, make_temp_table_name: t.Callable |
| 149 | +): |
| 150 | + mocker.patch( |
| 151 | + "sqlmesh.core.engine_adapter.fabric.FabricEngineAdapter.table_exists", |
| 152 | + return_value=False, |
| 153 | + ) |
| 154 | + |
| 155 | + adapter = make_mocked_engine_adapter(FabricEngineAdapter) |
| 156 | + |
| 157 | + temp_table_mock = mocker.patch("sqlmesh.core.engine_adapter.EngineAdapter._get_temp_table") |
| 158 | + table_name = "target" |
| 159 | + temp_table_id = "abcdefgh" |
| 160 | + temp_table_mock.return_value = make_temp_table_name(table_name, temp_table_id) |
| 161 | + |
| 162 | + df = pd.DataFrame({"id": [1, 2, 3], "ts": [1, 2, 3], "val": [4, 5, 6]}) |
| 163 | + |
| 164 | + # 1 key |
| 165 | + adapter.merge( |
| 166 | + target_table=table_name, |
| 167 | + source_table=df, |
| 168 | + target_columns_to_types={ |
| 169 | + "id": exp.DataType.build("int"), |
| 170 | + "ts": exp.DataType.build("TIMESTAMP"), |
| 171 | + "val": exp.DataType.build("int"), |
| 172 | + }, |
| 173 | + unique_key=[exp.to_identifier("id")], |
| 174 | + ) |
| 175 | + adapter._connection_pool.get().bulk_copy.assert_called_with( |
| 176 | + f"__temp_target_{temp_table_id}", [(1, 1, 4), (2, 2, 5), (3, 3, 6)] |
| 177 | + ) |
| 178 | + |
| 179 | + assert to_sql_calls(adapter) == [ |
| 180 | + f"""IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '__temp_target_{temp_table_id}') EXEC('CREATE TABLE [__temp_target_{temp_table_id}] ([id] INT, [ts] DATETIME2(6), [val] INT)');""", |
| 181 | + f"MERGE INTO [target] AS [__MERGE_TARGET__] USING (SELECT CAST([id] AS INT) AS [id], CAST([ts] AS DATETIME2(6)) AS [ts], CAST([val] AS INT) AS [val] FROM [__temp_target_{temp_table_id}]) AS [__MERGE_SOURCE__] ON [__MERGE_TARGET__].[id] = [__MERGE_SOURCE__].[id] WHEN MATCHED THEN UPDATE SET [__MERGE_TARGET__].[ts] = [__MERGE_SOURCE__].[ts], [__MERGE_TARGET__].[val] = [__MERGE_SOURCE__].[val] WHEN NOT MATCHED THEN INSERT ([id], [ts], [val]) VALUES ([__MERGE_SOURCE__].[id], [__MERGE_SOURCE__].[ts], [__MERGE_SOURCE__].[val]);", |
| 182 | + f"DROP TABLE IF EXISTS [__temp_target_{temp_table_id}];", |
| 183 | + ] |
| 184 | + |
| 185 | + # 2 keys |
| 186 | + adapter.cursor.reset_mock() |
| 187 | + adapter._connection_pool.get().reset_mock() |
| 188 | + temp_table_mock.return_value = make_temp_table_name(table_name, temp_table_id) |
| 189 | + adapter.merge( |
| 190 | + target_table=table_name, |
| 191 | + source_table=df, |
| 192 | + target_columns_to_types={ |
| 193 | + "id": exp.DataType.build("int"), |
| 194 | + "ts": exp.DataType.build("TIMESTAMP"), |
| 195 | + "val": exp.DataType.build("int"), |
| 196 | + }, |
| 197 | + unique_key=[exp.to_identifier("id"), exp.to_column("ts")], |
| 198 | + ) |
| 199 | + adapter._connection_pool.get().bulk_copy.assert_called_with( |
| 200 | + f"__temp_target_{temp_table_id}", [(1, 1, 4), (2, 2, 5), (3, 3, 6)] |
| 201 | + ) |
| 202 | + |
| 203 | + assert to_sql_calls(adapter) == [ |
| 204 | + f"""IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '__temp_target_{temp_table_id}') EXEC('CREATE TABLE [__temp_target_{temp_table_id}] ([id] INT, [ts] DATETIME2(6), [val] INT)');""", |
| 205 | + f"MERGE INTO [target] AS [__MERGE_TARGET__] USING (SELECT CAST([id] AS INT) AS [id], CAST([ts] AS DATETIME2(6)) AS [ts], CAST([val] AS INT) AS [val] FROM [__temp_target_{temp_table_id}]) AS [__MERGE_SOURCE__] ON [__MERGE_TARGET__].[id] = [__MERGE_SOURCE__].[id] AND [__MERGE_TARGET__].[ts] = [__MERGE_SOURCE__].[ts] WHEN MATCHED THEN UPDATE SET [__MERGE_TARGET__].[val] = [__MERGE_SOURCE__].[val] WHEN NOT MATCHED THEN INSERT ([id], [ts], [val]) VALUES ([__MERGE_SOURCE__].[id], [__MERGE_SOURCE__].[ts], [__MERGE_SOURCE__].[val]);", |
| 206 | + f"DROP TABLE IF EXISTS [__temp_target_{temp_table_id}];", |
| 207 | + ] |
| 208 | + |
| 209 | + |
| 210 | +def test_merge_exists( |
| 211 | + make_mocked_engine_adapter: t.Callable, mocker: MockerFixture, make_temp_table_name: t.Callable |
| 212 | +): |
| 213 | + mocker.patch( |
| 214 | + "sqlmesh.core.engine_adapter.fabric.FabricEngineAdapter.table_exists", |
| 215 | + return_value=False, |
| 216 | + ) |
| 217 | + |
| 218 | + adapter = make_mocked_engine_adapter(FabricEngineAdapter) |
| 219 | + |
| 220 | + temp_table_mock = mocker.patch("sqlmesh.core.engine_adapter.EngineAdapter._get_temp_table") |
| 221 | + table_name = "target" |
| 222 | + temp_table_id = "abcdefgh" |
| 223 | + temp_table_mock.return_value = make_temp_table_name(table_name, temp_table_id) |
| 224 | + |
| 225 | + df = pd.DataFrame({"id": [1, 2, 3], "ts": [1, 2, 3], "val": [4, 5, 6]}) |
| 226 | + |
| 227 | + # regular implementation |
| 228 | + adapter.merge( |
| 229 | + target_table=table_name, |
| 230 | + source_table=df, |
| 231 | + target_columns_to_types={ |
| 232 | + "id": exp.DataType.build("int"), |
| 233 | + "ts": exp.DataType.build("TIMESTAMP"), |
| 234 | + "val": exp.DataType.build("int"), |
| 235 | + }, |
| 236 | + unique_key=[exp.to_identifier("id")], |
| 237 | + ) |
| 238 | + |
| 239 | + assert to_sql_calls(adapter) == [ |
| 240 | + f"""IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '__temp_target_{temp_table_id}') EXEC('CREATE TABLE [__temp_target_{temp_table_id}] ([id] INT, [ts] DATETIME2(6), [val] INT)');""", |
| 241 | + f"MERGE INTO [target] AS [__MERGE_TARGET__] USING (SELECT CAST([id] AS INT) AS [id], CAST([ts] AS DATETIME2(6)) AS [ts], CAST([val] AS INT) AS [val] FROM [__temp_target_{temp_table_id}]) AS [__MERGE_SOURCE__] ON [__MERGE_TARGET__].[id] = [__MERGE_SOURCE__].[id] WHEN MATCHED THEN UPDATE SET [__MERGE_TARGET__].[ts] = [__MERGE_SOURCE__].[ts], [__MERGE_TARGET__].[val] = [__MERGE_SOURCE__].[val] WHEN NOT MATCHED THEN INSERT ([id], [ts], [val]) VALUES ([__MERGE_SOURCE__].[id], [__MERGE_SOURCE__].[ts], [__MERGE_SOURCE__].[val]);", |
| 242 | + f"DROP TABLE IF EXISTS [__temp_target_{temp_table_id}];", |
| 243 | + ] |
| 244 | + |
| 245 | + # merge exists implementation |
| 246 | + adapter.cursor.reset_mock() |
| 247 | + adapter._connection_pool.get().reset_mock() |
| 248 | + temp_table_mock.return_value = make_temp_table_name(table_name, temp_table_id) |
| 249 | + adapter.merge( |
| 250 | + target_table=table_name, |
| 251 | + source_table=df, |
| 252 | + target_columns_to_types={ |
| 253 | + "id": exp.DataType.build("int"), |
| 254 | + "ts": exp.DataType.build("TIMESTAMP"), |
| 255 | + "val": exp.DataType.build("int"), |
| 256 | + }, |
| 257 | + unique_key=[exp.to_identifier("id")], |
| 258 | + physical_properties={"mssql_merge_exists": True}, |
| 259 | + ) |
| 260 | + |
| 261 | + assert to_sql_calls(adapter) == [ |
| 262 | + f"""IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '__temp_target_{temp_table_id}') EXEC('CREATE TABLE [__temp_target_{temp_table_id}] ([id] INT, [ts] DATETIME2(6), [val] INT)');""", |
| 263 | + f"MERGE INTO [target] AS [__MERGE_TARGET__] USING (SELECT CAST([id] AS INT) AS [id], CAST([ts] AS DATETIME2(6)) AS [ts], CAST([val] AS INT) AS [val] FROM [__temp_target_{temp_table_id}]) AS [__MERGE_SOURCE__] ON [__MERGE_TARGET__].[id] = [__MERGE_SOURCE__].[id] WHEN MATCHED AND EXISTS(SELECT [__MERGE_TARGET__].[ts], [__MERGE_TARGET__].[val] EXCEPT SELECT [__MERGE_SOURCE__].[ts], [__MERGE_SOURCE__].[val]) THEN UPDATE SET [__MERGE_TARGET__].[ts] = [__MERGE_SOURCE__].[ts], [__MERGE_TARGET__].[val] = [__MERGE_SOURCE__].[val] WHEN NOT MATCHED THEN INSERT ([id], [ts], [val]) VALUES ([__MERGE_SOURCE__].[id], [__MERGE_SOURCE__].[ts], [__MERGE_SOURCE__].[val]);", |
| 264 | + f"DROP TABLE IF EXISTS [__temp_target_{temp_table_id}];", |
| 265 | + ] |
| 266 | + |
| 267 | + # merge exists and all model columns are keys |
| 268 | + adapter.cursor.reset_mock() |
| 269 | + adapter._connection_pool.get().reset_mock() |
| 270 | + temp_table_mock.return_value = make_temp_table_name(table_name, temp_table_id) |
| 271 | + adapter.merge( |
| 272 | + target_table=table_name, |
| 273 | + source_table=df, |
| 274 | + target_columns_to_types={ |
| 275 | + "id": exp.DataType.build("int"), |
| 276 | + "ts": exp.DataType.build("TIMESTAMP"), |
| 277 | + }, |
| 278 | + unique_key=[exp.to_identifier("id"), exp.to_column("ts")], |
| 279 | + physical_properties={"mssql_merge_exists": True}, |
| 280 | + ) |
| 281 | + |
| 282 | + assert to_sql_calls(adapter) == [ |
| 283 | + f"""IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '__temp_target_{temp_table_id}') EXEC('CREATE TABLE [__temp_target_{temp_table_id}] ([id] INT, [ts] DATETIME2(6))');""", |
| 284 | + f"MERGE INTO [target] AS [__MERGE_TARGET__] USING (SELECT CAST([id] AS INT) AS [id], CAST([ts] AS DATETIME2(6)) AS [ts] FROM [__temp_target_{temp_table_id}]) AS [__MERGE_SOURCE__] ON [__MERGE_TARGET__].[id] = [__MERGE_SOURCE__].[id] AND [__MERGE_TARGET__].[ts] = [__MERGE_SOURCE__].[ts] WHEN NOT MATCHED THEN INSERT ([id], [ts]) VALUES ([__MERGE_SOURCE__].[id], [__MERGE_SOURCE__].[ts]);", |
| 285 | + f"DROP TABLE IF EXISTS [__temp_target_{temp_table_id}];", |
| 286 | + ] |
0 commit comments