Repository navigation
Fix distributed RIGHT JOIN - #118611
Fix distributed RIGHT JOIN#118611alexey-milovidov merged 8 commits into
Conversation
Broadcast the original left table and keep the preserved right side sharded, so unmatched right rows are not duplicated on every shard. Related: ClickHouse#95728 Related: Altinity#1664 Co-authored-by: Cursor <[email protected]>
|
Workflow [PR], commit [6b52e28] Summary: ✅
AI ReviewSummaryThis PR fixes distributed Findings❌ Blockers
Final Verdict❌ Changes requested. The analyzer planner is fixed, but the old interpreter still appears to preserve the original wrong-result behavior for distributed LLVM Coverage ReportMeasured on commit 6b52e28.
Changed lines: Changed C/C++ lines covered: 82/87 (94.25%) · Uncovered code |
The rewrite that swaps the sides of a `GLOBAL RIGHT JOIN` between two `Distributed` tables only accepted an `ON` condition, so a join written with `USING` or `NATURAL` kept duplicating its unmatched rows once per shard. A `USING` key records its sides positionally, the left one first. The join condition, the `USING (a AS b)` clause shipped to the shards and the key supertype all read that order, so the sides are swapped together with the table expressions. A key that does not hold a plain column per side is not swappable that way and the query is left alone. `NATURAL` needs no separate handling because the analyzer turns it into `USING` before the planner sees it. Related: ClickHouse#118611 Co-authored-by: Cursor <[email protected]>
The rewrite that swaps the sides of a `GLOBAL RIGHT JOIN` required both sides to be `Distributed` tables with at least two shards, so it covered one shape out of seven. Only the left table fans the query out across shards, and that fanout is what makes every shard emit the rows of the preserved side again, so the right side has no bearing on it. With a sharded table on the left the rows were duplicated for a plain `MergeTree`, a single-shard `Distributed` table, a `Merge` table, a subquery, `numbers` and `remote` on the right alike. Gate the rewrite on the left table alone. Once the preserved side moves out of the broadcast position it keeps running on the shards if it is a sharded `Distributed` table of its own, and runs on the initiator otherwise, which is slower but is the only way to emit those rows once. `ANY`, `SEMI` and `ANTI` mirror into their `LEFT` counterparts exactly as `ALL` does, so they are rewritten too. `ASOF` stays excluded because swapping the sides reverses the direction of its inequality, and `RightAny` because the strictness itself names the side to take a row from. `FULL JOIN` duplicates the same way but is symmetric, so swapping cannot repair it. That is left alone. Related: ClickHouse#118611 Co-authored-by: Cursor <[email protected]>
The rewrite only ever looked at the root of the join tree. Join trees are left deep, so in `a GLOBAL RIGHT JOIN b INNER JOIN c` the root is the `INNER` join and the `RIGHT` join sits one level below it, where the rewrite never saw it and the rows preserved by the `RIGHT` join were emitted once per shard. Walk down to the deepest join before applying the existing checks. That join reads the leftmost table, and it is the only one whose sides can be swapped without moving a join into the right table expression, which is a tree shape no query can produce and the planner therefore never sees. The remaining shapes are the ones where the join that preserves broadcast rows has another join on its left, such as `a INNER JOIN c GLOBAL RIGHT JOIN b`, and `FULL JOIN`, which is symmetric and cannot be repaired by swapping at all. Read the leftmost table through a subquery for those, so the join runs on the initiator, where the preserved rows are emitted once. That costs the distributed execution of the join and makes shard specific values such as `shardNum` stop varying, so it is done only for the join trees that are wrong without it. A join tree the swap has already fixed keeps running on the shards. Related: ClickHouse#118611 Co-authored-by: Cursor <[email protected]>
`test_cluster_one_shard_localhost` is not defined in the test configuration, so the two `Distributed` tables the test creates over it could not be read. `test_shard_localhost` is the single shard cluster of the test configuration and produces the same rows, so the reference is unchanged. Co-authored-by: Cursor <[email protected]>
Port of the review fixes made for the `master` pull request ClickHouse#118611 `tryRewriteGlobalRightJoinAsLeftJoin` only recognised a narrow shape of the query it was meant to fix, so a `GLOBAL RIGHT JOIN` whose left table is a sharded `Distributed` table still emitted the rows preserved from the broadcast side once per shard in these cases: * `USING` and `NATURAL` joins were skipped, because only `ON` was accepted. A `USING` key stores its sides positionally, so the key pairs are now swapped together with the table expressions, and a key that does not hold a plain column per side leaves the query alone. * The rewrite required the right side to be a sharded `Distributed` table too. Only the left table fans the query out across shards, so the gate is now on the left table alone, and a local table, a one shard `Distributed` table, a subquery or a table function on the right is handled as well. `Any`, `Semi` and `Anti` join over to the swapped sides the same way `All` does and are accepted; `Asof` and `RightAny` do not mirror and stay out. * Only the topmost join was examined. Join trees are left deep, so the walk now descends to the join that reads the leftmost table. A `RIGHT` or `FULL` join that survives the rewrite, such as the one in `(A INNER C) RIGHT B`, cannot be swapped without moving a join into the right table expression. Those now read the leftmost table through a subquery, which keeps the join on the initiator, the only remaining way to emit the preserved rows once. `getDistributedStorageFromTableExpression` also missed `remote` and `clusterAllReplicas` on the left, because it only looked at `TableNode`. Co-authored-by: Cursor <[email protected]>
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units9 translation units recompiled, 43 s compile time in total, 9 of them have a recent master baseline. |
The flaky check ran 50 copies of the combined matrix at once. Successful runs already took 56-95 s, and two of them exceeded the 180 s limit. https: //github.com/ClickHouse/ClickHouse/actions/runs/34228156778/job/102106056610?pr=118611 https: //github.com/ClickHouse/pull/118611 Co-authored-by: Cursor <[email protected]>
`getDistributedStorageFromTableExpression` only recognised a bare `StorageDistributed`, so an `Alias` (and the same for `MaterializedView` and `StorageProxy`) of a sharded `Distributed` table still skipped the `GLOBAL RIGHT JOIN` rewrite and duplicated unmatched rows once per shard. https: //github.com/ClickHouse/pull/118611 Co-authored-by: Cursor <[email protected]>
`getDistributedStorageFromTableExpression` already unwrapped `Alias`, `MaterializedView` and `StorageProxy`, but `Buffer` still forwarded the unrepaired join to its `Distributed` destination and duplicated unmatched rows once per shard. https: //github.com/ClickHouse/pull/118611 Co-authored-by: Cursor <[email protected]>
Reproducer-- Stock server; `test_cluster_two_shards_localhost` ships in the default config.
CREATE TABLE l (k1 UInt32) ENGINE = MergeTree ORDER BY k1;
CREATE TABLE r (k2 UInt32) ENGINE = MergeTree ORDER BY k2;
CREATE TABLE ld AS l ENGINE = Distributed('test_cluster_two_shards_localhost', currentDatabase(), l);
CREATE TABLE rd AS r ENGINE = Distributed('test_cluster_two_shards_localhost', currentDatabase(), r);
INSERT INTO l VALUES (1);
INSERT INTO r VALUES (1), (3);
-- Reference: same join forced onto the initiator. Returns 6.
SELECT count() FROM (SELECT * FROM ld) AS l RIGHT JOIN (SELECT * FROM rd) AS r ON l.k1 = r.k2;
-- Returns 8 on 26.8 / 26.7 / 26.6 / 26.3: the 2 unmatched right rows (k2 = 3)
-- are emitted once per shard. Same 8 for `SETTINGS distributed_product_mode='global'`
-- and for `GLOBAL FULL JOIN`.
SELECT count() FROM ld AS l GLOBAL RIGHT JOIN rd AS r ON l.k1 = r.k2;Results:
Backport the fix to CC component owner: @vdimir @yariks5s Analysis metadata
Introducing change: predates `v23.8.16.40` (oldest testable release)
|
The filter was bound to the left `Distributed` table before the join sides were swapped, then followed that table into a `Memory` temporary table which does not support `PREWHERE`. Relocate it into the broadcast subquery when that table owns it, keep it on the initiator wrap otherwise, and preserve it when a remote non-leftmost table is wrapped. https: //github.com/ClickHouse/issues/119621 https: //github.com/ClickHouse/pull/118611 Co-authored-by: Cursor <[email protected]>
Cover the original no-op `PREWHERE` reproducer and preserve filters when a remote non-leftmost table is wrapped for a join. https: //github.com/ClickHouse/issues/119621 https: //github.com/ClickHouse/pull/118611 Co-authored-by: Cursor <[email protected]>
Fix distributed RIGHT JOIN.
Closes #95728
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix distributed RIGHT JOIN.
Broadcast the original left table and keep the preserved right side sharded, so unmatched right rows are not duplicated on every shard.
Workflow [PR]
Sync PR [sync-upstream/pr/118611]
Version info
26.9.1.1232(included in26.9and later)