Skip to content

Commit f5f20de

Browse files
committed
fix(snowflake): omit initialization warehouse from preview tables
Signed-off-by: mday-io <mdaytn@gmail.com>
1 parent 8d0b4de commit f5f20de

2 files changed

Lines changed: 88 additions & 1 deletion

File tree

sqlmesh/core/engine_adapter/snowflake.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,13 @@ def _build_table_properties_exp(
343343
# if we are creating a non-dynamic table; remove any properties that are only valid for dynamic tables
344344
# this is necessary because we create "normal" tables from the same managed model definition for dev previews and the "normal" tables dont support these parameters
345345
if "DYNAMIC" not in (table_kind or "").upper():
346-
for prop in {"WAREHOUSE", "TARGET_LAG", "REFRESH_MODE", "INITIALIZE"}:
346+
for prop in {
347+
"WAREHOUSE",
348+
"TARGET_LAG",
349+
"REFRESH_MODE",
350+
"INITIALIZE",
351+
"INITIALIZATION_WAREHOUSE",
352+
}:
347353
table_properties.pop(prop, None)
348354

349355
table_type = self._pop_creatable_type_from_properties(table_properties)

tests/core/engine_adapter/test_snowflake.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
from sqlmesh.utils import optional_import
1919
from tests.core.engine_adapter import to_sql_calls
2020
from sqlmesh.core.model.kind import ViewKind
21+
from sqlmesh.core.snapshot import DeployabilityIndex
22+
from sqlmesh.core.snapshot.evaluator import EngineManagedStrategy
2123

2224
pytestmark = [pytest.mark.engine, pytest.mark.snowflake]
2325

@@ -609,6 +611,85 @@ def test_ctas_skips_dynamic_table_properties(make_mocked_engine_adapter: t.Calla
609611
]
610612

611613

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+
612693
def test_set_current_catalog(make_mocked_engine_adapter: t.Callable):
613694
adapter = make_mocked_engine_adapter(SnowflakeEngineAdapter)
614695
adapter._default_catalog = "foo"

0 commit comments

Comments
 (0)