Skip to content

fix(macros): keep SAFE_ADD, SAFE_SUB and SAFE_DIV grouped inside surrounding operators - #6074

Merged
mday-io merged 2 commits into
SQLMesh:mainfrom
Rodrigo-Palma:fix/safe-sub-parens
Nov 22, 2026
Merged

mday-io merged 2 commits into
SQLMesh:mainfrom
Rodrigo-Palma:fix/safe-sub-parens

Conversation

@Rodrigo-Palma

@Rodrigo-Palma Rodrigo-Palma commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor

Description

Closes #5649.

@SAFE_ADD and @SAFE_SUB build CASE WHEN <all fields> IS NULL THEN NULL ELSE <arithmetic> END. When a field is a literal, the optimizer resolves 1 IS NULL to false and sqlglot's simplify_conditionals replaces the whole CASE with its ELSE branch. The branch is inserted as a bare Sub/Add node, so an enclosing operator swallows its left operand. Parentheses written around the macro call don't help: simplify_parens drops them in the same pass because the node they wrap is still a CASE at that point.

With the model from the issue, the projection renders as

CASE WHEN ... END * COALESCE(1, 0) - COALESCE("test_data"."percent_off" / 100, 0)

and DuckDB returns 99.8, 175 and 489 instead of 80, 175 and 0.

The collapse itself is sqlglot behaviour and reproduces without SQLMesh on 30.8.0 (the current pin), 30.18.0 and sqlglot main:

simplify(parse_one("SELECT x * CASE WHEN FALSE THEN NULL ELSE a - b END FROM t")).sql()
# 'SELECT x * a - b FROM t'

That deserves an upstream fix (wrapping the promoted branch when its new parent is an operator), and it also affects hand written CASE expressions. This PR makes the macros correct under the current pin regardless of that: the ELSE arithmetic is emitted as an explicit Paren, which is what the AST should hold for a grouped value, and the optimizer keeps it wherever it is needed.

@SAFE_DIV had a related defect that does not involve the optimizer. It returns a bare Div, and after the evaluator serializes and reparses the query, the quotient is no longer a single operand: x / @SAFE_DIV(a, b) rendered as x / a / NULLIF(b, 0), which is (x / a) / b. The macro now returns the quotient wrapped in a Paren.

Output changes to be aware of:

  • @SAFE_ADD/@SAFE_SUB render ELSE (COALESCE(a, 0) + COALESCE(b, 0)) END in every usage.
  • @SAFE_DIV used as a standalone projection keeps its outer parentheses when the query is not optimized (for example, dependencies missing from the schema). The optimizer removes them otherwise.
  • Fingerprints are unaffected: the data hash uses the unrendered query.

The doctests and the three examples in docs/concepts/macros/sqlmesh_macros.md were updated to the new output.

I also tried adding the parentheses generically in MacroEvaluator.transform for any binary result placed under an operator. It changes unrelated output (for example test_merge_filter_macro gains parentheses around a predicate) and would need full precedence handling, so it is left out of this fix.

Test Plan

  • New test_safe_arithmetic_macros_keep_precedence_after_optimization in tests/core/test_macros.py, parametrized over the issue example and sibling cases (x * @SAFE_SUB(1, y), -@SAFE_SUB(1, y), x - @SAFE_ADD(1, y), x * @SAFE_ADD(1, y), nested @SAFE_DIV(@SAFE_SUB(...), x), x / @SAFE_DIV(...), x * @SAFE_DIV(...), plus two standalone usages). It renders a model through the optimizer and asserts the projection SQL. 9 of the 10 cases fail on main; the remaining standalone @SAFE_DIV case passes on both and guards against the optimizer keeping redundant parentheses.
  • Ran the issue model on DuckDB: 80, 175, 0 after the change (99.8, 175, 489 before).
  • make py-style: ruff, ruff-format, mypy and migrations check pass.
  • tests/core/test_macros.py: 152 passed (143 passed, 9 failed against main).
  • make doc-test: 29 passed.
  • make fast-test: 2634 passed, 4 skipped, then 5, 1 and 161 passed in the isolated steps.
  • make cicd-test (Python 3.12, macOS): 3393 passed, 22 skipped in the main step. The isolated, registry and dialect steps pass (10, 1, 161). The pyspark step passes (3) once SPARK_HOME points at pyspark's bundled Spark instead of a missing local install. It fails the same way on main with that local setting.

Checklist

  • I have run make style and fixed any issues
  • I have added tests for my changes (if applicable)
  • All existing tests pass (make fast-test)
  • My commits are signed off (git commit -s) per the DCO

Summary by CodeRabbit

  • Bug Fixes
    • Safe addition and subtraction now return NULL when all inputs are NULL.
    • Safe arithmetic expressions are grouped consistently, including division that avoids a zero denominator.

…ounding operators

The optimizer collapses the CASE built by SAFE_ADD/SAFE_SUB into its ELSE
branch without parentheses, so an enclosing operator swallows an operand.
Emit the ELSE arithmetic as an explicit Paren. SAFE_DIV now returns its
quotient wrapped in a Paren so x / @SAFE_DIV(a, b) keeps its grouping.

Closes SQLMesh#5649

Signed-off-by: Rodrigo-Palma <email.rodrigopalma@gmail.com>
@Rodrigo-Palma

Rodrigo-Palma commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor Author

Upstream now has a fix for the root cause. tobymao/sqlglot#8384, against the issue I filed there (tobymao/sqlglot#8380), parenthesizes the promoted branch when a collapsed CASE/IF lands under an operator. Its fixtures cover the shapes these macros produce: x * CASE WHEN FALSE THEN NULL ELSE a - b END, the same with explicit parentheses around the CASE, the IF form, and the unary -CASE .... It is still open, not merged.

Two things that don't change because of it:

  1. The pin. pyproject.toml requires sqlglot~=30.8.0 and the latest sqlglot release is 30.18.0, so the upstream fix would land in 30.19.0, outside the current range. @SAFE_ADD and @SAFE_SUB stay wrong on every sqlglot version SQLMesh currently accepts until that bump happens.

  2. @SAFE_DIV. That defect does not involve the optimizer: the macro returns a bare Div, and once the evaluator serializes and reparses the query, x / @SAFE_DIV(a, b) renders as x / a / NULLIF(b, 0). fix(optimizer): preserve precedence when collapsing CASE/IF branches [BIG-PICKLE] tobymao/sqlglot#8384 does not touch that path, so the Paren in this PR is what fixes it.

Three ways to go, whichever you prefer:

  • Merge as is. The macros emit a Paren for a value that is grouped, which is what the AST should hold regardless of the optimizer, and it is correct before and after any bump. The cost is the output change listed above: ELSE (COALESCE(a, 0) + COALESCE(b, 0)) END in every usage.
  • Keep only the @SAFE_DIV change and let the sqlglot bump fix @SAFE_ADD/@SAFE_SUB. I can trim the PR and its tests to that scope.
  • Close this and track it behind the bump. In that case I'd leave @SAFE_SUB gets optimized during model rendering, causing improper calculations #5649 open with the upstream link, since it reproduces on every currently pinned version.

Happy to do any of the three.

@Rodrigo-Palma

Copy link
Copy Markdown
Contributor Author

Correcting my comment above: tobymao/sqlglot#8384 was closed without merging, about four hours after I linked it. georgesittas closed it with "I don't think this is the right approach. Closing and will take this on myself." My issue there (tobymao/sqlglot#8380) is still open, and there is no replacement PR open at the moment.

I checked the current state rather than assuming, against main at 59e9702 (2026-09-18):

input simplify() on sqlglot main today
x * CASE WHEN FALSE THEN NULL ELSE a - b END x * a - b
x * IF(FALSE, NULL, a - b) x * a - b
-CASE WHEN FALSE THEN NULL ELSE a - b END -a - b
x * (CASE WHEN FALSE THEN NULL ELSE a - b END) x * (a - b) (correct)

So the root cause is still live upstream, with no version to wait for: no open PR, and the maintainer reimplementing it on his own schedule. That removes the timing argument from my third option (close this and track it behind the bump), since there is nothing concrete to track it behind.

The rest of the comment stands. @SAFE_DIV never depended on the optimizer anyway, and sqlglot~=30.8.0 would keep @SAFE_ADD/@SAFE_SUB broken on every version this project currently accepts even after an upstream fix ships. Still happy to merge as is, or to trim this down to the @SAFE_DIV change alone, whichever you prefer.

@Rodrigo-Palma

Copy link
Copy Markdown
Contributor Author

Update: the root cause is fixed upstream. georgesittas opened tobymao/sqlglot#8389 himself and merged it today (2026-09-19, 06c7b3d), closing my issue tobymao/sqlglot#8380 as completed. It touches optimizer/simplify.py and the simplify fixtures.

I verified it rather than taking the title for it, on sqlglot main at 99940ea (which contains that commit):

input simplify() on sqlglot main today
x * CASE WHEN FALSE THEN NULL ELSE a - b END x * (a - b)
x * IF(FALSE, NULL, a - b) x * (a - b)
-CASE WHEN FALSE THEN NULL ELSE a - b END -(a - b)
x * (CASE WHEN FALSE THEN NULL ELSE a - b END) x * (a - b)

All four are correct now, so the timing argument I withdrew yesterday is back: there is something concrete to wait for.

Two things still hold:

  1. The pin. pyproject.toml requires sqlglot~=30.8.0, which accepts 30.8.x only. The fix is not in any release yet (the latest tag is v30.18.0, from 2026-09-03, and main is 75 commits ahead of it), so it ships in 30.19.0 at the earliest, and it is marked breaking (fix(optimizer)!), which makes the bump a decision of its own rather than a routine one. @SAFE_ADD and @SAFE_SUB stay wrong on every sqlglot version this project currently accepts until that happens.

  2. @SAFE_DIV. Unchanged by any of this. The macro returns a bare Div, and once the evaluator serializes and reparses the query, x / @SAFE_DIV(a, b) renders as x / a / NULLIF(b, 0). sqlglot#8389 does not touch that path; the Paren in this PR is what fixes it.

So the same three options, with the third one now viable:

  • Merge as is. The macros emit a Paren for a value that is grouped, correct before and after any bump, at the cost of the output change listed in the description.
  • Trim to the @SAFE_DIV change and let the sqlglot bump fix @SAFE_ADD/@SAFE_SUB. I can cut the PR and its tests to that scope.
  • Close this and track it behind the bump, keeping @SAFE_SUB gets optimized during model rendering, causing improper calculations #5649 open with the upstream link, since it reproduces on every currently pinned version.

Whichever you prefer. Note that CI here never ran: the workflows are sitting in action_required awaiting approval, so the checks on this PR are empty rather than failing.

@Rodrigo-Palma

Copy link
Copy Markdown
Contributor Author

Ran this repository's full CI on my fork, on the same commit as this PR (8bd2483), since the runs here are still awaiting approval:

https://github.com/Rodrigo-Palma/sqlmesh/actions/runs/35449729558

28 jobs, all green: style-and-cicd-tests on Python 3.9 through 3.13, cicd-tests-windows, migration-test, doc-tests, test-dbt-versions 1.3 through 1.12, and engine-tests-docker across duckdb, postgres, mysql, mssql, trino, spark, clickhouse, starrocks and risingwave. The only skipped jobs are the ones the changes job switches off for areas this PR does not touch.

This supersedes the last line of my previous comment, which said CI had never run here.

@coderabbitai

coderabbitai Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Safe arithmetic macros now parenthesize their arithmetic expressions. safe_add and safe_sub use a shared helper to build their CASE expressions. New tests check rendered SQL in loaded models.

Changes: Safe arithmetic macros

Layer / File(s) Summary
Safe arithmetic macro expressions
sqlmesh/core/macros.py
safe_add and safe_sub delegate their CASE construction to _null_if_all_null, which returns NULL when all inputs are NULL and parenthesizes the arithmetic expression. safe_div returns a parenthesized quotient and has an updated return annotation.
Rendered output and examples
tests/core/test_macros.py, docs/concepts/macros/sqlmesh_macros.md
Parametrized tests check rendered safe arithmetic expressions in loaded models. The SAFE_ADD, SAFE_SUB, and SAFE_DIV examples now show parenthesized arithmetic expressions.

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

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: vaggelisd

Merge Risk: Minimal · up to ee61d

The change keeps the safe arithmetic macros grouped inside surrounding operators, which fixes the miscalculation reported in the linked issue. No merge-blocking risk was identified in the reviewed changes.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the three changed macros (SAFE_ADD, SAFE_SUB, SAFE_DIV) and the core fix: keeping them grouped inside surrounding operators. The changeset adds Paren wrapping to the ELSE arithmetic in…
Description check Passed The description covers the problem, root cause, the fix, output changes, and test results. It follows the template's Description and Test Plan sections and includes the checklist. It cites the linked …
Linked Issues check Passed Issue #5649 requires that @SAFE_SUB keep its grouping inside surrounding arithmetic. At the reviewed head, sqlmesh/core/macros.py routes @SAFE_ADD and @SAFE_SUB through the new helper _null_if_all_nul…
Out of Scope Changes check Passed The diff changes three things beyond the literal @SAFE_SUB example: @SAFE_ADD ELSE parenthesization, @SAFE_DIV quotient wrapping, and matching doctest and documentation updates. @SAFE_ADD uses the sam…
Full details: Title check

Explanation

The title names the three changed macros (SAFE_ADD, SAFE_SUB, SAFE_DIV) and the core fix: keeping them grouped inside surrounding operators. The changeset adds Paren wrapping to the ELSE arithmetic in safe_add/safe_sub, wraps the safe_div quotient in a Paren, and updates docs and tests to match. The title is specific and matches the main change. It is 86 characters, which is slightly long but acceptable.

Full details: Description check

Explanation

The description covers the problem, root cause, the fix, output changes, and test results. It follows the template's Description and Test Plan sections and includes the checklist. It cites the linked issue (#5649) and explains why the fix is local. The checklist items are marked as complete, and the test plan lists the commands run and their outcomes. Minor template deviations: the 'Description' and 'Test Plan' headings are present, and the checklist is kept. No required section is missing.

Full details: Linked Issues check

Explanation

Issue #5649 requires that @SAFE_SUB keep its grouping inside surrounding arithmetic. At the reviewed head, sqlmesh/core/macros.py routes @SAFE_ADD and @SAFE_SUB through the new helper _null_if_all_null, which wraps the ELSE arithmetic in exp.paren. The optimizer can no longer promote a bare Sub node into the enclosing operator, so the issue model keeps (price - amount_off) as one operand. The PR description reports DuckDB output changing from 99.8, 175, 489 to 80, 175, 0. The new test test_safe_arithmetic_macros_keep_precedence_after_optimization covers the issue example, per the PR summary; I did not read the test file directly. The issue sets no additional acceptance criteria. Evidence comes from the macros.py diff and PR description, not from a rerun of the test suite.

Full details: Out of Scope Changes check

Explanation

The diff changes three things beyond the literal @SAFE_SUB example: @SAFE_ADD ELSE parenthesization, @SAFE_DIV quotient wrapping, and matching doctest and documentation updates. @SAFE_ADD uses the same CASE construction and the same optimizer collapse as @SAFE_SUB, so the shared helper is a direct part of the fix for #5649. The @SAFE_DIV change returns exp.paren(numerator / NULLIF(...)). The PR description states that a bare Div breaks grouping after serialization and reparsing, which the issue's operator-absorption problem also describes. The PR states that the generic MacroEvaluator.transform alternative was rejected because it changes unrelated output. The docs and doctest changes only reflect the new rendered output. No unrelated code changes appear in the reviewed macros.py diff.

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

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: f59ea670-224a-4b27-bf94-da29f85f18b8
Commits

Reviewing files that changed from the base of the PR and between dd16a0f and ee61dec.

Files selected for processing: 3

  • docs/concepts/macros/sqlmesh_macros.md
  • sqlmesh/core/macros.py
  • tests/core/test_macros.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.

@mday-io

mday-io commented Nov 22, 2026

Copy link
Copy Markdown
Collaborator

thanks for your contribution!

@mday-io
mday-io merged commit ca3f919 into SQLMesh:main Nov 22, 2026
34 of 36 checks passed
@mday-io mday-io added the Bug Something isn't working label Nov 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

@SAFE_SUB gets optimized during model rendering, causing improper calculations

2 participants