Skip to content

fix(format): keep SQL macro expressions in place across format runs - #1729

Queued
kvlonge wants to merge 3 commits into
mainfrom
fix/macro-expression-format
Queued

kvlonge wants to merge 3 commits into
mainfrom
fix/macro-expression-format

Conversation

@kvlonge

@kvlonge kvlonge commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

Why

sqb format never settled on a SQL macro whose body is an expression holding a nested, multi-line arithmetic expression in a subquery item without an alias, on Snowflake projects. Each run moved part of the body one more indent step left, so --fix always reported a change and sqb format --check could never pass. The meaning was kept; the layout drifted.

An expression body is formatted as the item of a SELECT, then one indent step is taken off every line. Snowflake names an unaliased select item after its text, so the formatter keeps that item's lines as written and never indents them. Taking the step off moved them left on every run. DuckDB has no such rule, which is why DuckDB projects settled.

Changes

  • Before formatting, every code line of an expression body after the first moves one step right. The step taken off afterwards now returns lines the formatter kept as written to where they were. Lines inside strings and comments are left alone both ways, as before.
  • Format requests name the indent step they rely on instead of the formatter's default.
  • DuckShop edge case: horner_ratio, a scoped macro nesting polynomials in an unaliased subquery item.

Verification

  • New case in the format-twice test of expression macro bodies: the nested polynomial on Snowflake. It fails without the fix.
  • New test: every tracked SQLBuild project in the repository, DuckShop included, is formatted twice on DuckDB, Snowflake, Databricks, BigQuery, PostgreSQL and SQL Server, and the second pass changes no file. Without the fix it fails on the new DuckShop macro.
  • New case: a line of spaces inside a kept Snowflake item stays in place on the second pass.
  • New e2e test: sqb format run twice on a Snowflake and a DuckDB project leaves the second run's file unchanged, and format --check passes.
  • sqlbuild-rules lint_text tests, clippy -D warnings, cargo fmt, fensu check (0 faults) and fensu dupes (none).

A macro or function body that is an expression is formatted as the item
of a SELECT, and every line is then moved one indent step left. Lines the
formatter keeps as written, such as an unaliased Snowflake select item,
were never moved right, so each run moved them one more step left and
`sqb format --check` never passed. The body's code lines now move one
step right before formatting, so the step taken off afterwards returns
them to where they were.
A line holding only spaces inside a select item that Snowflake keeps as written now moves right before formatting too, so taking the step off afterwards leaves it where it was.
@chio-labs-delivery
chio-labs-delivery Bot added this pull request to the merge queue Nov 30, 2026
Any commits made after this event will not be merged.
@kvlonge
kvlonge removed this pull request from the merge queue due to a manual request Nov 30, 2026
An unused local macro fails compile with S010, so stg_edge_partner_payloads now
calls horner_ratio on its FLOAT amount. The macro reads its subquery's inputs
as constants, so the model's tests, which already run on the warehouse, keep
their local and warehouse split.
@chio-labs-delivery
chio-labs-delivery Bot added this pull request to the merge queue Nov 30, 2026
Any commits made after this event will not be merged.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant