Repository navigation
Do not allocate a whole-block flag buffer per group in -If aggregation over a Nullable argument - #122505
Conversation
…ion over a `Nullable` argument The `-If` null adapters merged the null map with the condition into a buffer sized by the absolute end row of the call and filled only `[row_begin, row_end)`. Aggregation in order calls `addBatchSinglePlace` once per key interval, and window frames once per frame update, so with 8-row groups every call asked for up to 64 KiB to use 8 bytes. The merges the nested functions do for themselves (`SingleValueData`, `groupBit*`, the simple moments, `regr_*`, `timeSeriesLastTwoSamples`) were sized the same way. On x86_64 every such request above 32 KiB exceeds jemalloc's thread cache limit (`opt_tcache_max`) and takes the arena large-extent path, whose cost depends on the state of the process. On aarch64 (64 KiB pages) these sizes are small size classes served from the thread cache. That is why queries ClickHouse#2 to ClickHouse#5 of `tests/performance/aggregation_in_order_if_null_map.xml` are unstable on the amd runners only: in the last 14 days CIDB has 2008 of the test's 2174 amd `unstable` verdicts on ClickHouse#2 to ClickHouse#5, and 8 on arm in total. With a single `Nullable` argument no merged map is needed: `addBatchSinglePlaceNotNull` takes the null map and the condition column separately, and every implementation handles `if_argument_pos` (`AggregateFunctionIf` already calls them all that way). So `AggregateFunctionIfNullUnary` with a non-`Nullable` condition passes the value's null map and the condition at position 1, and `AggregateFunctionIfNullVariadic` with exactly one `Nullable` argument passes that argument's null map to the nested `-If` function, which applies the condition. The merges those calls reach allocate `row_end - row_begin` bytes and call their kernels with pointers rebased by `row_begin`, as `sum` and `avg` already do. The same rows are added in the same order, so the results do not change. Two or more `Nullable` arguments (query ClickHouse#5, `corr(x, w)`), a `Nullable` condition of a one-argument `-If` function and the TimeSeries array merges keep their merged buffer. Measured on a release build (x86_64, `max_threads` 8), base / this change, per run of each query of the test: - jemalloc large allocations above 32 KiB: #0 703 / 703, ClickHouse#1 714 / 714, ClickHouse#2 302182 / 703, ClickHouse#3 302142 / 663, ClickHouse#4 229046 / 1718, ClickHouse#5 115413 / 115413; - CPU profiler samples in the jemalloc large-extent path: ClickHouse#2 33% / 0.9%, ClickHouse#3 31% / 0.5%; - perf.py median of 13 runs, two alternating rounds (ms): ClickHouse#2 38.2, 39.8 / 24.6, 21.7; ClickHouse#3 39.0, 40.2 / 21.3, 19.8; ClickHouse#4 44.3, 45.4 / 28.3, 25.9; #0, ClickHouse#1 and ClickHouse#5 unchanged; - `minIf(x, cond) OVER (ORDER BY k ROWS BETWEEN 100 PRECEDING AND CURRENT ROW)` over 1M rows: 340 / 166 ms, and `sumIf(y, cn)` over a `Nullable` condition: 301 / 89 ms; - aggregation without keys over the same table: 12.2 / 12.3 ms. Query #0, flagged on ClickHouse#118490, never had such a buffer and is not changed here. CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=118490&sha=8ded98e22889e53dd536b141b564e136c76482a9&name_0=PR&name_1=Performance%20Comparison%20(amd_release%2C%20master_head%2C%203%2F6) Co-Authored-By: Claude Opus 5.5 <[email protected]>
…tion over `Nullable` arguments With one `Nullable` argument the `-If` null adapters now pass the null map and the condition column separately (previous commit) and rely on the nested function honouring `if_argument_pos`. `AggregateFunctionIf` did not: its `addBatchSinglePlaceNotNull` (`AggregateFunctionIf.h`) took the position as an unnamed parameter and passed its own condition, `num_arguments - 1`, instead. `-OrNull` and `-OrDefault` forward the position into their nested function, and the factory accepts `-If` under `-OrNull`/`-OrDefault` under `-If`, so in `sumIfOrNullIf(x, c1, c2)` with a `Nullable` `x`, and in `countIfOrNullIf(c, c2)` with a `Nullable(UInt8)` `c`, the inner `-If` dropped `c2` without keys, with aggregation in order and in window frames. Before the previous commit the adapters folded `!c2` into the merged null map, so the replaced position did no harm there. When a position is passed in, `AggregateFunctionIf::addBatchSinglePlaceNotNull` now adds the rows that are not NULL and pass that condition one by one through its own `add`, which checks its own condition. Without one it calls the nested function as before. `clickhouse local`, a `Memory` table (x Nullable(Float64), c Nullable(UInt8), c1 UInt8, c2 UInt8) with the rows (1, 1, 1, 1), (10, 1, 1, 0) and (100, 0, 0, 1): `SELECT sumIfOrNullIf(x, c1, c2)` and `SELECT countIfOrNullIf(c, c2)` print 1 and 1 on master, 11 and 2 with the previous commit, and 1 and 1 now. The test now covers both chains with an outer condition that is independent of the inner one, and fails without this change. The adapters check for a row to add with the existing `countBytesInFilterWithNull` instead of a new scalar loop. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 1 finding in round 1, none on the fix round) plus my own cold review (5 findings in round 1, 1 on the fix round).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-8:20260927-040700 |
|
Workflow [PR], commit [111f0e8] Summary: ❌
AI ReviewSummaryThis PR changes the Final Verdict
LLVM Coverage ReportMeasured on commit 111f0e8.
Changed lines: Changed C/C++ lines covered: 89/92 (96.74%) · Uncovered code |
|
📊 Cloud Performance Report ✅ AI verdict: no significant changes detected. K_source=6 K_base=30 flagged=0/65 clickbench🟢 No significant changes tpch_adapted_1_official🟢 No significant changes Debug info
|
CI finish ledger - 57ce4faEvery failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
All 169 check runs completed (153 pass, 14 not selected, 2 red), Session id: cron:our-pr-ci-monitor:20260927-130124 |
| FROM numbers(10000); | ||
|
|
||
| SET optimize_aggregation_in_order = 1; | ||
| SELECT count() > 0 FROM (EXPLAIN PIPELINE SELECT k, argMaxIf(y, x, cond) FROM t_agg_if_one_null_map GROUP BY k) WHERE explain ILIKE '%AggregatingInOrderTransform%'; |
There was a problem hiding this comment.
I don't understand the need of this test. Please remove this part and only include a query or queries that exercise this part of the code. No need for extra checks based on EXPLAIN and no need for TEMPORARY TABLES
There was a problem hiding this comment.
Thanks, done in 111f0e8: I removed the EXPLAIN checks, the temporary tables and the comparison queries. The test now only runs the queries that reach the changed code (aggregation in order, the no-key sumIfOrNullIf / countIfOrDefaultIf chain, window frames) and keeps their results in the reference. It still fails with the AggregateFunctionIf.h change reverted.
The test now runs aggregation in order, the no-key `-If` under `-OrNull` / `-OrDefault` under `-If` chain, and 101-row window frames, and records their values in the reference. The EXPLAIN checks, the temporary tables and the comparisons against hash aggregation and plain filters are removed. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Algunenano
left a comment
There was a problem hiding this comment.
The changes to src/AggregateFunctions/Combinators/AggregateFunctionIf.cpp aren't close to obvious.
The key part is that columns are indexed absolutely, same as null_map. We can only use spans when we call some inner function that accept it (like SingleValueData) and for that we need to reach the function with both columns, not combine them eagerly. Combining them eagerly makes sense mostly for vectorization, but for that you need to overload addBatchSinglePlaceNotNull and in that case you can merge the filters as you need (we already did). So in general it makes sense.
The alternative would be to change the API and make null_map an array where 0 is row_begin and ends in row_end, but I think that's even more confusing.
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units17 translation units recompiled, 75 s compile time in total, 17 of them have a recent master baseline. |
CI finish ledger - 111f0e8Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
146 of 146 check runs completed (131 passed, 14 not selected, 1 red: the fuzzer job above), Session id: cron:our-pr-ci-monitor:20260928-230115 |
5ece1cb
…ce test With groups of 8 rows about 75% of each query is per-group work (the group-end search of in-order aggregation, the `-If`/`-Null` adapters, state create and finalize). Its speed depends on how a build lays out these small hot functions: 73 of 2667 amd builds since 2026-10-26 run one of the six queries 16-35% slower than the rest of the same build, with the same instruction count and one more branch miss per group. That made the test fail on unrelated PRs (ClickHouse#120906: ClickHouse#3 +35%, #0 +27%) with a different query each time. Disable the six queries (their history keys stay) and append them on three new tables: `sumIfOrDefault`, `avgIfOrDefault` and `sumIf` on groups of 1024 rows, `corr` and `argMinIf` on groups of 128 rows and `minIf` on groups of 256 rows. `corr` has the smallest per-group penalty and needs the smaller groups to keep its ClickHouse#118025 guard (~1.10 at 1024 rows). `argMinIf` and `minIf` keep a guard of ClickHouse#122505: reverting its change to the variadic and unary `-If` null adapters slows them 1.21x and 1.28x, while the other `-If` queries move +0.9% to +13.2%. Measured locally with perf.py against the CI reference build: the ClickHouse#120906 build runs the old #0/ClickHouse#3 at 1.31x/1.39x and the new copies at 1.016x/1.020x in the same processes (`sumIf` 1.067x in another run); 7 slow CI builds stay within 6.7% on the new queries (A/A 2.5%), below the 15% minimum change threshold. Restoring the value-initialised `row_end`-sized buffer at each ClickHouse#118025 site master still reaches (`AggregateFunctionSum.h:669`, `AggregateFunctionAvg.h:539`, `SingleValueData.cpp:39`, `AggregateFunctionNull.h:769`) slows its queries 1.2x-1.8x, with identical results. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Related: #118490
Related: #118025
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
-Ifaggregate functions with oneNullableargument, such asminIf(x, cond)orsumIf(y, cond)with aNullablecondition, no longer allocate a buffer the size of the whole block for every group withoptimize_aggregation_in_orderor for every window frame. With small groups or frames this makes them 1.7 to 3.1 times faster on x86_64.Description
Queries #2 to #5 of
aggregation_in_order_if_null_map.xmlare noisy on amd runners only: over 10 days the two servers' times differ by over 15% in 62%, 60%, 49% and 22% of amd runs, never on arm. Example on master: Performance Comparison (amd_release, master_head, 2/6), where its two servers ran #2 in 96 and 64 ms. Found while investigating this job on #118490.The
-Ifnull adapters merged the null map and the condition into a buffer sized by the call's absolute end row. Aggregation in order calls them once per group and window functions once per frame, so every 8-row group asked for up to 64 KiB. On x86_64 a request above 32 KiB bypasses the jemalloc thread cache and takes the arena large-extent path; with aarch64's 64 KiB pages these sizes are cached small classes.With a single
Nullableargument the adapters now pass the null map and the condition to the nested function separately.-Ifnow applies such a condition passed down by-OrNullor-OrDefault(sumIfOrNullIf(x, c1, c2),Nullablex) instead of replacing it with its own; for a non-Nullablexit still does, as on master. The nested functions' own merges (min/max/argMin/argMax,groupBit*,varSampand other moments,regr_*,timeSeriesLastTwoSamples) are sized to the rows, assumandavgalready are. Two or moreNullablearguments (#5), aNullablecondition of a one-argument-If, and the TimeSeries array merges are unchanged. Results are identical.Release build, before / after:
perf.pymedians #2 37.1 / 22.0 ms, #3 39.9 / 20.0, #4 46.1 / 25.6; #0, #1 and #5 unchanged. 101-row window frames on 1M rows:minIf342 / 185 ms,sumIf(y, cn)313 / 103 ms.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/122505]
Version info
26.10.1.989-master(included in26.10and later)