Skip to content

Cherry pick #113759 to 26.7: Fix segfault in expandOrderByAll for a Merge child query - #116331

Merged
robot-ch-test-poll4 merged 9 commits into
backport/26.7/113759from
cherrypick/26.7/113759
Aug 27, 2026
Merged

robot-ch-test-poll4 merged 9 commits into
backport/26.7/113759from
cherrypick/26.7/113759

Conversation

@robot-clickhouse-ci-1

Copy link
Copy Markdown
Contributor

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-pick locally.
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:

  • delete the pr-cherrypick label from the PR
  • delete this branch from the repository

You also need to check the Original pull-request for pr-backports-created label, and delete if it's presented there

The PR source

The PR is created in the CI job

alexey-milovidov and others added 8 commits August 7, 2026 04:10
`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
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! do not test disable testing on pull request pr-critical-bugfix labels Aug 25, 2026
…ypick/26.7/113759

# Conflicts:
#	src/Interpreters/getHeaderForProcessingStage.cpp
@robot-ch-test-poll4
robot-ch-test-poll4 merged commit d28e9cb into backport/26.7/113759 Aug 27, 2026
2 checks passed
@robot-ch-test-poll4
robot-ch-test-poll4 deleted the cherrypick/26.7/113759 branch August 27, 2026 17:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do not test disable testing on pull request pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! pr-critical-bugfix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants