Skip to content

Commit ba228ab

Browse files
committed
Fix!: exclude Semicolon expressions from model state
1 parent 8e3ac17 commit ba228ab

2 files changed

Lines changed: 96 additions & 1 deletion

File tree

sqlmesh/core/model/common.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,9 @@ def parse_expression(
262262

263263
if isinstance(v, list):
264264
return [
265-
d.parse_one(e, dialect=dialect) if not isinstance(e, exp.Expression) else e for e in v
265+
e if isinstance(e, exp.Expression) else d.parse_one(e, dialect=dialect)
266+
for e in v
267+
if not isinstance(e, exp.Semicolon)
266268
]
267269

268270
if isinstance(v, str):

tests/core/test_model.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9332,6 +9332,7 @@ def test_python_env_references_are_unequal_but_point_to_same_definition(tmp_path
93329332

93339333
db_path = str(tmp_path / "db.db")
93349334
db_connection = DuckDBConnectionConfig(database=db_path)
9335+
93359336
config = Config(
93369337
gateways={"duckdb": GatewayConfig(connection=db_connection)},
93379338
model_defaults=ModelDefaultsConfig(dialect="duckdb"),
@@ -9447,3 +9448,95 @@ def f():
94479448

94489449
with pytest.raises(SQLMeshError, match=r"duplicate definitions found"):
94499450
Context(paths=tmp_path, config=config)
9451+
9452+
9453+
def test_semicolon_is_not_included_in_model_state(tmp_path, assert_exp_eq):
9454+
init_example_project(tmp_path, dialect="duckdb", template=ProjectTemplate.EMPTY)
9455+
9456+
db_connection = DuckDBConnectionConfig(database=str(tmp_path / "db.db"))
9457+
config = Config(
9458+
gateways={"duckdb": GatewayConfig(connection=db_connection)},
9459+
model_defaults=ModelDefaultsConfig(dialect="duckdb"),
9460+
)
9461+
9462+
model_file = tmp_path / "models" / "model_with_semicolon.sql"
9463+
model_file.write_text(
9464+
"""
9465+
MODEL (
9466+
name sqlmesh_example.incremental_model_with_semicolon,
9467+
kind INCREMENTAL_BY_TIME_RANGE (
9468+
time_column event_date
9469+
),
9470+
start '2020-01-01',
9471+
cron '@daily',
9472+
grain (id, event_date)
9473+
);
9474+
9475+
SELECT
9476+
1 AS id,
9477+
1 AS item_id,
9478+
CAST('2020-01-01' AS DATE) AS event_date
9479+
;
9480+
9481+
--Just a comment
9482+
"""
9483+
)
9484+
9485+
ctx = Context(paths=tmp_path, config=config)
9486+
model = ctx.get_model("sqlmesh_example.incremental_model_with_semicolon")
9487+
9488+
assert not model.pre_statements
9489+
assert not model.post_statements
9490+
9491+
assert_exp_eq(
9492+
model.render_query(),
9493+
'SELECT 1 AS "id", 1 AS "item_id", CAST(\'2020-01-01\' AS DATE) AS "event_date"',
9494+
)
9495+
ctx.format()
9496+
9497+
assert (
9498+
model_file.read_text()
9499+
== """MODEL (
9500+
name sqlmesh_example.incremental_model_with_semicolon,
9501+
kind INCREMENTAL_BY_TIME_RANGE (
9502+
time_column event_date
9503+
),
9504+
start '2020-01-01',
9505+
cron '@daily',
9506+
grain (id, event_date)
9507+
);
9508+
9509+
SELECT
9510+
1 AS id,
9511+
1 AS item_id,
9512+
'2020-01-01'::DATE AS event_date;
9513+
9514+
/* Just a comment */"""
9515+
)
9516+
9517+
ctx.plan(no_prompts=True, auto_apply=True)
9518+
9519+
model_file = tmp_path / "models" / "model_with_semicolon.sql"
9520+
model_file.write_text(
9521+
"""
9522+
MODEL (
9523+
name sqlmesh_example.incremental_model_with_semicolon,
9524+
kind INCREMENTAL_BY_TIME_RANGE (
9525+
time_column event_date
9526+
),
9527+
start '2020-01-01',
9528+
cron '@daily',
9529+
grain (id, event_date)
9530+
);
9531+
9532+
SELECT
9533+
1 AS id,
9534+
1 AS item_id,
9535+
CAST('2020-01-01' AS DATE) AS event_date
9536+
"""
9537+
)
9538+
9539+
ctx.load()
9540+
plan = ctx.plan(no_prompts=True, auto_apply=True)
9541+
9542+
assert not plan.context_diff.modified_snapshots

0 commit comments

Comments
 (0)