Skip to content

Fix inconsistent AST formatting for a no-argument window function in CODEC - #107806

Merged
alexey-milovidov merged 3 commits into
masterfrom
fix/inconsistent-ast-window-in-codec
Jun 23, 2026
Merged

alexey-milovidov merged 3 commits into
masterfrom
fix/inconsistent-ast-window-in-codec

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Jun 18, 2026 •

Copy link
Copy Markdown
Member

The AST fuzzer found an Inconsistent AST formatting logical error (STID 1941-26fa) on the stress test of #106522: a no-argument window function inside a CODEC declaration loses its parentheses when formatted.

A function with no arguments inside a CODEC (or an engine) is intentionally formatted without parentheses — NONE, LZ4, MergeTree — because the codec/engine parser (ParserIdentifierWithOptionalParameters) calls setNoEmptyArgs(true), which also clears the empty arguments node. When the parsed element is a window function, e.g. CODEC(cume_dist() OVER (...)), its () was dropped too. But a window function must keep them: cume_dist OVER (...) re-parses as cume_dist() OVER (...), so the formatted query did not survive a parse round-trip, tripping the consistency check.

The fix makes ASTFunction formatting always print a window function's parentheses (f() OVER (...)), regardless of noEmptyArgs. Plain zero-argument codecs and engines (NONE, LZ4, MergeTree) still format without parentheses.

The fuzzer's CREATE TABLE is itself degenerate (a window function is not a valid codec), but the formatter must still round-trip any query the parser accepts.

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fixed an Inconsistent AST formatting logical error for a no-argument window function inside a CODEC or engine declaration (e.g. CODEC(cume_dist() OVER (...))); such a function now keeps its parentheses so the query survives a format/parse round-trip.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.6.1.1178

…CODEC

A function with no arguments inside a `CODEC` (or engine) declaration is
formatted without parentheses (e.g. `NONE`, `LZ4`, `MergeTree`), because the
codec/engine parser sets `noEmptyArgs` on it (and `setNoEmptyArgs` even
clears the empty arguments node). A no-argument *window* function parsed in
that context — e.g. `CODEC(cume_dist() OVER (...))` — was stripped of its
`()` too, but it must keep them: `cume_dist OVER (...)` re-parses as
`cume_dist() OVER (...)`, so the formatting did not survive a parse
round-trip and tripped the `Inconsistent AST formatting` logical error
(found by the AST fuzzer, STID 1941-26fa).

Always print a window function's parentheses in `ASTFunction` formatting.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Jun 18, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [20d1edb]

Summary: ✅


AI Review

Summary

This PR fixes ASTFunction formatting for zero-argument window functions that were parsed through optional-parameter contexts such as CODEC or table engines. The formatter now preserves the required empty argument list before OVER, while the added stateless test covers the round-trip failure and checks that ordinary zero-argument codecs and engines still omit parentheses. I found no unresolved correctness, compatibility, or test-coverage issues.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jun 18, 2026
@alexey-milovidov

Copy link
Copy Markdown
Member Author

Continued via /continue-pr.

Re-merged master (the branch was 736 commits behind and red). The merge is clean — master did not touch src/Parsers/ASTFunction.cpp, so the effective diff is still exactly the three PR files. Pushed 31f1566fffd..5be993f5fbe.

Both prior CI failures are unrelated to this change, which only affects ASTFunction window-function formatting:

@groeneai, the stress failure above is an unrelated, pre-existing fuzzer-found logical error — could you route/investigate it in a separate PR? The reproducer query is above and the CIDB link is in the CI report.

@groeneai

Copy link
Copy Markdown
Collaborator

@alexey-milovidov Confirmed: STID 2672-39ba is the #91119 family (correlated scalar subquery under group_by_use_nulls + a grouping-set modifier), unrelated to this PR's ASTFunction formatting change. The dedicated fix is already in flight at #104350, so no separate PR is needed.

Reproduced on master HEAD (debug, build id 28d40078) with your exact query:

SET allow_experimental_correlated_subqueries = 1, group_by_use_nulls = 1;
SELECT number, number % 2, (SELECT sum(number))
FROM numbers(10) GROUP BY number % 2, number WITH CUBE
ORDER BY number, number % 2, sum(number);
Logical error: 'Bad cast from type DB::ColumnNullable to DB::ColumnVector<unsigned long>'
6. AggregateFunctionSum<UInt64>::add            (assert_cast.h:43)
7. IAggregateFunctionHelper<...>::addBatch      (IAggregateFunction.h:527)
8. DB::Aggregator::executeAggregateInstructions (Aggregator.cpp:1429)

Exact match for the stress stack.

Trigger isolated (both required, deterministic): drop group_by_use_nulls -> clean; drop WITH CUBE -> clean.

Root cause: under group_by_use_nulls = 1 a grouping-set modifier (CUBE/ROLLUP) promotes the outer GROUP BY key number to Nullable. The correlated (SELECT sum(number)) still resolves number at its non-Nullable type, so the inner sum receives a ColumnNullable where it asserts ColumnVector<UInt64>.

Fix: #104350 (QueryAnalyzer.applyGroupByUseNullsToExpression wraps the correlated reference in Nullable, modifier-agnostic across ROLLUP / CUBE / GROUPING SETS / TOTALS). Its regression test already covers the CUBE branch and the aggregate-over-correlated (SELECT sum(number)) branch separately; I will fold your exact CUBE + sum query in as an explicit case on the next review iteration. Your draft #100365 targets the same family.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=107806&sha=31f1566fffde53379c492ff14497c0a9b0a710e7&name_0=PR&name_1=Stress%20test%20%28arm_tsan%29

@alexey-milovidov

Copy link
Copy Markdown
Member Author

Continued via /continue-pr.

Re-merged master (the branch was 266 commits behind and red). The merge is clean — master has still not touched src/Parsers/ASTFunction.cpp (0 commits since the merge base), so the effective diff is still exactly the three PR files, byte-for-byte. Pushed 5be993f5fbe..20d1edb8fca. No local build (the formatting change is unchanged and already passed Fast test, Stateless tests, and the targeted AST fuzzer on the previous run).

The three CI reds on the previous run are all unrelated master-wide flakes / infra, none of which touch ASTFunction formatting:

  • Stress test (amd_asan_ubsan) — Hung check failed, possible deadlock found. This is a master-wide flake: 336 failures across 205 PRs in the last 7 days. The stack trace is in QueryAnalyzer::resolveFunction / IExecutableFunction::execute, with no path from window-function formatting.
  • Upgrade check (amd_release) — Download script failed (then Check failed downstream). An infra download flake (8 download failures across 8 PRs in 7 days).

The re-merge triggers a fresh CI run, which should clear these transient flakes.

AI Review is ✅ Approve, 0 unresolved review threads, changed-lines coverage 5/5 (100%).

@clickhouse-gh

clickhouse-gh Bot commented Jun 21, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.20% 85.30% +0.10%
Functions 92.50% 92.50% +0.00%
Branches 77.50% 77.50% +0.00%

Changed lines: Changed C/C++ lines covered by tests: 5/5 (100.00%) | Lost baseline coverage: none · Uncovered code

Full report · Diff report

@alexey-milovidov alexey-milovidov self-assigned this Jun 23, 2026

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok.

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Jun 23, 2026
Merged via the queue into master with commit d09aecd Jun 23, 2026
327 of 328 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix/inconsistent-ast-window-in-codec branch June 23, 2026 21:18
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Jun 23, 2026
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…-ast-window-in-codec

Fix inconsistent AST formatting for a no-argument window function in CODEC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-bugfix Pull request with bugfix, not backported by default pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants