Skip to content

fix: support partitioned_by = <expr> in model definitions - #6294

Open
andretorresdg wants to merge 1 commit into
SQLMesh:mainfrom
andretorresdg:partitioned-by-equals
Open

andretorresdg wants to merge 1 commit into
SQLMesh:mainfrom
andretorresdg:partitioned-by-equals

Conversation

@andretorresdg

Copy link
Copy Markdown

Description

Fixes #5234

partitioned_by = <expr> (with =) in a MODEL block is not handled consistently:

  • ClickHouse: sqlglot's ClickHouse _parse_partitioned_by doesn't skip the =, so on current main (sqlglot 30.8) the model fails to load with Required keyword: 'this' missing for EQ.
  • Other dialects: the base _parse_partitioned_by consumes the =, but the start token for meta["sql"] is recorded before it, so the SQL kept for state can include it. For example, with BigQuery partitioned_by = DATE_TRUNC(ds, MONTH) is stored as "= DATE_TRUNC(ds, MONTH)", and loading it back fails with Failed to parse '(= DATE_TRUNC(ds, MONTH))' into PartitionedByProperty. This is the error from the issue.

The fix consumes the optional = in SQLMesh's model parser before recording the start token and calling _parse_partitioned_by. partitioned_by = x now gives the same result as partitioned_by x in every dialect.

Test Plan

Added test_partitioned_by_with_equals_sign in tests/core/test_model.py, parametrized over ClickHouse (single expression and tuple) and BigQuery. It loads the model, checks partitioned_by, then round-trips it through SqlModel.parse_raw(model.json()). All 3 cases fail without the change and pass with it.

tests/core/test_model.py and tests/core/test_dialect.py give the same results as on main apart from the 3 new cases. ruff check, ruff format --check and mypy pass on the changed files.

Checklist

  • I have run make style and fixed any issues (ran ruff and mypy on the changed files)
  • I have added tests for my changes (if applicable)
  • All existing tests pass (make fast-test) (ran tests/core/test_model.py and tests/core/test_dialect.py only)
  • My commits are signed off (git commit -s) per the DCO
Consume the optional '=' before parsing the partitioned_by value. ClickHouse's
_parse_partitioned_by does not accept it, so the model failed to load. Dialects
that do accept it could keep it in the SQL stored in state (e.g. BigQuery with a
function expression), which then failed to deserialize with
"Failed to parse '(= ...)' into PartitionedByProperty".

Signed-off-by: Andre Devay <andretorresdg@alumni.usp.br>
@github-actions github-actions Bot added the Bug Something isn't working label Dec 1, 2026
@coderabbitai

coderabbitai Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Summary

Fixes partitioned_by = <expr> in model definitions. The parser now consumes an optional = before it records the start token and parses the partition expression. Without =, parsing does not change.

Changed files:

  • sqlmesh/core/dialect.py (+5): _create_parser change for partitioned_by.
  • tests/core/test_model.py (+30): tests for ClickHouse and BigQuery, including a parse and serialization round-trip.

Scope: 5 lines added and 0 removed in sqlmesh/core (excluding tests and docs). Changed file under sqlmesh/core: sqlmesh/core/dialect.py. No changes under sqlmesh/utils.

The partitioned_by parser now accepts an optional = before the partition expression. Tests cover ClickHouse and BigQuery expressions and verify their values after a SqlModel JSON round-trip.

Changes: Partition Expression Parsing

Layer / File(s) Summary
Parse and preserve partition expressions
sqlmesh/core/dialect.py, tests/core/test_model.py
The parser consumes an optional = before parsing the partition expression and updates the SQL-capture start position. Tests check ClickHouse and BigQuery expressions before and after a SqlModel JSON round-trip.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: mday-io, ptruscott

Merge Risk: Minimal · up to 8d995

This change lets model definitions use partitioned_by = expr, fixing the ClickHouse parse failure and the deserialization failure in other dialects. No merge-blocking risk is evident, and the added tests cover the new syntax.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: the fix makes partitioned_by = <expr> work in model definitions. The changeset matches this. The parser now consumes an optional = before the partition expression,…
Description check Passed The description follows the template sections (Description, Test Plan, Checklist). It explains the root cause for ClickHouse and for other dialects, and it links issue #5234. The Test Plan names the n…
Linked Issues check Passed Issue #5234 requires partitioned_by = toYYYYMM(time) in a ClickHouse model to load and to round-trip through state without the Failed to parse '(= ...)' into PartitionedByProperty error. The chang…
Out of Scope Changes check Passed The changed hunk in sqlmesh/core/dialect.py is limited to the partitioned_by branch of the MODEL parser and directly implements the issue requirement. The test additions in `tests/core/test_model.…
Full details: Title check

Explanation

The title names the main change: the fix makes partitioned_by = &lt;expr&gt; work in model definitions. The changeset matches this. The parser now consumes an optional = before the partition expression, and a new test covers ClickHouse and BigQuery. The title is specific and uses the fix prefix.

Full details: Description check

Explanation

The description follows the template sections (Description, Test Plan, Checklist). It explains the root cause for ClickHouse and for other dialects, and it links issue #5234. The Test Plan names the new test, the cases it covers, and the test files it ran. The checklist is filled in. The one unchecked item is the full make fast-test run, and the author states that gap openly. The description is mostly complete.

Full details: Linked Issues check

Explanation

Issue #5234 requires partitioned_by = toYYYYMM(time) in a ClickHouse model to load and to round-trip through state without the Failed to parse '(= ...)' into PartitionedByProperty error. The change in sqlmesh/core/dialect.py (_create_parser, partitioned_by branch) matches an optional = with TokenType.EQ before calling _parse_partitioned_by(). It also resets start after the =, so the captured SQL no longer includes it. This covers the ClickHouse rejection and the stored-SQL error for other dialects. The added test_partitioned_by_with_equals_sign in tests/core/test_model.py covers ClickHouse (single expression and tuple) and BigQuery, including a SqlModel.parse_raw(model.json()) round-trip. The PR author reports these cases fail before the change and pass after it. This review did not run the tests. The result rests on the diff and the author's report.

Full details: Out of Scope Changes check

Explanation

The changed hunk in sqlmesh/core/dialect.py is limited to the partitioned_by branch of the MODEL parser and directly implements the issue requirement. The test additions in tests/core/test_model.py exercise that same behavior. No unrelated changes appear in the reviewed diff. The summary lists no changes to public declarations.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between eb15c1f and 8d99554.

Files selected for processing: 2

  • sqlmesh/core/dialect.py
  • tests/core/test_model.py

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug Something isn't working

1 participant