Skip to content

Cherry pick #131789 to 26.3: Check the aggregates of deserialized query plan steps against their input - #133173

Merged
robot-ch-test-poll merged 17 commits into
backport/26.3/131789from
cherrypick/26.3/131789
Nov 20, 2026
Merged

robot-ch-test-poll merged 17 commits into
backport/26.3/131789from
cherrypick/26.3/131789

Conversation

@robot-ch-test-poll

Copy link
Copy Markdown
Contributor

Original pull-request #131789

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.

Before you resolve anything

Conflicts are often caused by a prerequisite change that has not been backported yet, rather than by a real divergence. The bot re-tries this cherry-pick against the release branch on every run, so if that is the case here it will merge itself as soon as the prerequisite lands, and you will see a comment saying so. Manual resolution is only needed while the conflict persists.

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 16 commits November 15, 2026 12:46
…nput

The aggregate functions of a deserialized `Aggregating` step are resolved from the argument types written
in the plan, but the `Aggregator` executes them on the input columns found by name. A plan with other
types (or a missing column) made a function interpret a column of another type. Likewise, the steps that
merge states (`Aggregating` with `only_merge`, `MergingAggregated`, `Rollup`, `Cube`) merged the input
columns with the functions from the plan.

`checkDeserializedAggregatesMatchHeader` refuses such plans with `INCORRECT_DATA`: arguments must be in
the input with the types the function is resolved for (`LowCardinality` aside, which the factory and the
`Aggregator` both remove), and merged columns must be states of the same representation.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Address review: the `Aggregator` looks up the types of the keys and the
states by name, but merges the columns by position (`makeAggregateColumnsData`
reads `columns[keys_size + i]`). A plan whose input had the right states in
another order, or that repeated an aggregate, passed the by-name check and
merged a state with the function of another one.

Now `checkDeserializedMergedAggregatesLayout` requires the input of the
merging steps (`Aggregating` with `only_merge`, `MergingAggregated`, `Rollup`,
`Cube`) to start with the keys and then the states, in the order of the plan,
with distinct names. For `MergingAggregated` with grouping sets, the layout is
checked per set as `MergingAggregatedTransform` reorders it. The merge-only
`Aggregating` with grouping sets, which the planner never produces, is refused.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
`Aggregator::Params::getHeader` builds the header of the merge-only steps from the whole input,
so a later column with the name of a key or a state would be there, but not in the merged chunks.
Addresses the review comment in #131789.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…s input

Address review: `TotalsHavingStep::deserialize` accepted `AggregateDescriptions`
without checking them against the input header, and `TotalsHavingTransform`
finalizes the columns named by them, assuming they are states. Now every
aggregate must name an input column holding a state of the same
representation (`checkDeserializedFinalizedAggregates`), otherwise the plan is
refused with `INCORRECT_DATA`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…d` step

With grouping sets, `MergingAggregatedTransform` reads `__grouping_set` as `ColumnUInt64` and the output
header takes its type from the input. A forged plan with another type or several such columns was accepted
and then hit an exception in `MergingAggregatedTransform::addChunk`, or produced chunks that disagreed with
the output header. Require exactly one `UInt64` `__grouping_set` column during deserialization.

#131789 (comment)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ated` step without grouping sets

`appendGroupingIfNeeded` copies a `__grouping_set` column of the input into the output header, but the merge without grouping sets does not produce it, so the chunks would not match the header.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… and distinct aggregate names

- `Rollup` and `Cube` always finalize their states and address the whole input with a mask built
  for the keys and the states only, and finalize a single input chunk with the functions of its
  columns. `checkDeserializedGroupByModifierInput` now requires `final`, an input of exactly the keys
  and the states, and states whose functions finalize to the result types of the plan (functions
  with the same state representation, like `quantile` and `quantiles`, may finalize differently).
- The merge-only `Aggregating` step requires an input of exactly the keys and the states, because
  `AggregatingInOrderTransform` addresses the whole input with the mask too.
- The ordinary `Aggregating` step requires distinct key and aggregate names, because the
  finalization finds the states by name.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… sort descriptions

The merge-only `Aggregating` step (also in order) finalizes the states with the functions of its input,
while its header takes the result types from the plan, so require them to agree, as for `Rollup` and `Cube`.

`AggregatingInOrderTransform` takes the first columns of `group_by_sort_description` as the ones the input is
sorted by and compares the key columns at their positions, so require `sort_description_for_merging` to be
a prefix of `group_by_sort_description`, and the latter to name only keys.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
`DataTypeAggregateFunction::equals` considers the states of functions with the same
state representation (like `quantile` and `quantiles`) equal, so a `Union` can pass
a column of `quantile` states under a header of `quantiles` states. `finalizeChunk`
finalized it with the column's own function, producing `Float64` under an advertised
`Array(Float64)`. The header checks of the deserialized steps cannot see this, so
`finalizeChunk` now takes the expected finalized header and throws `ILLEGAL_COLUMN`
if a state finalizes to another type (as `checkAggregateStateCanBeFinalized` does for
`finalizeAggregation`). This covers `Rollup`, `Cube`, `TotalsHaving` and the in-order
aggregation.

The test now also reads the packets after the plan is built, so it sees the exceptions
of the execution. That exposed a `LOGICAL_ERROR` for a deserialized `MergingAggregated`
step with grouping sets and `distributed_aggregation_memory_efficient` (which is on by
default in the step settings): the planners never combine them, so such a plan is now
refused with `INCORRECT_DATA`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…d grouping-sets merge

A deserialized `MergingAggregated` step with grouping sets now refuses an input in which a key occurs more
than once (the output header and `makeCreatingMissingKeysForGroupingSetDAG` take keys by name), and the
memory-bound merge with grouping sets, which threw a `LOGICAL_ERROR` in `transformPipeline`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ent-types

Check the aggregates of deserialized query plan steps against their input
@robot-ch-test-poll robot-ch-test-poll 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 Nov 20, 2026
Adapt the backport of #131789 to 26.3:
- 26.3 has no serialization of `Rollup` and `Cube` steps, and no merge-only
  or in-order `Aggregating` step in a serialized plan, so their checks
  (`checkDeserializedGroupByModifierInput`, `checkDeserializedMergeOnlyInput`,
  the sort description checks) are dropped. The `finalizeChunk` check in the
  `Rollup` and `Cube` transforms is kept.
- `deserializeAggregateDescriptions` has no `max_type_complexity` on 26.3.
- `AggregateDescription.h` forward-declares `Block`.
- The test uses query plan serialization version 0 and `ReadFromTable` leaves
  reading from tables created by the test; the `Rollup`, `Cube`, merge-only
  and in-order cases are removed. The byte layout was checked against the
  `clickhouse/clickhouse-server:26.3` image: the well-formed plans are
  accepted there, and the broken ones hit `LOGICAL_ERROR` and other errors
  without the fix.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@robot-ch-test-poll
robot-ch-test-poll merged commit b8038cb into backport/26.3/131789 Nov 20, 2026
2 checks passed
@robot-ch-test-poll
robot-ch-test-poll deleted the cherrypick/26.3/131789 branch November 20, 2026 11:34
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.

2 participants