Skip to content

Commit d659141

Browse files
Backport #94680 to 25.10: Fix incorrect RIGHT join result when using complex ON conditions
1 parent 92bb8e7 commit d659141

File tree

4 files changed

+73
-2
lines changed

4 files changed

+73
-2
lines changed

src/Interpreters/HashJoin/HashJoin.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -787,7 +787,7 @@ bool HashJoin::addBlockToJoin(const Block & block, ScatteredBlock::Selector sele
787787
all_values_unique);
788788

789789
if (flag_per_row)
790-
used_flags->reinit<kind_, strictness_, std::is_same_v<std::decay_t<decltype(map)>, MapsAll>>(&stored_columns->columns_info.columns);
790+
used_flags->reinit<kind_, strictness_, std::is_same_v<std::decay_t<decltype(map)>, MapsAll>>(&stored_columns->columns_info.columns, stored_columns->selector);
791791
});
792792
}
793793

src/Interpreters/HashJoin/JoinUsedFlags.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,21 @@ class JoinUsedFlags
4545
}
4646

4747
template <JoinKind KIND, JoinStrictness STRICTNESS, bool prefer_use_maps_all>
48-
void reinit(const Columns * columns)
48+
void reinit(const Columns * columns, const ScatteredBlock::Selector & selector)
4949
{
5050
if constexpr (MapGetter<KIND, STRICTNESS, prefer_use_maps_all>::flagged)
5151
{
5252
assert(per_row_flags[columns].size() <= columns->at(0)->size());
5353
need_flags = true;
5454
per_row_flags[columns] = std::vector<std::atomic_bool>(columns->at(0)->size());
55+
56+
/// Mark all rows outside of selector as used.
57+
/// We should not emit them in RIGHT/FULL JOIN result,
58+
/// since they belongs to another shard, which will handle flags for these rows
59+
for (auto & flag : per_row_flags[columns])
60+
flag.store(true);
61+
for (size_t index : selector)
62+
per_row_flags[columns][index].store(false);
5563
}
5664
}
5765

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
1 10 1 30
2+
0 0 2 20
3+
-
4+
1 10 1 30
5+
0 0 2 20
6+
-
7+
0 0 2 20
8+
-
9+
0 0 2 20
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
DROP TABLE IF EXISTS t0;
2+
DROP TABLE IF EXISTS t1;
3+
4+
CREATE TABLE t0 (c0 UInt64, c1 UInt64) ENGINE = MergeTree() ORDER BY (c0);
5+
INSERT INTO t0 VALUES (2, 20), (1, 30);
6+
CREATE TABLE t1 (c0 UInt64, c1 UInt64) ENGINE = MergeTree() ORDER BY (c0);
7+
INSERT INTO t1 VALUES (1, 10);
8+
9+
SET query_plan_join_swap_table = 0;
10+
SET enable_analyzer = 1;
11+
12+
SELECT
13+
*
14+
FROM t1
15+
RIGHT JOIN t0
16+
ON (t0.c0 = t1.c0)
17+
AND (t0.c1 >= t1.c1)
18+
ORDER BY t0.c0
19+
SETTINGS join_algorithm='hash';
20+
21+
SELECT '-';
22+
23+
SELECT
24+
*
25+
FROM t1
26+
RIGHT JOIN t0
27+
ON (t0.c0 = t1.c0)
28+
AND (t0.c1 >= t1.c1)
29+
ORDER BY t0.c0
30+
SETTINGS join_algorithm='parallel_hash';
31+
32+
33+
SELECT '-';
34+
35+
SELECT
36+
*
37+
FROM t1
38+
RIGHT JOIN t0
39+
ON (t0.c0 = t1.c0)
40+
AND (t0.c1 >= t1.c1)
41+
WHERE t0.c0 = 2
42+
SETTINGS join_algorithm='parallel_hash', query_plan_filter_push_down = 1;
43+
44+
SELECT '-';
45+
46+
47+
SELECT
48+
*
49+
FROM t1
50+
RIGHT JOIN t0
51+
ON (t0.c0 = t1.c0)
52+
AND (t0.c1 >= t1.c1)
53+
WHERE t0.c0 = 2
54+
SETTINGS join_algorithm='parallel_hash', query_plan_filter_push_down = 0;

0 commit comments

Comments
 (0)