Repository navigation
Ship the preliminary DISTINCT to replicas in plan-based parallel replicas - #130355
Conversation
…gments Replicas now deduplicate their share and can read in order for it, instead of sending every row they read. Related: #128560 Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
|
Workflow [PR], commit [410a020] Summary: ✅
AI ReviewSummaryShips preliminary Missing context / blind spots
Final VerdictStatus: ✅ Approve. No new inline findings or thread updates are needed. |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Slowest function optimization changes (ThinLTO)Median per-function time ratio to the master baseline:
Compile time of recompiled translation units123 translation units recompiled, 907 s compile time in total, 123 of them have a recent master baseline. Median compile-time ratio to the baselines is ×1.06 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio. |
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
groeneai
left a comment
There was a problem hiding this comment.
Looks good to me. I built d40f83a7 and the same tree without the applyParallelReplicas.cpp hunk, and compared them on a three-replica localhost cluster.
- Every preliminary
DistinctStepthe planner creates (SELECT,UNION DISTINCT, theliftUpUnioncopies) has a finalDISTINCTabove it, with noLIMITin between (DISTINCTgets neither a partial-sort limit nor a preliminaryLIMIT), so the split may continue into the sort branch. The finalDISTINCTgets no sort order through the split, so it reads in order only from the merged stream of a shippedORDER BY. - Results match the query without parallel replicas for
parallel_replicas_local_plan0/1,optimize_distinct_in_order0/1,max_threads = 1andmax_block_size = 97, on Wide, Compact and sparse parts:ORDER BY(alsoDESCand a column outside theDISTINCT),LIMIT/OFFSET,WITH TIES,LIMIT BY,DISTINCT ON,UNION DISTINCT,WITH TOTALS,ROLLUP, a window function,JOIN,IN (SELECT DISTINCT ...),LowCardinality/Nullablekeys,force_distinct_partitions_independently = 1, and a subquery whoseoptimize_distinct_in_orderdiffers from the outer one.max_rows_in_distinctstill throws with'throw'and returns with'break'. - No new version gate is needed:
PreDistinctis a registered step, and the existing plan version check for parallel replicas admits only replicas that know it. - The five tests from the description pass 3/3 on the head and fail 3/3 on the old code with plan-based parallel replicas. With
parallel_replicas_local_plan = 0, the replicas now send 200000 rows instead of 2000000 forSELECT DISTINCT aover 2M rows with 50000 values.
Not from this PR: with exact_rows_before_limit = 1, rows_before_limit_at_least of SELECT DISTINCT a FROM t LIMIT 3 is wrong with parallel replicas before and after it (expected 50000; 0 with the local plan, also in classic parallel replicas; 2000000 before and 200000 after without it).
One test suggestion inline.
nickitat
left a comment
There was a problem hiding this comment.
Might make sense to test how much we send when there is also a limit.
…NCT` in plan-based parallel replicas `limitPushDown` also gives the limit hint to the preliminary `DISTINCT` below the final one, so replicas both stop reading and send at most `limit + offset` rows, as with query-based parallel replicas. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…limitPushDown` Also keep the `Limit -> Sorting` comment on its function and shorten a comment. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…`limitPushDown` A shipped plan fragment may not contain the `LIMIT` the hint comes from, so a replica cannot derive it again. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
@nickitat Thanks, with a
Rows sent and read per replica, 2M rows with 50000 distinct values,
|
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…plan-based-distinct
`Distinct` and `PreDistinct` are at step version 2 now. Revert the round trip test added to `gtest_distinct_step_input_order.cpp`. https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=130355&sha=c86cfdd5dd054e22367811298f1c535e9c9bee80&name_0=PR&name_1=Unit%20tests%20%28amd_debug%29 #130355 Co-Authored-By: Claude Opus 5.5 <[email protected]>
groeneai
left a comment
There was a problem hiding this comment.
Re-reviewed at 4717cb4, approving. What I checked in the code:
- The shipped
LIMITonly exists when the preliminaryDISTINCThas a hint, and onlyaddDistinctStepinPlanner.cppsets that hint (liftUpUnioncopies it,limitPushDownstops at the finalDISTINCT). It is 0 withORDER BYbefore the step,LIMIT BY,WITH TIES, a negative or fractionalLIMIT,exact_rows_before_limitandWITH TOTALS, so the fragment'sLIMITnever has to read till the end.LIMIT n OFFSET -mgets the hintn + m, which is still enough forNegativeOffset+LIMITon the initiator. - The local final
DISTINCTis needed for correctness, not only for size: without it, duplicates from different streams could fill thelimit + offsetrows and the initiator could end up with fewer distinct values than theLIMITasks for. - On the explicit
LIMIT: I agree it is needed.DistinctTransform::transformchecks the hint after filtering a chunk and returns that chunk whole, andDistinctSortedStreamTransformdoes the same with its output row count. The hint stops reading, it does not cap what is sent. - Version gate: 21 is in no release yet (26.10 writes 20), so step version 2 at 21 follows the rule in
ProtocolDefines.h.
Remove the `DISTINCT` tests, fixed by #130355, and `05292`, which passes now. Add `04627`, and read the projection in `05339` with `parallel_replicas_local_plan = 0`, so both fail deterministically with #128537. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The test counts the `DISTINCT` steps in EXPLAIN. With parallel replicas, EXPLAIN also prints the preliminary `DISTINCT` that runs on the replicas: in the query sent to them (query-based), or in the plan fragment (plan-based, since ClickHouse#130355 ships the preliminary `DISTINCT`). Co-Authored-By: Claude Opus 5.5 <[email protected]>
Closes: #128560
Related: #129811
Related: #128413
Plan-based parallel replicas (
parallel_replicas_plan_based) did not ship aDISTINCTto the replicas. The split marker was lifted through expressions, filters, aggregation and sorting, but stopped at the preliminaryDistinctStep, so the fragment was only the read: every replica sent all the rows it read to the initiator, and its read was not in order (optimize_distinct_in_ordercould not apply on either side).The split is now lifted above the preliminary
DISTINCT, the same way as above an expression. The preliminaryDISTINCTdeduplicates each stream on its own and is always followed by the finalDISTINCT, so it runs on a replica as it does on a shard of aDistributedtable, and the finalDISTINCTstays on the initiator. This matches classic parallel replicas.With a
LIMIT, the preliminaryDISTINCThas a limit hint (limit + offset) that makes each of its streams stop reading once it has that many distinct rows.DistinctStepdid not serialize the hint, and theLIMITit comes from stays on the initiator, so a replica read its whole share. The hint is now serialized, as version 2 of theDistinctandPreDistinctsteps, from query plan serialization version 21. A peer at an older version, such as 26.10, gets version 1 without the hint and only reads more. The hint does not bound what a replica sends, because the chunk that reaches it passes through whole, so the fragment also gets a finalDISTINCTand aLIMITof the hint, as on shards. The finalDISTINCTon replicas is needed because the preliminary one deduplicates each stream separately.For
SELECT DISTINCT a ... LIMIT 3over 2M rows with 50000 distinct values, a replica sent 200000 rows and read about 1000000 before, and now sends 3 and reads 261636, the same as classic parallel replicas.#129811, now merged, ships a
LIMITwithoutORDER BY; with this PR, #128560 is fully addressed. Once this is merged,02862_sorted_distinct_sparse_fix,04330_distinct_sorted_const_key_after_filter,04549_virtual_row_distinct_in_order_boundary,05217_aggregation_in_order_fixed_keysand05401_aggregation_nullable_fixed_keys_batch_packcan be removed fromtests/parallel_replicas_plan_based_blacklist.txtin #128413. They pass locally with plan-based parallel replicas.Changelog category (leave one):
🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/130355]
Version info
26.11.1.2229-master(included in26.11and later)