Repository navigation
Cherry pick #105470 to 25.8: Fix segfault in uniqStateOrNull with WITH ROLLUP/CUBE/TOTALS - #105869
Merged
Merged
Conversation
`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`
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 #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-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