Skip to content

Commit 6336f43

Browse files
committed
preserve the semantics of the 'creating' runtime stage
1 parent 3317c3a commit 6336f43

File tree

4 files changed

+11
-8
lines changed

4 files changed

+11
-8
lines changed

docs/concepts/macros/macro_variables.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ SQLMesh provides additional predefined variables used to modify model behavior b
130130

131131
* @runtime_stage - A string value denoting the current stage of the SQLMesh runtime. Typically used in models to conditionally execute pre/post-statements (learn more [here](../models/sql_models.md#optional-prepost-statements)). It returns one of these values:
132132
* 'loading' - The project is being loaded into SQLMesh's runtime context.
133-
* 'creating' - The model tables are being created without inserting data.
134-
* 'evaluating' - The model query logic is being evaluated.
133+
* 'creating' - The model tables are being created for the first time. The data may be inserted during table creation.
134+
* 'evaluating' - The model query logic is evaluated, and the data is inserted into the existing model table.
135135
* 'promoting' - The model is being promoted in the target environment (view created during virtual layer update).
136136
* 'demoting' - The model is being demoted in the target environment (view dropped during virtual layer update).
137137
* 'auditing' - The audit is being run.

sqlmesh/core/snapshot/evaluator.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -707,12 +707,15 @@ def _evaluate_snapshot(
707707
not _intervals(snapshot, deployability_index) or not target_table_exists
708708
) and batch_index == 0
709709

710+
# Use the 'creating' stage if the table doesn't exist yet to preserve backwards compatibility with existing projects
711+
# that depend on a separate physical table creation stage.
712+
runtime_stage = RuntimeStage.EVALUATING if target_table_exists else RuntimeStage.CREATING
710713
common_render_kwargs = dict(
711714
start=start,
712715
end=end,
713716
execution_time=execution_time,
714717
snapshot=snapshot,
715-
runtime_stage=RuntimeStage.EVALUATING,
718+
runtime_stage=runtime_stage,
716719
**kwargs,
717720
)
718721
create_render_kwargs = dict(

tests/core/test_context.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1776,14 +1776,14 @@ def test_plan_environment_statements(tmp_path: pathlib.Path):
17761776
);
17771777
17781778
@IF(
1779-
@runtime_stage = 'evaluating',
1779+
@runtime_stage IN ('evaluating', 'creating'),
17801780
SET VARIABLE stats_model_start = now()
17811781
);
17821782
17831783
SELECT 1 AS cola;
17841784
17851785
@IF(
1786-
@runtime_stage = 'evaluating',
1786+
@runtime_stage IN ('evaluating', 'creating'),
17871787
INSERT INTO analytic_stats (physical_table, evaluation_start, evaluation_end, evaluation_time)
17881788
VALUES (@resolve_template('@{schema_name}.@{table_name}'), getvariable('stats_model_start'), now(), now() - getvariable('stats_model_start'))
17891789
);
@@ -1849,11 +1849,11 @@ def access_adapter(evaluator):
18491849

18501850
assert (
18511851
model.pre_statements[0].sql()
1852-
== "@IF(@runtime_stage = 'evaluating', SET VARIABLE stats_model_start = NOW())"
1852+
== "@IF(@runtime_stage IN ('evaluating', 'creating'), SET VARIABLE stats_model_start = NOW())"
18531853
)
18541854
assert (
18551855
model.post_statements[0].sql()
1856-
== "@IF(@runtime_stage = 'evaluating', INSERT INTO analytic_stats (physical_table, evaluation_start, evaluation_end, evaluation_time) VALUES (@resolve_template('@{schema_name}.@{table_name}'), GETVARIABLE('stats_model_start'), NOW(), NOW() - GETVARIABLE('stats_model_start')))"
1856+
== "@IF(@runtime_stage IN ('evaluating', 'creating'), INSERT INTO analytic_stats (physical_table, evaluation_start, evaluation_end, evaluation_time) VALUES (@resolve_template('@{schema_name}.@{table_name}'), GETVARIABLE('stats_model_start'), NOW(), NOW() - GETVARIABLE('stats_model_start')))"
18571857
)
18581858

18591859
stats_table = context.fetchdf("select * from memory.analytic_stats").to_dict()

tests/core/test_integration.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6207,7 +6207,7 @@ def test_plan_production_environment_statements(tmp_path: Path):
62076207
);
62086208
62096209
@IF(
6210-
@runtime_stage = 'evaluating',
6210+
@runtime_stage IN ('evaluating', 'creating'),
62116211
INSERT INTO schema_names_for_prod (physical_schema_name) VALUES (@resolve_template('@{schema_name}'))
62126212
);
62136213

0 commit comments

Comments
 (0)