Skip to content

Cherry pick #105470 to 26.3: Fix segfault in uniqStateOrNull with WITH ROLLUP/CUBE/TOTALS - #105871

Merged
robot-clickhouse merged 7 commits into
backport/26.3/105470from
cherrypick/26.3/105470
May 26, 2026
Merged

robot-clickhouse merged 7 commits into
backport/26.3/105470from
cherrypick/26.3/105470

Conversation

@robot-clickhouse

Copy link
Copy Markdown
Member

Original pull-request #105470

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

groeneai and others added 7 commits May 20, 2026 19:43
`SELECT uniqStateOrNull(h) FROM t GROUP BY h WITH ROLLUP` (and the
`WITH CUBE` / `WITH TOTALS` and `*OrDefault` variants) segfault the
server: the synthesised "all-grouped-up" row's nested `UniquesHashSet`
ends up with a `nullptr` `buf` member, and the subsequent
serialization loop dereferences it.

`UniquesHashSet::write` now bails out and emits an empty state when
`buf` is null, instead of trying to scan a zero-size buffer.

Closes #105462
The flag-set branch of `AggregateFunctionOrFill::insertResultIntoImpl`
pushes the inner aggregate state pointer into the result
`ColumnAggregateFunction` as an external reference; the result column's
`src` field keeps the source column alive so the source owns the
destruction of those states.

The flag-unset branch previously called `to.insertDefault()`. For
`State`-nested combinators that runs `ColumnAggregateFunction::insertDefault`
on the inner column, which calls `ensureOwnership()` and resets `src` to
null. Any later flag-set row then pushes an external pointer into a
column with no `src` protection, so both this column and the source
column destroy the same aggregate state at pipeline tear-down.

For `UniquesHashSet` the double-destroy surfaces under `MemorySanitizer`
as `use-of-uninitialized-value` reading `buf` inside `~UniquesHashSet`
(STID 8424-6802 on the regression test added in this PR for
`uniqStateOrNull`/`uniqStateOrDefault`/`uniqOrNullState` with `ROLLUP`,
`CUBE`, and `TOTALS`).

Mirror the flag-set branch for `State`-nested combinators: forward the
already-default-initialized inner state through `nested_function->insertResultInto`
and push `1` to the null map. This keeps the column's `src` set for the
entire `convertToValues` loop and avoids the double-destroy.

Verified locally with 50 randomized runs of
`04262_105462_uniq_state_or_null_with_rollup`.

Closes #105462
Two changes in response to #105470 (review):

1. Replace the silent `if (!buf) bail` in `UniquesHashSet::write` with
`chassert(buf)`. The `AggregateFunctionOrFill::insertResultIntoImpl`
change in commit 4187aa5 establishes the invariant that the source
state's `buf` is never null at serialise time. The previous bail-out
silently masked corruption rather than surfacing it; the assertion now
documents the invariant and fails loudly in debug / sanitiser builds if
it is ever broken again.

2. Add new regression test `04273_105462_uniq_state_or_null_rollup_consumers`
covering consumer paths not exercised by the original
`04262_105462_uniq_state_or_null_with_rollup` test. The original test
only reaches the `serialize`-driven final-projection path. Algunenano
pointed out four additional consumers that all relied on the same
corrupted `UniquesHashSet`:

- `<func>Merge` over the rollup-produced state column. Reaches
  `UniquesHashSet::merge` via `AggregateFunctionMerge::add`.
- `INSERT INTO AggregatingMergeTree`. Persists the rollup-produced state
  via `ColumnAggregateFunction::insertMergeFrom` ->
  `AggregatingSortedAlgorithm::AggregatingMergedData::addRow` ->
  `MergeTreeDataWriter::mergeBlock`.
- Two-level + multi-thread aggregator scheduling variants.
- `Memory` engine source confirms the regression is pipeline-level
  rather than `MergeTree`-specific.

The test also mirrors the first reproducer for `uniqStateOrDefault`,
since #99839 enabled the `*StateOrDefault` chain in the same way as
`*StateOrNull`.

Verified locally:
- Without the `OrFill` fix (binary from a sibling branch lacking commit
  4187aa5), reproducer 1 segfaults at `UniquesHashSet::merge:453` (stack
  trace captured during this work).
- With the `OrFill` fix, 100/100 randomised runs of the new test pass,
  50/50 randomised runs of `04262` still pass, and the AggregatingMergeTree
  reproducer is asserted via `uniqMerge(s)` rather than `count()` so the
  assertion is deterministic across `enable_parallel_replicas` and
  `max_insert_threads` randomisation.
`UniquesHashSet.h`: the `chassert(buf)` comment now describes the
invariant violation at the call site only, without referencing other
parts of the PR.

Test file: renamed to `04273_uniq_state_or_null_rollup_consumers.{sql,reference}`,
dropping the `_105462_` issue-number infix.

Test header and inline comments: rewritten to describe the consumer
paths being exercised (`Merge`, `AggregatingMergeTree` insert,
two-level / multi-threaded aggregator, `Memory` engine). Removed
references to the other test file and to the bug history.

Verified: 15/15 PASS for the renamed test and the existing `04262`
test against the rebuilt PR-branch binary.
Per @Algunenano on #105470, drop the `_105462_` issue-number infix from
the test filename to follow the standard `<number>_<name>` convention
(same shape as the `04273_105462_*` -> `04273_*` rename in `f5f1eb45a9a6`).

Pure metadata change: file contents unchanged.

Co-Authored-By: Claude Opus 4.7 <[email protected]>
…eornull-rollup

Fix segfault in `uniqStateOrNull` with `WITH ROLLUP`/`CUBE`/`TOTALS`
@robot-clickhouse robot-clickhouse 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-bugfix Pull request with bugfix, not backported by default labels May 26, 2026
@robot-clickhouse
robot-clickhouse merged commit 88e3ae7 into backport/26.3/105470 May 26, 2026
@robot-clickhouse
robot-clickhouse deleted the cherrypick/26.3/105470 branch May 26, 2026 15:19
@robot-clickhouse robot-clickhouse added do not test disable testing on pull request pr-bugfix Pull request with bugfix, not backported by default pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! labels May 26, 2026
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-bugfix Pull request with bugfix, not backported by default pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants