Skip to content

Sum the adaptive aggregation seal normalization assertion over the three arms - #115204

Merged
PedroTadim merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-04676-aggregate-normalization-assertion
Aug 18, 2026
Merged

PedroTadim merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-04676-aggregate-normalization-assertion

Conversation

@groeneai

@groeneai groeneai commented Aug 17, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #115034

Changelog category (leave one):

  • CI Fix or Improvement (changelog entry is not required)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

...

Description

04676_adaptive_aggregation_replicated_argument, which I added in #115034, is flaky: as of
2026-08-17 20:00 UTC it had failed 22 times across 22 unrelated pull requests against 1341 passes,
roughly 1.6%, and it is still firing. Every failure is one of three assertion lines flipping 1
to 0, for example +normalized nullable 0.
Sample report
(Stateless tests (amd_debug, distributed plan, s3 storage, parallel) on #108431).

The test asserted AdaptiveAggregationSealNormalizations > 0 separately for each of its three
argument representations. That counter fires in Aggregator::sealPendingChunks only when the
batches one producer buffered together disagree at an argument position, which needs two batches
buffered in one producer, both representations reachable, and one producer holding blocks of both
kinds. The test pins only the second; the others are a read pool decision and thread scheduling,
so a non-zero count for a single query is not an engine guarantee. Randomization is not the cause:
it also reproduces with randomization off and with enable_lazy_columns_replication 0.

This sums the counter over the three arms instead, which is the property the engine does
guarantee, and changes nothing else. The three queries stay separate, so each keeps its own value
comparison and its own abort site; the sealed chunks assertion, the six value lines and the tags
are untouched. Within one run the test no longer proves that every representation reached the
normalization. 04654 already asserts the sibling AdaptiveAggregationSealedChunks in aggregate.

Validation: in one interleaved loop the old form failed twice while the new form passed 300 of 300.
Of 600 runs, 6 had an arm at zero and all 6 still had a non-zero total. With #115034's fix reverted
the new form aborts 20 of 20 with Bad cast from type DB::ColumnReplicated to DB::ColumnVector in
sealPendingChunks. 50 runs with randomized settings and 50 without both pass.


Workflow [PR]
Sync PR [sync-upstream/pr/115204]

…ree arms

04676_adaptive_aggregation_replicated_argument asserted
AdaptiveAggregationSealNormalizations > 0 separately for each of its three
argument representations. That counter is incremented in
Aggregator::sealPendingChunks only when the batches one producer has buffered
together disagree in physical representation at that argument position, which
requires three things: at least two batches buffered in one producer, both
representations reachable, and one producer holding blocks of both classes.

The test pins the settings governing the second condition. The first depends on
the staged payload size, since stageChunk publishes a batch as-is above a byte
threshold and sealPendingChunks returns before either counter when only one mini
chunk is buffered. The third is a read pool and thread scheduling outcome. So a
non-zero count for a single query is not an engine guarantee, and the per-arm
assertion asserted a property the engine does not promise. It failed on about
1.9% of the runs the test appeared in, 19 times across 19 unrelated pull
requests in 14 days, always one line flipping 1 to 0.

Randomization is not the cause. The failure reproduces with randomized settings
disabled and with enable_lazy_columns_replication 0, the very setting the test
pins, so there is nothing to clamp and a no-random-settings tag would hide the
flake rather than address it.

Summing the counter over the three arms states the property that does hold: the
seal normalized somewhere in the query set. The three queries stay separate, so
each keeps its own value comparison and its own abort site, and the aggregate
sealed chunks assertion, the six value lines and the tags are unchanged. The
same LIKE predicate the sealed chunks assertion already uses is reused, and the
three log_comment values are kept so per-arm attribution remains available in
query_log.

What this gives up is the per-run guarantee that every representation exercised
the normalization. Across CI runs each arm still does in about 99.4% of runs,
and a binary without the normalization fix aborts on whichever position first
sees heterogeneity.

Validated locally on a debug build: the old form failed 2 of 300 runs in the
same interleaved loop where the new form passed 300 of 300; of 600 runs 6 had an
arm at zero and all 6 still had a non-zero total; with the normalization loop
reverted the new form aborts 20 of 20 with Bad cast from type
DB::ColumnReplicated to DB::ColumnVector in sealPendingChunks, so the regression
oracle still detects the original defect. 50 runs with randomized settings and
50 without both pass.

The test was added by ClickHouse#115034.
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review (click to expand)

An independent model reviewed this change cold against the diff, the surrounding aggregation
code and the PR contract, without access to the implementer's notes. One blocker was raised and
adjudicated below.

# Finding Verdict
❌ Pooling the three counters weakens the regression oracle: the pooled predicate passes when any one arm reports a normalization, so the other two could use homogeneous seals and their value comparisons would also pass. String and Nullable cast during prepareForSquashing while UInt128 casts later in ColumnVector::insertRangeFrom, so one arm cannot cover both failure classes. Disagreed. The counter is not an independent coverage meter; the increment and the fix are the same if block. AdaptiveAggregation.cpp:322-327 is if (!all_of(structureEquals)) { increment(SealNormalizations); for (source : sources) source = removeSpecialRepresentations(source); }, and the removeSpecialRepresentations loop is the fix from #115034. So "this arm would have incremented the counter" and "this arm entered the un-normalized squash" are the same condition, and the second one aborts (assert_cast throws in debug and sanitizer builds, which is what every parametrization carrying this test uses). An arm therefore cannot reach heterogeneity and pass quietly; an arm that never reaches it contributes nothing to either form of the assertion, and under the old form it simply flaked red. Confirmed empirically: with the normalization loop reverted and the counter left in place, the new form aborts 20 of 20, and the abort observed is the UInt128 numeric insertRangeFrom site, the arm the finding identifies as at risk. All three arm queries and all six value lines are byte-unchanged, so each arm keeps its own abort site and its own adaptive-vs-baseline comparison. What pooling gives up is the per-run coverage meter, which the description states explicitly. The suggested remedy, making the heterogeneous seal deterministic, needs a new failpoint: nothing the test can set controls which blocks one producer buffers.

Two accuracy issues found in my own review were corrected before publishing: the failure counts
in the description were re-measured at review time (22 failures across 22 pull requests, 1.61%,
still firing) rather than left at the implementer's earlier figures, and an invented per-arm
success percentage was replaced by the 04654 precedent, which is checkable in the tree.

Session id: cron:clickhouse-review-slot-49:20260817-202400

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes for the defect this oracle guards: with #115034's source fix reverted, the new test form aborts on demand, 20 of 20 runs, one command per run. The flake itself is probabilistic by nature and was reproduced at 3 failures / 300 runs (1.0%) plus 2 more in the interleaved window, matching CI's 1.9%.
b Root cause explained? Yes. AdaptiveAggregationSealNormalizations is incremented in Aggregator::sealPendingChunks only when the batches one producer buffered together disagree at an argument position. That needs at least two batches buffered in one producer, both representations reachable, and one producer holding blocks of both classes. The test pins only the second; the other two are a read pool decision and thread scheduling, so a per-query non-zero count is not an engine guarantee.
c Fix matches root cause? Yes. The oracle is restated as the invariant the engine actually guarantees, by summing the counter over the three arms. Not a widened bound, not a no-random-* tag, not a data reduction, not >= 0.
d Test intent preserved / new tests added? Preserved. The six value lines (adaptive vs baseline for String, UInt128, Nullable(UInt64)), the aggregate sealed chunks assertion, the long, no-parallel-replicas tags and both DROP TABLEs are untouched. This test IS the regression test for #115034 and item (e) shows it still catches that bug. Coverage given up: within one run it no longer proves that every representation reached the normalization.
e Both directions demonstrated? Yes, three ways. In one interleaved runner invocation the old per-arm form failed 2 of 300 while the new form passed 300 of 300. Over 600 runs, 6 had an arm at zero and all 6 had a non-zero total (minimum 4), read per run from system.query_log - the exact state that used to red. With #115034's source fix reverted, the new form aborts 20 of 20 in sealPendingChunks.
f Fix is general across code paths? Yes. AdaptiveAggregationSealNormalizations has exactly one consumer in the tree (this test). Its sibling AdaptiveAggregationSealedChunks is already asserted in aggregate by both consumers (this test and 04654_adaptive_aggregator_profile_events) and never flipped, so no other carrier needs the change. ProfileEvents['Adaptive matches only those two tests.
g Fix generalizes across inputs (params/datatypes/wrappers)? Yes. The three arms deliberately span String, UInt128 and Nullable(UInt64), covering both abort sites (prepareForSquashing and the numeric insertRangeFrom). All three keep their own value assertions; only the liveness claim is summed, and coalesce(..., 0) still handles the no-rows case.
h Backward compatible? N/A - test-only change, no src/ diff. No SettingsChangesHistory.cpp row, no serialization or format change, no setting default moved. This test and src/Interpreters/AdaptiveAggregation.cpp exist only on master (#111459, #115034), so no backport applies.
i Invariants and contracts preserved? Yes. The LIKE '04676_seal_%' predicate is the same one the adjacent sealed chunks assertion already uses, and the three log_comment values are unchanged, so per-arm attribution stays available in query_log for debugging. Statement order, the SYSTEM FLUSH LOGS query_log placement and the DROP symmetry are unchanged.

Session id: cron:clickhouse-impl-slot-7:20260817-192500

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @nihalzp, could you review this? It is a test-only change: the adaptive aggregation seal test asserted the normalization counter separately for each of its three argument representations, but that counter only fires when the batches one producer happens to buffer together disagree, so the per-arm form flaked on about 1.6% of runs. This sums the counter over the three arms instead.

@clickhouse-gh

clickhouse-gh Bot commented Aug 17, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [2772f8a]

Summary: ✅


AI Review

Summary

This PR changes 04676_adaptive_aggregation_replicated_argument so AdaptiveAggregationSealNormalizations is asserted in aggregate across the three adaptive-query arms instead of per arm. After checking the test, the surrounding sealPendingChunks implementation, the prior PR discussion, and the current PR diff, I did not find a concrete correctness or regression-coverage bug in the patch itself: the value comparisons stay per representation, the aggregate sealed chunks oracle is unchanged, and the pooled normalization assertion now matches the event contract the engine actually exposes.

Final Verdict

✅ No new blocking findings.

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Aug 17, 2026
@nihalzp nihalzp added the can be tested Allows running workflows for external contributors label Aug 18, 2026
@PedroTadim PedroTadim self-assigned this Aug 18, 2026
@PedroTadim
PedroTadim enabled auto-merge August 18, 2026 07:22
@PedroTadim
PedroTadim added this pull request to the merge queue Aug 18, 2026
Merged via the queue into ClickHouse:master with commit b87ee15 Aug 18, 2026
537 of 541 checks passed
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 18, 2026
@groeneai groeneai added the groeneai-origin-ci-master PR origin: master/nightly CI monitoring finding label Aug 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors groeneai-origin-ci-master PR origin: master/nightly CI monitoring finding pr-ci pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants