Skip to content

Commit 551b6f7

Browse files
committed
Fix: Always force recreation of materialized views
1 parent fe64851 commit 551b6f7

File tree

3 files changed

+58
-48
lines changed

3 files changed

+58
-48
lines changed

sqlmesh/core/snapshot/evaluator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2297,7 +2297,7 @@ def insert(
22972297
table_name,
22982298
query_or_df,
22992299
model.columns_to_types,
2300-
replace=not self.adapter.HAS_VIEW_BINDING,
2300+
replace=snapshot.is_materialized_view or not self.adapter.HAS_VIEW_BINDING,
23012301
materialized=self._is_materialized_view(model),
23022302
view_properties=kwargs.get("physical_properties", model.physical_properties),
23032303
table_description=model.description,

tests/core/engine_adapter/integration/test_integration.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3731,3 +3731,60 @@ def _set_config(_gateway: str, config: Config) -> None:
37313731
"incremental_model",
37323732
"seed_model",
37333733
]
3734+
3735+
3736+
def test_materialized_view_evaluation(ctx: TestContext):
3737+
adapter = ctx.engine_adapter
3738+
dialect = ctx.dialect
3739+
if not adapter.SUPPORTS_MATERIALIZED_VIEWS:
3740+
pytest.skip(f"Skipping engine {dialect} as it does not support materialized views")
3741+
3742+
elif dialect == "snowflake":
3743+
pytest.skip(
3744+
f"Skipping Snowflake as it requires an enterprise account for materialized views"
3745+
)
3746+
3747+
model_name = ctx.table("test_tbl")
3748+
mview_name = ctx.table("test_mview")
3749+
3750+
sqlmesh = ctx.create_context()
3751+
3752+
sqlmesh.upsert_model(
3753+
load_sql_based_model(
3754+
d.parse(
3755+
f"""
3756+
MODEL (name {model_name}, kind FULL);
3757+
3758+
SELECT 1 AS col
3759+
"""
3760+
)
3761+
)
3762+
)
3763+
3764+
sqlmesh.upsert_model(
3765+
load_sql_based_model(
3766+
d.parse(
3767+
f"""
3768+
MODEL (name {mview_name}, kind VIEW (materialized true));
3769+
3770+
SELECT * FROM {model_name}
3771+
"""
3772+
)
3773+
)
3774+
)
3775+
3776+
# Case 1: Ensure that plan is successful and we can query the materialized view
3777+
sqlmesh.plan(auto_apply=True, no_prompts=True)
3778+
3779+
df = adapter.fetchdf(f"SELECT * FROM {mview_name.sql(dialect=dialect)}")
3780+
assert df["col"][0] == 1
3781+
3782+
# Case 2: Ensure that we can change the underlying table and the materialized view is recreated
3783+
sqlmesh.upsert_model(
3784+
load_sql_based_model(d.parse(f"""MODEL (name {model_name}, kind FULL); SELECT 2 AS col"""))
3785+
)
3786+
3787+
sqlmesh.plan(auto_apply=True, no_prompts=True)
3788+
3789+
df = adapter.fetchdf(f"SELECT * FROM {mview_name.sql(dialect=dialect)}")
3790+
assert df["col"][0] == 2

tests/core/engine_adapter/integration/test_integration_bigquery.py

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -441,53 +441,6 @@ def test_table_diff_table_name_matches_column_name(ctx: TestContext):
441441
assert row_diff.full_match_count == 1
442442

443443

444-
def test_materialized_view_evaluation(ctx: TestContext, engine_adapter: BigQueryEngineAdapter):
445-
model_name = ctx.table("test_tbl")
446-
mview_name = ctx.table("test_mview")
447-
448-
sqlmesh = ctx.create_context()
449-
450-
sqlmesh.upsert_model(
451-
load_sql_based_model(
452-
d.parse(
453-
f"""
454-
MODEL (name {model_name}, kind FULL);
455-
456-
SELECT 1 AS col
457-
"""
458-
)
459-
)
460-
)
461-
462-
sqlmesh.upsert_model(
463-
load_sql_based_model(
464-
d.parse(
465-
f"""
466-
MODEL (name {mview_name}, kind VIEW (materialized true));
467-
468-
SELECT * FROM {model_name}
469-
"""
470-
)
471-
)
472-
)
473-
474-
# Case 1: Ensure that plan is successful and we can query the materialized view
475-
sqlmesh.plan(auto_apply=True, no_prompts=True)
476-
477-
df = engine_adapter.fetchdf(f"SELECT * FROM {mview_name.sql(dialect=ctx.dialect)}")
478-
assert df["col"][0] == 1
479-
480-
# Case 2: Ensure that we can change the underlying table and the materialized view is recreated
481-
sqlmesh.upsert_model(
482-
load_sql_based_model(d.parse(f"""MODEL (name {model_name}, kind FULL); SELECT 2 AS col"""))
483-
)
484-
485-
sqlmesh.plan(auto_apply=True, no_prompts=True)
486-
487-
df = engine_adapter.fetchdf(f"SELECT * FROM {mview_name.sql(dialect=ctx.dialect)}")
488-
assert df["col"][0] == 2
489-
490-
491444
def test_correlation_id_in_job_labels(ctx: TestContext):
492445
model_name = ctx.table("test")
493446

0 commit comments

Comments
 (0)