Repository navigation
Allow the preliminary DISTINCT to abandon deduplication on mostly-unique input - #116508
Conversation
|
Workflow [PR], commit [bac8502] Summary: ✅
AI ReviewSummaryThis PR teaches the preliminary per-stream Findings
Tests
Final VerdictChanges requested: the distributed / serialized LLVM Coverage ReportMeasured on commit bac8502.
Changed lines: Changed C/C++ lines covered: 74/74 (100.00%) · Uncovered code |
| /// The preliminary deduplication is best-effort (a deduplicating consumer follows), so on | ||
| /// mostly-unique input the transform may abandon it and free its hash table - unless a limit | ||
| /// hint is set: an abandoned transform cannot count the distinct rows to stop the input early. | ||
| const bool allow_abandoning = pre_distinct && settings.allow_preliminary_distinct_abandoning && limit_hint == 0; |
There was a problem hiding this comment.
allow_abandoning is keyed off limit_hint == 0, but DistinctStep::serialize still drops limit_hint (serialize even notes that the follower would need to re-derive it) and deserialize reconstructs the step with 0. Both off-node plan executors then build from that deserialized step (src/QueryPipeline/DistributedPlanExecutor.cpp:841-845, src/Interpreters/executeQuery.cpp:3191-3200), so a remote PreDistinct for SELECT DISTINCT ... LIMIT ... can still abandon even though the original planner explicitly disabled it.
That breaks the new "don't abandon when a limit hint is present" contract for serialized/distributed execution, and the worker fragment often cannot recover the hint on re-optimization because the LIMIT sits above the coordinator-side final DISTINCT, not above the shipped preliminary one. Please serialize limit_hint (or an explicit allow_abandoning bit) and cover the deserialize/build path with a focused round-trip test.
There was a problem hiding this comment.
This is an existing master behavior and improvements to it are out of scope for this PR. Even if limit_hint is dropped, this is not a correctness issue.
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units684 translation units recompiled, 5106 s compile time in total, 684 of them have a recent master baseline. |
|
📊 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
|
|
@nihalzp @yariks5s — title/description suggestions: Description (currently
Ignore if intentional. |
|
@nihalzp @yariks5s — ClickGap found the following in this PR:
Close anything that's wrong or already addressed. |
Runtime:
Memory:

Related: #114645
The preliminary (per-stream)
DISTINCTpays off only when it removes rows: the finalDISTINCTdeduplicates its output anyway, so on mostly-unique input the preliminary transforms keep a second full copy of the unique keys in their per-stream hash tables and pay a hash probe per row for nothing.This PR lets the preliminary
DISTINCTgive up: its transforms now run theDeduplicationAbandonControllerintroduced for the per-partitionIN (subquery)set build in #114645. The controller observes the first chunks (five chunks, or earlier once the observed set reaches 16 MiB for wide keys) and, when nearly all rows survive deduplication (≥ 90% unique), the transform drops its hash table and passes the remaining chunks through untouched. Duplicate-heavy inputs never abandon and keep the preliminary reduction.This is sound because the preliminary
DISTINCTis best-effort by contract: duplicates from different streams pass through it even when it deduplicates, so every consumer already tolerates duplicates, and a finalDISTINCTdeduplicates the output again. Abandoning only changes how many duplicates reach the final, never the result. It applies to every preliminaryDISTINCT, including shard-side plans of distributed queries (bounded by the 90% threshold, a shard ships at most ~11% more rows on the inputs where it fires). It is not applied when the step carries a limit hint (SELECT DISTINCT ... LIMIT nwithout subsequent ordering), because stopping the input early after enough distinct rows requires counting them, and the reading-in-order path is unaffected (the sorted transform deduplicates by ranges and accumulates nothing worth abandoning).The behavior is controlled by the new setting
allow_preliminary_distinct_abandoning(enabled by default) and observable through the new profile eventDistinctTransformsAbandonedDeduplication, which also covers the set-build path from #114645.Measured on 30M unique
UInt64rows in a 16-partitionMergeTreetable,SELECT DISTINCT(5 runs, first 2 discarded):On a 10M-row mostly-unique
LowCardinality(Nullable(String))key the peak drops from 2.25 GB to 1.20 GB at 16 threads. Duplicate-heavy inputs (100M rows, 1M unique) are unchanged within noise.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
The preliminary (per-stream)
DISTINCTnow abandons deduplication when the observed input is almost entirely unique, freeing its hash tables and skipping per-row hashing: the finalDISTINCTdeduplicates the output anyway, so on unique data this roughly halves the memory ofSELECT DISTINCTand speeds it up (measured 1114 MB → 576 MB and 0.130 s → 0.074 s on 30M unique rows). Controlled by the new settingallow_preliminary_distinct_abandoning; observable via the new profile eventDistinctTransformsAbandonedDeduplication.Workflow [PR]
Sync PR [sync-upstream/pr/116508]
Version info
26.9.1.507(included in26.9and later)