Skip to content

Fix query parameter substitution inside WINDOW clause definitions - #110506

Merged
alexey-milovidov merged 7 commits into
masterfrom
fix/window-clause-query-parameters
Jul 18, 2026
Merged

alexey-milovidov merged 7 commits into
masterfrom
fix/window-clause-query-parameters

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Jul 15, 2026 •

Copy link
Copy Markdown
Member

Related: #100173

ParserWindowList stored the parsed window definition in the definition member of ASTWindowListElement but did not attach it to children (only clone did), so generic AST visitors never descended into the definitions of a named WINDOW clause. In particular, ReplaceQueryParameterVisitor did not substitute query parameters there:

  • with the parameter set, the placeholder silently remained unsubstituted (e.g. SELECT count() OVER w WINDOW w AS (PARTITION BY {p:String}) did not see the value);
  • with an unset Identifier parameter, an ASTIdentifier with an empty name part leaked into the query tree, and the EXPLAIN SYNTAX round trip via IdentifierNode::toASTImpl failed the chassert(!part.empty()) in the ASTIdentifier constructor with the exception Logical error: '!part.empty()' in debug builds.

The inline OVER (...) path already attached the definition to the children of ASTFunction, so it was not affected; now the named WINDOW clause is consistent with it: parameters are substituted, and an unset parameter throws UNKNOWN_QUERY_PARAMETER instead of being silently kept.

The exception was found by the AST fuzzer (STID 2508-2ef2) and currently hits dozens of unrelated PRs (about 25 in the last 30 days); I did not find an existing tracking issue for it. CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=100173&sha=b1e22dc973fff17a0d52660a132ef3f7a1d45498&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%2C%20targeted%29

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):

Fix substitution of query parameters inside the definitions of a named WINDOW clause. Previously, parameters there were silently kept unsubstituted, and an unset Identifier parameter could lead to the exception Logical error: '!part.empty()' during EXPLAIN SYNTAX.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.7.1.1157 (included in 26.7 and later)

`ParserWindowList` stored the parsed window definition in the
`definition` member of `ASTWindowListElement` but did not attach it to
`children` (only `clone` did), so generic AST visitors never descended
into the definitions of a named `WINDOW` clause. In particular,
`ReplaceQueryParameterVisitor` did not substitute query parameters
there: with a parameter set, the placeholder silently remained; with an
unset `Identifier` parameter, an `ASTIdentifier` with an empty name
part leaked into the query tree, and the `EXPLAIN SYNTAX` round trip
via `IdentifierNode::toASTImpl` failed the `chassert(!part.empty())`
in the `ASTIdentifier` constructor with the exception
`Logical error: '!part.empty()'` in debug builds (found by AST fuzzer,
STID 2508-2ef2, hitting dozens of unrelated PRs).

The inline `OVER (...)` path already attached the definition to the
children of `ASTFunction`; now the named `WINDOW` clause is consistent
with it: parameters are substituted, and an unset parameter throws
`UNKNOWN_QUERY_PARAMETER` instead of being silently kept.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=100173&sha=b1e22dc973fff17a0d52660a132ef3f7a1d45498&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%2C%20targeted%29
Found on: #100173

Co-Authored-By: Claude Fable 5 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Jul 15, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [3764123]

Summary: ✅


AI Review

Summary

This PR fixes the missing AST-child link for named WINDOW clause definitions so generic AST visitors can see and rewrite query parameters there, and it adds focused regressions for direct SELECT / EXPLAIN use plus the parameterized CREATE VIEW path. I did not find any remaining correctness, compatibility, or PR-metadata issues in the current diff, and the previously raised review point about end-to-end parameterized view coverage is addressed by the added test.

Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jul 15, 2026
alexey-milovidov and others added 4 commits July 17, 2026 06:49
…egression

The AI review and the `clickhouse-gh` review thread asked to prove the
view-DDL path end to end, not just direct `SELECT` / `EXPLAIN` substitution.
The same missing child that hid query parameters under a named `WINDOW`
from `ReplaceQueryParameterVisitor` also hid them from
`ASTSelectQuery::hasQueryParameters`, which `ASTCreateQuery::isParameterizedView`
relies on to classify a view as parameterized.

Add a case that creates a view whose only query parameter lives inside a
named `WINDOW` frame definition, checks it is classified as parameterized
via `system.tables.parameterized_view_parameters`, and calls it with an
argument to confirm the parameter is substituted at call time.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Jul 18, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.10% 86.10% +0.00%
Functions 91.80% 91.80% +0.00%
Branches 78.20% 78.20% +0.00%

Changed lines: Changed C/C++ lines covered: 2/3 (66.67%) · Uncovered code

Full report · Diff report

@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.

Very good fix!

@alexey-milovidov alexey-milovidov self-assigned this Jul 18, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Jul 18, 2026
Merged via the queue into master with commit 6b5099c Jul 18, 2026
178 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix/window-clause-query-parameters branch July 18, 2026 18:14
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 18, 2026
@clickgapai

Copy link
Copy Markdown
Contributor

Hi — this looks like a backport candidate for 26.1, 26.2, 26.3, 26.4, 26.5, 26.6. I ran this PR's own regression test against each branch's binary and the bug reproduces there (the fix isn't present yet).

pull Bot pushed a commit to peijunlin2008/ClickHouse that referenced this pull request Sep 7, 2026
ORDER BY x WITH FILL INTERPOLATE (value AS {p:Int64}) failed with
UNKNOWN_QUERY_PARAMETER even when the parameter was supplied, while the
same parameter nested in a larger expression, such as value + {p:Int64},
worked.

ASTInterpolateElement stores its expression twice: in the member expr and
in children[0]. The parser, clone() and readJSON() all establish that
duplication. ReplaceQueryParameterVisitor does not mutate a parameter node
in place, it reassigns the ASTPtr slot it was handed, so visitChildren
afterwards asks the parent to retarget any duplicate member through
IAST::updatePointerToChild, which dispatches to the virtual
forEachPointerToChild. The base implementation of that hook is empty and
ASTInterpolateElement never overrode it, so the retarget did nothing:
children[0] received the substituted literal while expr kept pointing at
the released ASTQueryParameter. Every consumer of the clause reads ->expr,
which is why the query failed under both the old and the new analyzer.
The compound form was unaffected because expr then points at the plus
function, whose identity survives while its children are replaced in
place, and {p:Identifier} was unaffected because visitIdentifier mutates
that node in place.

Add the missing forEachPointerToChild override so the member follows the
child, matching the other AST classes that implement this hook. Because
the divergence is upstream of both analyzers, the single override fixes
both. This is a different mechanism from the WINDOW clause fix in ClickHouse#110506,
where the parser had never registered the child at all.

Closes ClickHouse#118458.

Co-Authored-By: Claude Opus 5 <[email protected]>
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…e-query-parameters

Fix query parameter substitution inside `WINDOW` clause definitions
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.

ASTWindowListElement::definition not in children during parsing — query parameters in named WINDOW definitions are not substituted

3 participants