Skip to content

Print the WINDOW clause after HAVING - #350

Merged
git-hulk merged 1 commit into
AfterShip:masterfrom
bardrop-oss:fix-window-clause-order
Nov 26, 2026
Merged

git-hulk merged 1 commit into
AfterShip:masterfrom
bardrop-oss:fix-window-clause-order

Conversation

@bardrop

@bardrop bardrop commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Summary

The formatter writes WINDOW right after FROM. ClickHouse expects it after HAVING and before QUALIFY, and #173 moved the parser there, but the output side was left as it was. So the result is not valid SQL as soon as the query has another clause in between:

SELECT sum(b) OVER w FROM t WHERE b > 0 WINDOW w AS (PARTITION BY a)

is formatted as

SELECT sum(b) OVER w FROM t WINDOW w AS (PARTITION BY a) WHERE b > 0

which ParseStmts rejects. The fixtures did not show it because none of them has WINDOW together with PREWHERE, WHERE, GROUP BY or HAVING.

Changes

  • FormatSQL writes WINDOW after HAVING.
  • Accept and Walk visit it at the same place, so both follow the source order.

Tests

  • New fixture query/select_window_clause_order.sql with all these clauses (compact, beautified, AST).
  • TestTraversal_SelectClauseOrder for Walk and Accept.

All of them fail without the change. I had no clickhouse-local here. The order is the one in the SELECT syntax, and #343 checked HAVING, WINDOW, QUALIFY with it.

@git-hulk
git-hulk merged commit 410a500 into AfterShip:master Nov 26, 2026
2 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.

2 participants