Repository navigation
Cherry pick #113759 to 26.3: Fix segfault in expandOrderByAll for a Merge child query - #116328
Merged
robot-ch-test-poll4 merged 9 commits intoAug 27, 2026
Merged
Conversation
`removeJoin` strips the `ORDER BY` and `GROUP BY` clauses from the rewritten single-table query but left the `order_by_all`/`group_by_all` flags set. Re-analysis of that query for a child plan of `StorageMerge` then called `expandOrderByAll`, which dereferenced the null `orderBy()`. Reset both flags in `removeJoin` and turn the null dereference in `TreeRewriter` into an exception. Found by the targeted AST fuzzer: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=104510&sha=c265df97ecae25ad7a94813525aaedaa2b4deee2&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%2C%20targeted%2C%20old_compatibility%29 (PR: #104510) Closes: #113758 Co-Authored-By: Claude Fable 5 <[email protected]>
…r_by_all` reset Two review findings on the clause-removal invariant: - `removeJoin` left `INTERPOLATE` behind after removing `ORDER BY`. `RequiredSourceColumnsVisitor` traverses `select.interpolate()` independently of `orderBy()`, so a rewritten `Merge` child query could still reference columns of the removed joined table and fail with `UNKNOWN_IDENTIFIER`. Now the clause is removed together with `ORDER BY` (regression case added to the test). - Other writers also removed `ORDER BY` without clearing `order_by_all` (`StorageWindowView::getSourceTableSelectQuery`, `DuplicateOrderByVisitor`), leaving the same malformed state. The reset is now centralized in `ASTSelectQuery::setExpression`: removing the `ORDER_BY` expression clears `order_by_all`. This cannot be done for `group_by_all` because `GROUP BY ALL` is legitimately represented as the flag with no `GROUP BY` list until `expandGroupByAll` runs (the parser itself calls `setExpression(GROUP_BY, nullptr)` after setting the flag), so `removeJoin` and `StorageWindowView` reset it explicitly. `CREATE WINDOW VIEW ... ORDER BY ALL` with a `tumble(now(), ...)` window segfaulted before this change (verified on 26.7.1); it no longer does. A clean stateless test for it is blocked by the unrelated pre-existing #113493. Co-Authored-By: Claude Fable 5 <[email protected]>
…rewrites `ORDER BY` `StorageWindowView::getSourceTableSelectQuery` replaces (or removes) the `ORDER BY` clause while rewriting the POPULATE source query, but left a stale `INTERPOLATE` list attached. `RequiredSourceColumnsVisitor` walks `INTERPOLATE` independently of `ORDER BY`, so its expressions could pull aliases of the removed select list into the source query (`UNKNOWN_IDENTIFIER`), and the result was a parser-impossible AST shape (`INTERPOLATE` without `WITH FILL`).
Address review: `removeJoin` and the mirrored non-join branch of `StorageWindowView::getSourceTableSelectQuery` cleared `group_by_all` but left `group_by_with_totals`, `group_by_with_rollup`, `group_by_with_cube`, and `group_by_with_grouping_sets` set after deleting the GROUP BY clause. A stale modifier flag on the rewritten aggregation-free query is a malformed internal AST: `ASTSelectQuery::formatImpl` still emits `WITH TOTALS`/`ROLLUP`/`CUBE` without a GROUP BY, and `InterpreterSelectQuery` rejects that shape with "WITH TOTALS, ROLLUP, CUBE or GROUPING SETS are not supported without aggregation" when the child query is analyzed past the aggregation stage. Regression cases with `WITH TOTALS`/`WITH ROLLUP`/`WITH CUBE`/`GROUPING SETS` on the old-analyzer `merge()` path are appended to the test (all four segfault on 26.7.1 via the original `expandOrderByAll` bug). Co-Authored-By: Claude Fable 5 <[email protected]>
`removeJoin` rewrites the select list of a `Merge`/`Distributed` child query down to left-table columns, but left the `WINDOW` and `LIMIT BY` clauses attached. The old analyzer resolves both unconditionally (`ExpressionAnalyzer::makeWindowDescriptions` and `appendLimitBy`), so a query such as `... NATURAL JOIN r WINDOW w AS (PARTITION BY r.t)` or `... LIMIT 1 BY r.t` still failed with `UNKNOWN_IDENTIFIER` after the join was stripped. Both are now cleared together with the other post-join decorations, along with `QUALIFY`, the `limit_by_all` flag, and the `limit_with_ties` flag (meaningless without the removed `ORDER BY`). The non-join rewrite in `StorageWindowView::getSourceTableSelectQuery` gets the same cleanup. Both `WINDOW` and `LIMIT BY` failures reproduce on a stock 26.7.1 binary and pass with the fix; regression cases added to the test.
…rge-join # Conflicts: # src/Interpreters/getHeaderForProcessingStage.cpp
Fix segfault in `expandOrderByAll` for a `Merge` child query
…ypick/26.3/113759 # Conflicts: # src/Interpreters/getHeaderForProcessingStage.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Original pull-request #113759
Do not merge this PR manually
This pull-request is a first step of an automated backporting.
It contains changes similar to calling
git cherry-picklocally.If you intend to continue backporting the changes, then resolve all conflicts if any.
Otherwise, if you do not want to backport them, then just close this pull-request.
The check results does not matter at this step - you can safely ignore them.
Troubleshooting
If the conflicts were resolved in a wrong way
If this cherry-pick PR is completely screwed by a wrong conflicts resolution, and you want to recreate it:
pr-cherrypicklabel from the PRYou also need to check the Original pull-request for
pr-backports-createdlabel, and delete if it's presented thereThe PR source
The PR is created in the CI job