Skip to content

Commit 32d94eb

Browse files
committed
Fix!: exclude Semicolon expressions from model state
1 parent 64c3305 commit 32d94eb

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
@@ -9246,6 +9246,7 @@ def test_python_env_references_are_unequal_but_point_to_same_definition(tmp_path
92469246

92479247
db_path = str(tmp_path / "db.db")
92489248
db_connection = DuckDBConnectionConfig(database=db_path)
9249+
92499250
config = Config(
92509251
gateways={"duckdb": GatewayConfig(connection=db_connection)},
92519252
model_defaults=ModelDefaultsConfig(dialect="duckdb"),
@@ -9361,3 +9362,95 @@ def f():
93619362

93629363
with pytest.raises(SQLMeshError, match=r"duplicate definitions found"):
93639364
Context(paths=tmp_path, config=config)
9365+
9366+
9367+
def test_semicolon_is_not_included_in_model_state(tmp_path, assert_exp_eq):
9368+
init_example_project(tmp_path, dialect="duckdb", template=ProjectTemplate.EMPTY)
9369+
9370+
db_connection = DuckDBConnectionConfig(database=str(tmp_path / "db.db"))
9371+
config = Config(
9372+
gateways={"duckdb": GatewayConfig(connection=db_connection)},
9373+
model_defaults=ModelDefaultsConfig(dialect="duckdb"),
9374+
)
9375+
9376+
model_file = tmp_path / "models" / "model_with_semicolon.sql"
9377+
model_file.write_text(
9378+
"""
9379+
MODEL (
9380+
name sqlmesh_example.incremental_model_with_semicolon,
9381+
kind INCREMENTAL_BY_TIME_RANGE (
9382+
time_column event_date
9383+
),
9384+
start '2020-01-01',
9385+
cron '@daily',
9386+
grain (id, event_date)
9387+
);
9388+
9389+
SELECT
9390+
1 AS id,
9391+
1 AS item_id,
9392+
CAST('2020-01-01' AS DATE) AS event_date
9393+
;
9394+
9395+
--Just a comment
9396+
"""
9397+
)
9398+
9399+
ctx = Context(paths=tmp_path, config=config)
9400+
model = ctx.get_model("sqlmesh_example.incremental_model_with_semicolon")
9401+
9402+
assert not model.pre_statements
9403+
assert not model.post_statements
9404+
9405+
assert_exp_eq(
9406+
model.render_query(),
9407+
'SELECT 1 AS "id", 1 AS "item_id", CAST(\'2020-01-01\' AS DATE) AS "event_date"',
9408+
)
9409+
ctx.format()
9410+
9411+
assert (
9412+
model_file.read_text()
9413+
== """MODEL (
9414+
name sqlmesh_example.incremental_model_with_semicolon,
9415+
kind INCREMENTAL_BY_TIME_RANGE (
9416+
time_column event_date
9417+
),
9418+
start '2020-01-01',
9419+
cron '@daily',
9420+
grain (id, event_date)
9421+
);
9422+
9423+
SELECT
9424+
1 AS id,
9425+
1 AS item_id,
9426+
'2020-01-01'::DATE AS event_date;
9427+
9428+
/* Just a comment */"""
9429+
)
9430+
9431+
ctx.plan(no_prompts=True, auto_apply=True)
9432+
9433+
model_file = tmp_path / "models" / "model_with_semicolon.sql"
9434+
model_file.write_text(
9435+
"""
9436+
MODEL (
9437+
name sqlmesh_example.incremental_model_with_semicolon,
9438+
kind INCREMENTAL_BY_TIME_RANGE (
9439+
time_column event_date
9440+
),
9441+
start '2020-01-01',
9442+
cron '@daily',
9443+
grain (id, event_date)
9444+
);
9445+
9446+
SELECT
9447+
1 AS id,
9448+
1 AS item_id,
9449+
CAST('2020-01-01' AS DATE) AS event_date
9450+
"""
9451+
)
9452+
9453+
ctx.load()
9454+
plan = ctx.plan(no_prompts=True, auto_apply=True)
9455+
9456+
assert not plan.context_diff.modified_snapshots

0 commit comments

Comments
 (0)