Skip to content

Commit f6fdb9b

Browse files
committed
Fix!: exclude Semicolon expressions from model state
1 parent c1ae64f commit f6fdb9b

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
@@ -9235,3 +9235,96 @@ def test_runtime_stage(evaluator):
92359235
model = load_sql_based_model(expressions, jinja_macros=jinja_macros)
92369236
assert model.render_query().sql() == "SELECT 'loading' AS a, 'loading_bla' AS b"
92379237
assert set(model.python_env) == {"noop", "test_runtime_stage"}
9238+
9239+
9240+
def test_semicolon_is_not_included_in_model_state(tmp_path, assert_exp_eq):
9241+
init_example_project(tmp_path, dialect="duckdb", template=ProjectTemplate.EMPTY)
9242+
9243+
db_path = str(tmp_path / "db.db")
9244+
db_connection = DuckDBConnectionConfig(database=str(tmp_path / "db.db"))
9245+
config = Config(
9246+
gateways={"duckdb": GatewayConfig(connection=db_connection)},
9247+
model_defaults=ModelDefaultsConfig(dialect="duckdb"),
9248+
)
9249+
9250+
model_file = tmp_path / "models" / "model_with_semicolon.sql"
9251+
model_file.write_text(
9252+
"""
9253+
MODEL (
9254+
name sqlmesh_example.incremental_model_with_semicolon,
9255+
kind INCREMENTAL_BY_TIME_RANGE (
9256+
time_column event_date
9257+
),
9258+
start '2020-01-01',
9259+
cron '@daily',
9260+
grain (id, event_date)
9261+
);
9262+
9263+
SELECT
9264+
1 AS id,
9265+
1 AS item_id,
9266+
CAST('2020-01-01' AS DATE) AS event_date
9267+
;
9268+
9269+
--Just a comment
9270+
"""
9271+
)
9272+
9273+
ctx = Context(paths=tmp_path, config=config)
9274+
model = ctx.get_model("sqlmesh_example.incremental_model_with_semicolon")
9275+
9276+
assert not model.pre_statements
9277+
# assert not model.post_statements
9278+
9279+
assert_exp_eq(
9280+
model.render_query(),
9281+
'SELECT 1 AS "id", 1 AS "item_id", CAST(\'2020-01-01\' AS DATE) AS "event_date"',
9282+
)
9283+
ctx.format()
9284+
9285+
assert (
9286+
model_file.read_text()
9287+
== """MODEL (
9288+
name sqlmesh_example.incremental_model_with_semicolon,
9289+
kind INCREMENTAL_BY_TIME_RANGE (
9290+
time_column event_date
9291+
),
9292+
start '2020-01-01',
9293+
cron '@daily',
9294+
grain (id, event_date)
9295+
);
9296+
9297+
SELECT
9298+
1 AS id,
9299+
1 AS item_id,
9300+
'2020-01-01'::DATE AS event_date;
9301+
9302+
/* Just a comment */"""
9303+
)
9304+
9305+
ctx.plan(no_prompts=True, auto_apply=True)
9306+
9307+
model_file = tmp_path / "models" / "model_with_semicolon.sql"
9308+
model_file.write_text(
9309+
"""
9310+
MODEL (
9311+
name sqlmesh_example.incremental_model_with_semicolon,
9312+
kind INCREMENTAL_BY_TIME_RANGE (
9313+
time_column event_date
9314+
),
9315+
start '2020-01-01',
9316+
cron '@daily',
9317+
grain (id, event_date)
9318+
);
9319+
9320+
SELECT
9321+
1 AS id,
9322+
1 AS item_id,
9323+
CAST('2020-01-01' AS DATE) AS event_date
9324+
"""
9325+
)
9326+
9327+
ctx.load()
9328+
plan = ctx.plan(no_prompts=True, auto_apply=True)
9329+
9330+
assert not plan.context_diff.modified_snapshots

0 commit comments

Comments
 (0)