Repository navigation
Conversation
Internal second-model review — adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 2 findings across an initial pass and a delta recheck; bounded: 1 full pass + 1 fix round + 1 delta recheck).
Final fix: Severity: ❌ blocker / Session id: cron:clickhouse-author-slot-2:20260720-163500 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-author-slot-2:20260720-163500 |
|
cc @azat @devcrafter - could you review this? |
|
Workflow [PR], commit [b990620] Summary: ❌
AI ReviewSummaryThis PR closes the correctness holes around same-named columns in distributed joins, but the new shard-local precheck is still too conservative in two realistic shapes that used to keep Findings
Tests
Final Verdict
|
…gation The previous guard disabled optimize_distributed_group_by_sharding_key for any DISTINCT / GROUP BY / LIMIT BY key that referenced a column from another join side, which regressed sound cases: grouping by this table's own sharding key (e.g. GROUP BY l.k, r.v with the table sharded by k) keeps every group on a single shard, so shards can still process it to Complete. Accept the shard-local shortcut only when it stays sound and reject the three situations the unqualified-name matching breaks on: - an unqualified name is exposed by more than one source: the type-checked DAG collapses them and can throw (issue ClickHouse#111089); - a sharding-key column name is not backed by this table's own column: a same-named column from another side does not pin the shard (wrong results); - a RIGHT/FULL join can null-extend this table: an unmatched row carries no sharding key and is produced on every shard, so a group keyed by it spans shards (wrong results). Add EXPLAIN-based assertions that GROUP BY over this table's sharding key alongside another side's column stays shard-local, while a RIGHT JOIN that can null-extend it merges on the initiator and matches the non-optimized result. Co-Authored-By: Claude Opus 4.8 <[email protected]>
e788f21 to
4779b18
Compare
|
Fixed in 4779b18. You were right, the previous guard over-corrected. The guard now accepts the shard-local shortcut whenever it stays sound and rejects only three cases:
So Verified on a 2-shard cluster (
While checking the RIGHT/FULL case I found the first narrow version still returned unmerged partials for Note: #111123 guards the same function for the related |
The shard-local precheck for optimize_distributed_group_by_sharding_key collected an unqualified name for every ColumnNode, including ALIAS columns. The type-checked DAG it guards is built with use_column_identifier_as_action_node_name = false, where an expression-backed column is represented by its expression (PlannerActionsVisitor::visitColumn recurses into it) and never materialized under its own name. So the precheck saw name collisions the DAG never produces and disabled the optimization for sound keys, e.g. GROUP BY l.k_alias, r.k_alias where l.k_alias is ALIAS of the sharding key and the two aliases wrap different physical columns. Record only leaf physical column names (skip expression-backed wrappers; the visitor still descends into their expressions to reach the leaves), and do not descend into a constant-backed column, mirroring how the DAG treats constants. This keeps the correctness guards from issue ClickHouse#111089 (the type-mismatch throw, the wrong-side sharding-key wrong-results, and the RIGHT/FULL null-extension cases) while restoring the optimization for alias-backed sound keys. Co-Authored-By: Claude Opus 4.8 <[email protected]>
sourceIsAlwaysPreserved only recognized JoinNode, so it returned false for every other join-tree node. The query tree keeps CROSS and comma joins under CrossJoinNode and ARRAY JOIN under ArrayJoinNode, so grouping by this table's own sharding key over such a join (e.g. GROUP BY l.k, r.c with l.k the sharding key) was wrongly forced to merge on the initiator instead of taking the shard-local path, a performance regression for a sound query. Descend through every node the join tree can hold: leaf, binary JoinNode (null-extension aware), N-ary CrossJoinNode (CROSS/comma never null-extends any child), and unary ArrayJoinNode (ARRAY JOIN, including LEFT ARRAY JOIN, only replicates the input rows and never nulls the base table columns). Mixed joins are left-associative, so the recursion must reach a table nested under a CrossJoinNode. Add CROSS-join regression coverage to 03471 (EXPLAIN preservation and result equality) and regenerate the reference. Co-Authored-By: Claude Opus 4.8 <[email protected]>
| return false; | ||
| } | ||
|
|
||
| return true; |
There was a problem hiding this comment.
This is still too conservative for INNER equi-joins where the other side's key is proven equal to the distributed table's sharding key. For example, if l is sharded by k, SELECT r.k, count() FROM dist AS l INNER JOIN dim AS r ON l.k = r.k GROUP BY r.k is still shard-local: every output row has r.k = l.k, and l.k pins the shard. This branch forces that query (and the same DISTINCT / LIMIT BY shapes) back to initiator merge because the carrier of k is r, even though the join condition already proves it is the same value.
…-analyzer job The guarded optimization under test (getOptimizedQueryProcessingStageAnalyzer) only runs under the analyzer. In the old-analyzer stateless job the server defaults enable_analyzer=0, so the MergingAggregated plan-shape assertions took a different code path and the RIGHT-join cases flipped (deterministic 3/3 FAIL). Pin enable_analyzer=1 at statement level so the test exercises the guarded path regardless of the job's analyzer default. Results and correctness cases unchanged. Co-Authored-By: Claude Opus 4.8 <[email protected]>
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 44/47 (93.62%) · Uncovered code |
CI finish ledger - ba4d7acCI fully finished (Finish Workflow / Mergeable Check / Config Workflow / CH Inc sync all green). One red check, owned:
Session id: cron:our-pr-ci-monitor:20260721-130000 |
…-skip-unused-shards-distributed-join-column-binding Co-Authored-By: Claude Opus 5.5 <[email protected]>
Keep only the invariants a reader needs at each spot: the source collector follows the plain-name ActionsDAG builder's column visibility, null-extended rows carry no sharding key, and the guard must run before the DAG is built. The examples and the per-case reasoning live in the earlier commit messages. Co-Authored-By: Claude Opus 5.5 <[email protected]>
PlannerActionsVisitor::visitColumn materializes a column whose expression is a constant as a constant action named after the column, instead of looking through it. The source collector skipped such columns entirely, so a constant ALIAS `x` on one join side and a physical `x` of another type on the other side still collapsed onto one plain-named node and threw ILLEGAL_TYPE_OF_ARGUMENT, e.g. `x String ALIAS 'abc'` against `x Int64` with `SELECT DISTINCT l.k, l.x, r.x + 2`. Record them under their own name. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The guarded probe only runs with the setting enabled, and the runner turns it off for about 5% of runs, so the exception and r.k cases passed vacuously then. Measured through system.query_log: 5 of 65 runs had it off without the clamp, 0 of 85 with it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
| if (!expected_source) | ||
| return false; | ||
|
|
||
| if (!sourceIsAlwaysPreserved(join_tree, expected_source)) |
There was a problem hiding this comment.
query_info.table_expression is not always a direct child of the top-level join tree here. When optimize_trivial_view_pushdown_to_distributed inlines a trivial VIEW, PlannerJoinTree rewrites query_info.query_tree to contain the view-body QueryNode but keeps query_info.table_expression pointed at the underlying dist_table_node. Because sourceIsAlwaysPreserved stops at QueryNode, even a simple SELECT k FROM v GROUP BY k now fails this check and loses the shard-local Complete path although the view is only passing k through. We need a query-node-aware preservation check (or some other way to recognize passthrough view projections); otherwise this correctness fix becomes a broad performance regression for pushed-down views.
After the master merge the file already includes `Analyzer/ArrayJoinNode.h` near the top of the analyzer block, so this PR's copy tripped `readability-duplicate-include` in `Build (arm_tidy)`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The existing RIGHT JOIN cases group by `l.k, r.k`, so the same-name check alone already forces the merge, and master now rewrites a GLOBAL RIGHT JOIN into a LEFT JOIN before the stage is chosen. Neither case reached the null-extension check: with it disabled the test still passed. A non-GLOBAL RIGHT or FULL JOIN with a local right table runs on every shard against the whole table, so an unmatched right row gets a default `l.k` on each shard. Grouping by `l.k` alone then has no name collision, and on master returns the group `0` twice (8 and 7 rows instead of one row of 15). Verified: with the check disabled the two new assertions flip to 0 (default, parallel replicas and serialize_query_plan configurations); with it, 33/33, 20/20 and 15/15 randomized non-environment runs pass. Co-Authored-By: Claude Opus 5.5 <[email protected]>
CI finish ledger - b990620Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
All 149 check runs completed, Session id: cron:our-pr-ci-monitor:20261008-020432 |
Closes: #111089
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed
optimize_skip_unused_shards = 1misbehaving on global distributed joins (distributed_product_mode = 'global'): a valid query such asSELECT DISTINCT l.c, r.c + 2 ...where both sides have a column of the same name threwILLEGAL_TYPE_OF_ARGUMENT, and grouping by a right-side column whose name matched the left table's sharding key could return unmerged per-shard partial results.Description
On 26.3.42.3 and 26.8.18.2,
SELECT DISTINCT l.c, r.c + 2 FROM nb_dl AS l INNER JOIN nb_dr AS r ON l.a = r.a SETTINGS distributed_product_mode = 'global', optimize_skip_unused_shards = 1throwsILLEGAL_TYPE_OF_ARGUMENTwhen both Distributed tables have a columncof different types; withoptimize_skip_unused_shards = 0it runs.Root cause:
getOptimizedQueryProcessingStageAnalyzerprobes theDISTINCT/GROUP BY/LIMIT BYkey withisShardingKeySuitsQueryTreeNodeExpression, which builds anActionsDAGover unqualified column names. Same-named columns from different join sides collapse onto one node (the exception above), and a right-side column named like the left table's sharding key (GROUP BY r.k) was matched to the sharding key, so shards returned unmerged partials.The fix adds a precheck before that DAG is built. The shard-local stage is kept only when no unqualified name the DAG will see (including constant-backed
ALIAScolumns) comes from more than one source, every sharding-key column comes from this table, and no outer join can null-extend this table. Otherwise the initiator merges, as withoptimize_skip_unused_shards = 0. Joins that group by this table's sharding key plus other-side columns with distinct names keep the optimization.The test covers the exception (
INNER,LEFT, comma join,GROUP BY,LIMIT BY, constantALIAS), theGROUP BY r.kwrong result,RIGHTjoins, a non-GLOBALRIGHT/FULLjoin with a local right table (master returned a group once per shard), andEXPLAINchecks that the sound cases (CROSSjoin,ARRAY JOIN,ALIASof the sharding key) stay shard-local.Not covered: a column whose name equals a generated expression name (for example a column literally named
toString(a)) can still collide; covering it would mean building the probe over qualified identifiers.Workflow [PR]
Sync PR [sync-upstream/pr/111126]