|
18 | 18 | from sqlmesh.utils import optional_import |
19 | 19 | from tests.core.engine_adapter import to_sql_calls |
20 | 20 | from sqlmesh.core.model.kind import ViewKind |
| 21 | +from sqlmesh.core.snapshot import DeployabilityIndex |
| 22 | +from sqlmesh.core.snapshot.evaluator import EngineManagedStrategy |
21 | 23 |
|
22 | 24 | pytestmark = [pytest.mark.engine, pytest.mark.snowflake] |
23 | 25 |
|
@@ -609,6 +611,85 @@ def test_ctas_skips_dynamic_table_properties(make_mocked_engine_adapter: t.Calla |
609 | 611 | ] |
610 | 612 |
|
611 | 613 |
|
| 614 | +@pytest.mark.parametrize( |
| 615 | + "operation", ["create_annotated", "create_ctas", "insert", "create_managed"] |
| 616 | +) |
| 617 | +def test_managed_preview_dynamic_table_properties( |
| 618 | + make_mocked_engine_adapter: t.Callable, mocker: MockerFixture, operation: str |
| 619 | +): |
| 620 | + adapter = make_mocked_engine_adapter(SnowflakeEngineAdapter) |
| 621 | + model = load_sql_based_model( |
| 622 | + d.parse(""" |
| 623 | + MODEL ( |
| 624 | + name test_schema.test_model, |
| 625 | + dialect snowflake, |
| 626 | + kind MANAGED, |
| 627 | + physical_properties ( |
| 628 | + warehouse = 'SMALL', |
| 629 | + target_lag = '10 minutes', |
| 630 | + refresh_mode = 'AUTO', |
| 631 | + initialize = 'ON_CREATE', |
| 632 | + initialization_warehouse = 'INITIAL_WH', |
| 633 | + data_retention_time_in_days = 1, |
| 634 | + max_data_extension_time_in_days = 2 |
| 635 | + ) |
| 636 | + ); |
| 637 | + SELECT a FROM source_table; |
| 638 | + """) |
| 639 | + ) |
| 640 | + properties = model.physical_properties |
| 641 | + original_properties = {key: value.copy() for key, value in properties.items()} |
| 642 | + strategy = EngineManagedStrategy(adapter) |
| 643 | + mocker.patch.object(adapter, "columns", return_value={"A": exp.DataType.build("INT")}) |
| 644 | + if operation == "create_annotated": |
| 645 | + model = model.copy(update={"columns_to_types_": {"A": exp.DataType.build("INT")}}) |
| 646 | + if operation == "insert": |
| 647 | + strategy.insert( |
| 648 | + "test_table", |
| 649 | + model.render_query_or_raise(), |
| 650 | + model, |
| 651 | + True, |
| 652 | + {}, |
| 653 | + deployability_index=DeployabilityIndex.none_deployable(), |
| 654 | + snapshot=mocker.Mock(), |
| 655 | + physical_properties=properties, |
| 656 | + ) |
| 657 | + else: |
| 658 | + strategy.create( |
| 659 | + "test_table", |
| 660 | + model, |
| 661 | + operation == "create_managed", |
| 662 | + {}, |
| 663 | + skip_grants=True, |
| 664 | + is_snapshot_deployable=operation == "create_managed", |
| 665 | + physical_properties=properties, |
| 666 | + ) |
| 667 | + shared_properties = "DATA_RETENTION_TIME_IN_DAYS=1 MAX_DATA_EXTENSION_TIME_IN_DAYS=2" |
| 668 | + query = 'SELECT "A" AS "A" FROM "SOURCE_TABLE" AS "SOURCE_TABLE"' |
| 669 | + if operation == "create_annotated": |
| 670 | + expected_sql = [ |
| 671 | + f'CREATE TABLE IF NOT EXISTS "test_table" ("A" INT) {shared_properties}', |
| 672 | + f"{query} WHERE FALSE LIMIT 0", |
| 673 | + ] |
| 674 | + elif operation == "create_ctas": |
| 675 | + expected_sql = [ |
| 676 | + f'CREATE TABLE IF NOT EXISTS "test_table" {shared_properties} AS {query} WHERE FALSE LIMIT 0' |
| 677 | + ] |
| 678 | + elif operation == "insert": |
| 679 | + expected_sql = [ |
| 680 | + f'CREATE OR REPLACE TABLE "test_table" {shared_properties} AS SELECT CAST("A" AS INT) AS "A" FROM ({query}) AS "_subquery"' |
| 681 | + ] |
| 682 | + else: |
| 683 | + expected_sql = [ |
| 684 | + 'CREATE OR REPLACE DYNAMIC TABLE "test_table" ' |
| 685 | + "WAREHOUSE='SMALL' TARGET_LAG='10 minutes' REFRESH_MODE='AUTO' " |
| 686 | + "INITIALIZE='ON_CREATE' INITIALIZATION_WAREHOUSE='INITIAL_WH' " |
| 687 | + f"{shared_properties} AS {query}" |
| 688 | + ] |
| 689 | + assert to_sql_calls(adapter) == expected_sql |
| 690 | + assert properties == original_properties |
| 691 | + |
| 692 | + |
612 | 693 | def test_set_current_catalog(make_mocked_engine_adapter: t.Callable): |
613 | 694 | adapter = make_mocked_engine_adapter(SnowflakeEngineAdapter) |
614 | 695 | adapter._default_catalog = "foo" |
|
0 commit comments