Repository navigation
Conversation
The final `DistinctStep` merged all of its input streams into one and deduplicated the whole input in a single transform. It now hash-partitions the streams by the `DISTINCT` columns instead, so that equal rows land in the same stream and every stream is deduplicated on its own thread. The merge is kept when the input is sorted by the `DISTINCT` columns (the sorted deduplication relies on its stream order), when the global size limits are set (they are enforced by the single transform that sees the whole input), and for a single stream. New setting `allow_parallel_final_distinct`, on by default. The chunks are squashed before the scatter: the scatter turns every chunk into one chunk per partition, and the preliminary `DISTINCT` in front of it emits a small chunk whenever it collapses its input, so without squashing the per-chunk cost of the partitions dominates and the step is several times slower than the merge. `SortingStep` gathers its streams instead of merging them when the sort description is left with constants only. `SortingTransform` drops the constant columns of the header, and with nothing left `MergeSortingTransform` passes chunks through as they come; a k-way merge over such an input deadlocks behind the scatter, which does not consume new input until every partition has accepted the previous chunk. `SELECT DISTINCT` of one column over a sorted table with a million distinct values goes from 185ms to 51ms, `SELECT DISTINCT number FROM numbers_mt(40000000)` from 2.0s to 585ms, and the queries whose result is small enough for the merge to win are unchanged. Measured with `tests/performance/scripts/perf.py` over `sorted_distinct`, `distinct_in_order`, `distinct_low_cardinality_prelimit_distinct_in_order` and `distinct_high_cardinality`: 24 queries faster, none slower. Neither a set operation nor a sort by constants promises a row order, and three tests asserted the one the previous single-threaded merge happened to produce; they order their results now.
`max_block_size = 5` over a million rows produces two hundred thousand blocks, and the per-block cost of the pipeline makes the query trip the execution speed limit under ASan. A block size of 1000 is still far below the 65536 rows the transform squashes to, so it accumulates the same way with a fraction of the blocks.
…ut of this PR Both are independent of `INTERSECT`/`EXCEPT` and are reviewed on their own: - the final `DISTINCT` on all threads, with the constant-sort gather it needs, in ClickHouse#119228 - the left semi join that stops reading its left side for an empty right side, in ClickHouse#119218 `scatterByPartition`'s limit helpers and the `SortingStep` gather stay here as well, because the partitioned `INTERSECT`/`EXCEPT` step needs them too; they are the same change in both pull requests. Related: ClickHouse#119228 Related: ClickHouse#119218
The final `DISTINCT` merges its streams again now that the parallel one is reviewed in ClickHouse#119228, so the `EXPLAIN PIPELINE` of the set-operation step no longer shows a scatter above it.
…heck The style check lists every new `allow_*` setting that neither `enableAllExperimentalSettings` nor the ignore list knows about. This one gates an execution strategy, not an experimental feature.
…9228 `allow_parallel_final_distinct` is declared there now, and the style check reads the list against the settings this branch declares. Related: ClickHouse#119228
|
Workflow [PR], commit [38e1f3b] Summary: ✅
AI ReviewSummaryThis PR parallelizes the final Missing context / blind spots
Final Verdict✅ No new findings. LLVM Coverage ReportMeasured on commit 38e1f3b.
Changed lines: Changed C/C++ lines covered: 74/74 (100.00%) · Uncovered code |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes60 object files changed (+28.14 KiB total), 0 added.
716 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units674 translation units recompiled, 5201 s compile time in total, 674 of them have a recent master baseline. |
…al order through it `applyOrder` lets the global order of the input survive a `DistinctStep`, and the steps above may rely on it, so the hash scatter into partition streams must not run for such a step. It now marks the step with `keepGlobalOrder`, and the step keeps the single-stream merge. This is a defensive change: every source of a global order today ends in a single stream, which the scatter never sees, so no query changed its plan. Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_01K1wpba9fdyc4QQYKWwVMWU
…of the parallel `DISTINCT` test With the randomized `MergeTree` settings the sorted table can be read in order through several streams, which shows up as a stream count on the preliminary `DistinctSortedStreamTransform`. The check only cares that no scatter appears. Failed in https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=119228&sha=0519b038c67fa15c2cf04fab1d769ce069e05c91&name_0=PR&name_1=Stateless%20tests%20(amd_tsan,%20s3%20storage,%20parallel,%20selected%20tests) Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_01K1wpba9fdyc4QQYKWwVMWU
|
📊 Cloud Performance Report ✅ AI verdict: This PR adds parallel final DISTINCT (hash-partition the streams instead of merging into one) plus a constants-only sort guard; the only execution-path changes live in DistinctStep and SortingStep and take effect for SELECT DISTINCT queries. TPC-H Q9 is a multi-join aggregation with no DISTINCT, so it doesn't exercise the changed code and the flagged +16.7% (+16.5 ms, 99.0 -> 115.5 ms) is not plausibly caused by this change. It also ran co-located with another benchmark on the same node and has no history baseline, both of which favor noise; downgraded to not sure. No cross-query pattern: only one query was flagged. clickbench🟢 No significant changes tpch_adapted_1_officialFlagged queries (1 of 22)
Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on. Debug info
|
…T` test The old analyzer nests the inner pipeline of `05194_parallel_final_distinct_keeps_global_order` one level less deep than the new one, so the `EXPLAIN PIPELINE` lines differed only by their indentation and the test failed in the jobs that run with the old analyzer. The test now trims the leading whitespace: it checks which transforms appear and in which order, which is the same under both analyzers. CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=119228&sha=fc4c7194ac1896f7c006228157638bf554fb59bc&name_0=PR&name_1=Stateless%20tests%20%28amd_llvm_coverage%2C%20old%20analyzer%2C%20s3%20storage%2C%20DBReplicated%2C%20parallel%2C%203%2F3%29 PR: ClickHouse#119228 Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_01M5DPfWFrJ8G7XxrUxD6EUD
|
Actually, the other PR (mapping to highly optimized aggregation instead of custom parallelization) seems more promising. Thanks for sending this PR anyways @raimannma |
The final
DISTINCTmerged all of its input streams into one and deduplicated the whole input in a single transform. It now hash-partitions the streams by theDISTINCTcolumns, so equal rows land in the same stream and every stream is deduplicated on its own thread. The merge is kept when the input is sorted by theDISTINCTcolumns, when the global size limits are set, and for a single stream. New settingallow_parallel_final_distinct, on by default.A sort whose description is left with constants only gathers its streams instead of merging them: a k-way merge over such an input deadlocks behind the scatter.
SELECT DISTINCTof one column over a sorted table with a million distinct values goes from 185ms to 51ms,SELECT DISTINCT number FROM numbers_mt(40000000)from 2.0s to 585ms. Measured withperf.pyoversorted_distinct,distinct_in_order,distinct_low_cardinality_prelimit_distinct_in_orderanddistinct_high_cardinality: 24 queries faster, none slower.Split out of #118230; it is independent of
INTERSECT/EXCEPTand speeds up anySELECT DISTINCT.Related: #118230
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
The final
DISTINCTis executed on all threads by hash-partitioning its input by theDISTINCTcolumns (new settingallow_parallel_final_distinct).Workflow [PR]
Sync PR [sync-upstream/pr/119228]