Repository navigation
Weigh the adaptive aggregation's thaw by staged bytes and add the freeze-bytes settings - #115755
Conversation
|
Workflow [PR], commit [2e78fe2] Summary: ✅
AI ReviewSummaryThis PR makes adaptive Findings❌ Blockers
Final Verdict❌ Not ready to merge until the LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 210/211 (99.53%) · Uncovered code |
| {"enable_adaptive_aggregator", false, true, "New setting to enable adaptive `GROUP BY` aggregation that freezes each thread's local hash table once it reaches `adaptive_aggregator_freeze_threshold` keys, so frequent keys keep aggregating in the small local tables while rare keys are routed by their hash into per-bucket backlogs and aggregated exactly once, inside the bucket-parallel merge; this stores and processes rare keys once instead of once per thread."}, | ||
| {"adaptive_aggregator_freeze_threshold", 16384, 16384, "New setting to set the number of keys at which the adaptive aggregator (`enable_adaptive_aggregator`) freezes a thread's local hash table."}, | ||
| {"adaptive_aggregator_freeze_threshold_bytes", 4194304, 4194304, "New setting bounding the adaptive aggregator's frozen local tables in bytes, whichever of it and the key-count threshold is reached first; 0 disables the byte bound."}, | ||
| {"query_plan_aggregation_bucket_top_k", false, true, "New setting to toggle the plan optimization that materializes only each two-level bucket's best n groups when a final aggregation feeds ORDER BY over its outputs with LIMIT n and the per-bucket selection is provably exact."}, |
There was a problem hiding this comment.
These compatibility defaults do not match the pre-PR behavior.
adaptive_aggregator_freeze_threshold_bytesdid not exist before this change, so oldercompatibilityversions should see0, not4 MiB, otherwise they still opt into the new byte-triggered freeze.query_plan_aggregation_bucket_top_kis only a dedicated toggle for an existing rewrite that used to be always on underquery_plan_enable_optimizations, so oldercompatibilityversions should keep it enabled instead of flipping it tofalse.
As written, compatibility against an older version changes both features away from what that older version actually did.
There was a problem hiding this comment.
There is no older version in release that has Adaptive Aggregator at all. So, adaptive_aggregator_freeze_threshold_bytes and Adaptive Aggregator will come in the same release.
query_plan_aggregation_bucket_top_k feature is not present in any release. query_plan_aggregation_bucket_top_k feature will be shipped in the upcoming release and this setting just gates the optimization.
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction. Compile time of recompiled translation units732 translation units recompiled, 6209 s compile time in total, 732 of them have a recent master baseline. |
| - 0 - Disable | ||
| - 1 - Enable | ||
| )", 0) \ | ||
| DECLARE(Bool, query_plan_aggregation_bucket_top_k, true, R"( |
There was a problem hiding this comment.
I'd prefer us to not mix this change into the main fix
| for (const auto & column : std::get<StagedChunk::AggregatePayload>(block->payload).argument_columns) | ||
| if (column && !column->valuesHaveFixedSize()) | ||
| batch_bytes += column->byteSize(); | ||
| batch_bytes += total * 16; |
There was a problem hiding this comment.
If I'm not mistaken
| batch_bytes += total * 16; | |
| batch_bytes += total * (sizeof(UInt64) + (adaptive_key_stages_bytes<SharedKey> ? sizeof(UInt64) : 0)); |
There was a problem hiding this comment.
Yes, it would be more accurate.
|
@groeneai Investigate the CI failure. |
|
The only red is It is the chronic trunk I reproduced the identical stack on pristine master today ( EXPLAIN ANALYZE indexes = 0, actions = 1, projections = 1
SELECT (SELECT YYYYMMDDToDate(yyyymmdd)) FROM (
SELECT 19840121 AS yyyymmdd UNION ALL SELECT 20230911 AS yyyymmdd
QUALIFY exists((SELECT toDateOrZero(isZeroOrNull(NULL), NULL, toNullable(NULL))
UNION ALL SELECT toDateOrZero(isZeroOrNull(NULL), NULL, toNullable(NULL))))
) ORDER BY yyyymmdd ASC
SETTINGS allow_experimental_correlated_subqueries = 1, correlated_subqueries_use_in_memory_buffer = 0,
execute_exists_as_scalar_subquery = 0, correlated_subqueries_default_join_kind = 'left'Your failing query has the same shape (a |
|
If I'm not mistaken, the release branch is already created - you'll need to backport it. |
24dc825
Backport #115755 to 26.8: Weigh the adaptive aggregation's thaw by staged bytes and add the freeze-bytes settings
…8/115755 Revert "Backport #115755 to 26.8: Weigh the adaptive aggregation's th…
…3-backport/26.8/115755 Revert "Revert "Backport #115755 to 26.8: Weigh the adaptive aggregation's th…"
…thaw by staged bytes and add the freeze-bytes settings
…ation's thaw by staged bytes and add the freeze-bytes settings"
…e aggregation's th…"
Memory regression fix:


Related: #111459
Related: #115480
The behavioral half of #115480, split out so it can merge independently of the restructuring: one behavioral improvement to the adaptive aggregation's thaw, one new setting, and a latent initialization bug fix.
The thaw verdict is weighted by bytes
The thaw previously fired on the staged stream's repeat factor alone (> 12). That misses streams that repeat keys only a few times but drag wide payloads with every staged record. The verdict is now wasted staged bytes per distinct key:
(repeat factor − 1) × bytes per record > 300— a key's first record is the price of storing-it-once; every repeat is bytes the baseline would have absorbed as an in-place update. Near-unique streams measure ~0 by construction and cannot fire; heavy-column streams stand down early in proportion to the damage.Only the keys and the variable-width argument columns count toward a record's bytes. Fixed-width arguments are deliberately excluded: the drain applies them with the same batch executor the scan would have used, so their staging costs about what their consumption saves — a five-
UInt64-argument stream at repeat 10 stays engaged and is a clear adaptive win, while a bare repeat rule (or counting its fixed bytes) would thaw it away.Measured on
hits(100M) (off → on, 16/64 threads):SELECT URL, min(Title) GROUP BY URL(90-byte string key): 0.83×/0.73× → 0.99×/1.01×SELECT UserID, min(URL) GROUP BY UserID(heavy argument, +121% peak RSS before): 0.83×/0.59× → 0.97×/1.00×, peak RSS 14.5 → 6.3 GB (matching the ordinary path)GROUP BYsweep within noise; winning shapes unchanged.New settings
adaptive_aggregator_freeze_threshold_bytes(default 4 MiB): the byte twin of the key-count freeze threshold — a thread's local table freezes at whichever bound is reached first. Measured as the local table's own allocated bytes (hash-table buffer plus arenas), checked between blocks like the baseline's conversion thresholds; deliberately not the query-wide tracked memory, which would freeze all the locals off each other's growth. At the default it is a guardrail: calibration on the heaviest shapes shows ordinary tables keep freezing by key count (e.g.WatchID, min(URL): 14 of 16 locals unchanged, two freeze earlier by bytes, at 15582 and 8192 keys), and a full ClickBenchGROUP BYsweep of default vs0is noise (geomeans 1.001 / 1.007 at 16 / 64 threads). The plan-serialization side rides version 7, since all the adaptive settings ship in the same release.HashTable Statistics
Adaptive Aggregator no longer uses previous run hash tables sizes to resize it's small buffer tables. First, buffer tables are very small so any save in runtime is negligible. Second, if buffer table is resized above
adaptive_aggregator_freeze_threshold_bytes, it will freeze after first block instead storing all frequent keys eventually.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
The adaptive
GROUP BYaggregation (enable_adaptive_aggregator) now measures whether deferring rare keys pays off and returns to the ordinary algorithm when their keys or aggregate arguments are too wide to copy profitably, so aggregations over heavy string columns (e.g.min(URL)per user) run at the ordinary algorithm's speed and memory. Addsadaptive_aggregator_freeze_threshold_bytesto bound each thread's frozen table in bytes (whichever of it and the key-count threshold is reached first freezes the table).Workflow [PR]
Sync PR [sync-upstream/pr/115755]
Version info
26.8.1.2000