Skip to content

fix(format): lay out CTE macros with comments between them - #46

Merged
kvlonge merged 1 commit into
mainfrom
fix/cte-macro-comments
Nov 9, 2026
Merged

kvlonge merged 1 commit into
mainfrom
fix/cte-macro-comments

Conversation

@kvlonge

@kvlonge kvlonge commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

Why

SQLBuild formats models whose CTE list mixes macro calls and comments, such as:

WITH base AS (SELECT 1 AS order_id),
-- First order stage.
@py.make_ctes("first_orders", "base"),
-- Second order stage.
@py.make_ctes("second_orders", "first_orders"),
@py.other_ctes("third_orders", "second_orders"),
-- Publish order identifiers.
final AS (SELECT order_id FROM third_orders)
SELECT * FROM final

SQLClean 0.6.1 declines it with "native formatter could not restore CTE macro placeholders". A comment before or after a run of placeholders works; a comment between two of them does not.

Changes

  • A comment between CTE macro placeholders stays in the SQL laid out without them, so the placeholders after it are now taken out as their own group and put back after it. Before, the whole run went back before the comment and the token check refused the result.
  • A comment between a placeholder and its own comma is still declined, as before; the token check catches it.

Verification

  • New layout-rule case with that shape on DuckDB: it fails without the fix (formatted false) and passes with it, including the fingerprint and idempotency checks.
  • Probed on DuckDB and Snowflake: block comments and a trailing comment after a placeholder's comma also format; every result keeps the authored tokens and comments in order.
  • DuckShop's 7 captured format requests give byte-identical responses before and after.
  • make verify: fensu 0 faults; cargo 40 + 8 passed; pytest 72 passed.
  • SQLQuack stays pinned at 804864d2, the rev SQLBuild's cutover pins.

A comment between two CTE macro placeholders stays in the SQL laid out
without them, so the placeholders after it are now put back after it
as a group of their own. Before, the whole run went back before the
comment, the token check failed, and the query was declined with
"native formatter could not restore CTE macro placeholders".
@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying sqlclean with  Cloudflare Pages  Cloudflare Pages

Latest commit: 916acb1
Status: ✅  Deploy successful!
Preview URL: https://77dd9723.sqlclean.pages.dev
Branch Preview URL: https://fix-cte-macro-comments.sqlclean.pages.dev

View logs

@kvlonge
kvlonge merged commit 34d709f into main Nov 9, 2026
15 checks passed
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