-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix hash join with sort push down #13560
Changes from all commits
ea4a7f5
5e5fa7d
528b57e
7f81178
13b98b5
52d9def
e080ee0
b4e214d
64fac8f
2f05a33
64f0188
3b1f50a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2864,13 +2864,13 @@ explain SELECT t1_id, t1_name FROM left_semi_anti_join_table_t1 t1 WHERE t1_id I | |
---- | ||
physical_plan | ||
01)SortPreservingMergeExec: [t1_id@0 ASC NULLS LAST] | ||
02)--SortExec: expr=[t1_id@0 ASC NULLS LAST], preserve_partitioning=[true] | ||
03)----CoalesceBatchesExec: target_batch_size=2 | ||
04)------HashJoinExec: mode=Partitioned, join_type=RightSemi, on=[(t2_id@0, t1_id@0)] | ||
05)--------CoalesceBatchesExec: target_batch_size=2 | ||
06)----------RepartitionExec: partitioning=Hash([t2_id@0], 2), input_partitions=2 | ||
07)------------RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1 | ||
08)--------------MemoryExec: partitions=1, partition_sizes=[1] | ||
02)--CoalesceBatchesExec: target_batch_size=2 | ||
03)----HashJoinExec: mode=Partitioned, join_type=RightSemi, on=[(t2_id@0, t1_id@0)] | ||
04)------CoalesceBatchesExec: target_batch_size=2 | ||
05)--------RepartitionExec: partitioning=Hash([t2_id@0], 2), input_partitions=2 | ||
06)----------RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1 | ||
07)------------MemoryExec: partitions=1, partition_sizes=[1] | ||
08)------SortExec: expr=[t1_id@0 ASC NULLS LAST], preserve_partitioning=[true] | ||
09)--------CoalesceBatchesExec: target_batch_size=2 | ||
10)----------RepartitionExec: partitioning=Hash([t1_id@0], 2), input_partitions=2 | ||
11)------------RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1 | ||
|
@@ -2905,13 +2905,13 @@ explain SELECT t1_id, t1_name FROM left_semi_anti_join_table_t1 t1 LEFT SEMI JOI | |
---- | ||
physical_plan | ||
01)SortPreservingMergeExec: [t1_id@0 ASC NULLS LAST] | ||
02)--SortExec: expr=[t1_id@0 ASC NULLS LAST], preserve_partitioning=[true] | ||
03)----CoalesceBatchesExec: target_batch_size=2 | ||
04)------HashJoinExec: mode=Partitioned, join_type=RightSemi, on=[(t2_id@0, t1_id@0)] | ||
05)--------CoalesceBatchesExec: target_batch_size=2 | ||
06)----------RepartitionExec: partitioning=Hash([t2_id@0], 2), input_partitions=2 | ||
07)------------RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1 | ||
08)--------------MemoryExec: partitions=1, partition_sizes=[1] | ||
02)--CoalesceBatchesExec: target_batch_size=2 | ||
03)----HashJoinExec: mode=Partitioned, join_type=RightSemi, on=[(t2_id@0, t1_id@0)] | ||
04)------CoalesceBatchesExec: target_batch_size=2 | ||
05)--------RepartitionExec: partitioning=Hash([t2_id@0], 2), input_partitions=2 | ||
06)----------RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1 | ||
07)------------MemoryExec: partitions=1, partition_sizes=[1] | ||
08)------SortExec: expr=[t1_id@0 ASC NULLS LAST], preserve_partitioning=[true] | ||
09)--------CoalesceBatchesExec: target_batch_size=2 | ||
10)----------RepartitionExec: partitioning=Hash([t1_id@0], 2), input_partitions=2 | ||
11)------------RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1 | ||
|
@@ -2967,10 +2967,10 @@ explain SELECT t1_id, t1_name FROM left_semi_anti_join_table_t1 t1 WHERE t1_id I | |
---- | ||
physical_plan | ||
01)SortPreservingMergeExec: [t1_id@0 ASC NULLS LAST] | ||
02)--SortExec: expr=[t1_id@0 ASC NULLS LAST], preserve_partitioning=[true] | ||
03)----CoalesceBatchesExec: target_batch_size=2 | ||
04)------HashJoinExec: mode=CollectLeft, join_type=RightSemi, on=[(t2_id@0, t1_id@0)] | ||
05)--------MemoryExec: partitions=1, partition_sizes=[1] | ||
02)--CoalesceBatchesExec: target_batch_size=2 | ||
03)----HashJoinExec: mode=CollectLeft, join_type=RightSemi, on=[(t2_id@0, t1_id@0)] | ||
04)------MemoryExec: partitions=1, partition_sizes=[1] | ||
05)------SortExec: expr=[t1_id@0 ASC NULLS LAST], preserve_partitioning=[true] | ||
06)--------RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1 | ||
07)----------MemoryExec: partitions=1, partition_sizes=[1] | ||
|
||
|
@@ -3003,10 +3003,10 @@ explain SELECT t1_id, t1_name FROM left_semi_anti_join_table_t1 t1 LEFT SEMI JOI | |
---- | ||
physical_plan | ||
01)SortPreservingMergeExec: [t1_id@0 ASC NULLS LAST] | ||
02)--SortExec: expr=[t1_id@0 ASC NULLS LAST], preserve_partitioning=[true] | ||
03)----CoalesceBatchesExec: target_batch_size=2 | ||
04)------HashJoinExec: mode=CollectLeft, join_type=RightSemi, on=[(t2_id@0, t1_id@0)] | ||
05)--------MemoryExec: partitions=1, partition_sizes=[1] | ||
02)--CoalesceBatchesExec: target_batch_size=2 | ||
03)----HashJoinExec: mode=CollectLeft, join_type=RightSemi, on=[(t2_id@0, t1_id@0)] | ||
04)------MemoryExec: partitions=1, partition_sizes=[1] | ||
05)------SortExec: expr=[t1_id@0 ASC NULLS LAST], preserve_partitioning=[true] | ||
06)--------RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1 | ||
07)----------MemoryExec: partitions=1, partition_sizes=[1] | ||
|
||
|
@@ -3061,13 +3061,13 @@ explain SELECT t1_id, t1_name, t1_int FROM right_semi_anti_join_table_t1 t1 WHER | |
---- | ||
physical_plan | ||
01)SortPreservingMergeExec: [t1_id@0 ASC NULLS LAST] | ||
02)--SortExec: expr=[t1_id@0 ASC NULLS LAST], preserve_partitioning=[true] | ||
03)----CoalesceBatchesExec: target_batch_size=2 | ||
04)------HashJoinExec: mode=Partitioned, join_type=RightSemi, on=[(t2_id@0, t1_id@0)], filter=t2_name@1 != t1_name@0 | ||
05)--------CoalesceBatchesExec: target_batch_size=2 | ||
06)----------RepartitionExec: partitioning=Hash([t2_id@0], 2), input_partitions=2 | ||
07)------------RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1 | ||
08)--------------MemoryExec: partitions=1, partition_sizes=[1] | ||
02)--CoalesceBatchesExec: target_batch_size=2 | ||
03)----HashJoinExec: mode=Partitioned, join_type=RightSemi, on=[(t2_id@0, t1_id@0)], filter=t2_name@1 != t1_name@0 | ||
04)------CoalesceBatchesExec: target_batch_size=2 | ||
05)--------RepartitionExec: partitioning=Hash([t2_id@0], 2), input_partitions=2 | ||
06)----------RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1 | ||
07)------------MemoryExec: partitions=1, partition_sizes=[1] | ||
08)------SortExec: expr=[t1_id@0 ASC NULLS LAST], preserve_partitioning=[true] | ||
09)--------CoalesceBatchesExec: target_batch_size=2 | ||
10)----------RepartitionExec: partitioning=Hash([t1_id@0], 2), input_partitions=2 | ||
11)------------RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1 | ||
|
@@ -3083,13 +3083,13 @@ explain SELECT t1_id, t1_name, t1_int FROM right_semi_anti_join_table_t2 t2 RIGH | |
---- | ||
physical_plan | ||
01)SortPreservingMergeExec: [t1_id@0 ASC NULLS LAST] | ||
02)--SortExec: expr=[t1_id@0 ASC NULLS LAST], preserve_partitioning=[true] | ||
03)----CoalesceBatchesExec: target_batch_size=2 | ||
04)------HashJoinExec: mode=Partitioned, join_type=RightSemi, on=[(t2_id@0, t1_id@0)], filter=t2_name@0 != t1_name@1 | ||
05)--------CoalesceBatchesExec: target_batch_size=2 | ||
06)----------RepartitionExec: partitioning=Hash([t2_id@0], 2), input_partitions=2 | ||
07)------------RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1 | ||
08)--------------MemoryExec: partitions=1, partition_sizes=[1] | ||
02)--CoalesceBatchesExec: target_batch_size=2 | ||
03)----HashJoinExec: mode=Partitioned, join_type=RightSemi, on=[(t2_id@0, t1_id@0)], filter=t2_name@0 != t1_name@1 | ||
04)------CoalesceBatchesExec: target_batch_size=2 | ||
05)--------RepartitionExec: partitioning=Hash([t2_id@0], 2), input_partitions=2 | ||
06)----------RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1 | ||
07)------------MemoryExec: partitions=1, partition_sizes=[1] | ||
08)------SortExec: expr=[t1_id@0 ASC NULLS LAST], preserve_partitioning=[true] | ||
09)--------CoalesceBatchesExec: target_batch_size=2 | ||
10)----------RepartitionExec: partitioning=Hash([t1_id@0], 2), input_partitions=2 | ||
11)------------RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1 | ||
|
@@ -3143,10 +3143,10 @@ explain SELECT t1_id, t1_name, t1_int FROM right_semi_anti_join_table_t1 t1 WHER | |
---- | ||
physical_plan | ||
01)SortPreservingMergeExec: [t1_id@0 ASC NULLS LAST] | ||
02)--SortExec: expr=[t1_id@0 ASC NULLS LAST], preserve_partitioning=[true] | ||
03)----CoalesceBatchesExec: target_batch_size=2 | ||
04)------HashJoinExec: mode=CollectLeft, join_type=RightSemi, on=[(t2_id@0, t1_id@0)], filter=t2_name@1 != t1_name@0 | ||
05)--------MemoryExec: partitions=1, partition_sizes=[1] | ||
02)--CoalesceBatchesExec: target_batch_size=2 | ||
03)----HashJoinExec: mode=CollectLeft, join_type=RightSemi, on=[(t2_id@0, t1_id@0)], filter=t2_name@1 != t1_name@0 | ||
04)------MemoryExec: partitions=1, partition_sizes=[1] | ||
05)------SortExec: expr=[t1_id@0 ASC NULLS LAST], preserve_partitioning=[true] | ||
06)--------RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1 | ||
07)----------MemoryExec: partitions=1, partition_sizes=[1] | ||
|
||
|
@@ -3160,10 +3160,10 @@ explain SELECT t1_id, t1_name, t1_int FROM right_semi_anti_join_table_t2 t2 RIGH | |
---- | ||
physical_plan | ||
01)SortPreservingMergeExec: [t1_id@0 ASC NULLS LAST] | ||
02)--SortExec: expr=[t1_id@0 ASC NULLS LAST], preserve_partitioning=[true] | ||
03)----CoalesceBatchesExec: target_batch_size=2 | ||
04)------HashJoinExec: mode=CollectLeft, join_type=RightSemi, on=[(t2_id@0, t1_id@0)], filter=t2_name@0 != t1_name@1 | ||
05)--------MemoryExec: partitions=1, partition_sizes=[1] | ||
02)--CoalesceBatchesExec: target_batch_size=2 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These plans imply that the sort has been pushed into the second (probe) input which makes sense I think : https://docs.rs/datafusion/latest/datafusion/physical_plan/joins/struct.HashJoinExec.html |
||
03)----HashJoinExec: mode=CollectLeft, join_type=RightSemi, on=[(t2_id@0, t1_id@0)], filter=t2_name@0 != t1_name@1 | ||
04)------MemoryExec: partitions=1, partition_sizes=[1] | ||
05)------SortExec: expr=[t1_id@0 ASC NULLS LAST], preserve_partitioning=[true] | ||
06)--------RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1 | ||
07)----------MemoryExec: partitions=1, partition_sizes=[1] | ||
|
||
|
@@ -4313,3 +4313,86 @@ physical_plan | |
04)------HashJoinExec: mode=CollectLeft, join_type=Inner, on=[(binary_col@0, binary_col@0)] | ||
05)--------MemoryExec: partitions=1, partition_sizes=[1] | ||
06)--------MemoryExec: partitions=1, partition_sizes=[1] | ||
|
||
# Test hash join sort push down | ||
# Issue: https://github.com/apache/datafusion/issues/13559 | ||
statement ok | ||
CREATE TABLE test(a INT, b INT, c INT) | ||
|
||
statement ok | ||
insert into test values (1,2,3), (4,5,6), (null, 7, 8), (8, null, 9), (9, 10, null) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cc @alamb, are these tests appropriate? |
||
|
||
statement ok | ||
set datafusion.execution.target_partitions = 2; | ||
|
||
query TT | ||
explain select * from test where a in (select a from test where b > 3) order by c desc nulls first; | ||
---- | ||
logical_plan | ||
01)Sort: test.c DESC NULLS FIRST | ||
02)--LeftSemi Join: test.a = __correlated_sq_1.a | ||
03)----TableScan: test projection=[a, b, c] | ||
04)----SubqueryAlias: __correlated_sq_1 | ||
05)------Projection: test.a | ||
06)--------Filter: test.b > Int32(3) | ||
07)----------TableScan: test projection=[a, b] | ||
physical_plan | ||
01)SortPreservingMergeExec: [c@2 DESC] | ||
02)--CoalesceBatchesExec: target_batch_size=3 | ||
03)----HashJoinExec: mode=Partitioned, join_type=RightSemi, on=[(a@0, a@0)] | ||
04)------CoalesceBatchesExec: target_batch_size=3 | ||
05)--------RepartitionExec: partitioning=Hash([a@0], 2), input_partitions=2 | ||
06)----------CoalesceBatchesExec: target_batch_size=3 | ||
07)------------FilterExec: b@1 > 3, projection=[a@0] | ||
08)--------------RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1 | ||
09)----------------MemoryExec: partitions=1, partition_sizes=[1] | ||
10)------SortExec: expr=[c@2 DESC], preserve_partitioning=[true] | ||
11)--------CoalesceBatchesExec: target_batch_size=3 | ||
12)----------RepartitionExec: partitioning=Hash([a@0], 2), input_partitions=2 | ||
13)------------RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1 | ||
14)--------------MemoryExec: partitions=1, partition_sizes=[1] | ||
|
||
query TT | ||
explain select * from test where a in (select a from test where b > 3) order by c desc nulls last; | ||
---- | ||
logical_plan | ||
01)Sort: test.c DESC NULLS LAST | ||
02)--LeftSemi Join: test.a = __correlated_sq_1.a | ||
03)----TableScan: test projection=[a, b, c] | ||
04)----SubqueryAlias: __correlated_sq_1 | ||
05)------Projection: test.a | ||
06)--------Filter: test.b > Int32(3) | ||
07)----------TableScan: test projection=[a, b] | ||
physical_plan | ||
01)SortPreservingMergeExec: [c@2 DESC NULLS LAST] | ||
02)--CoalesceBatchesExec: target_batch_size=3 | ||
03)----HashJoinExec: mode=Partitioned, join_type=RightSemi, on=[(a@0, a@0)] | ||
04)------CoalesceBatchesExec: target_batch_size=3 | ||
05)--------RepartitionExec: partitioning=Hash([a@0], 2), input_partitions=2 | ||
06)----------CoalesceBatchesExec: target_batch_size=3 | ||
07)------------FilterExec: b@1 > 3, projection=[a@0] | ||
08)--------------RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1 | ||
09)----------------MemoryExec: partitions=1, partition_sizes=[1] | ||
10)------SortExec: expr=[c@2 DESC NULLS LAST], preserve_partitioning=[true] | ||
11)--------CoalesceBatchesExec: target_batch_size=3 | ||
12)----------RepartitionExec: partitioning=Hash([a@0], 2), input_partitions=2 | ||
13)------------RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1 | ||
14)--------------MemoryExec: partitions=1, partition_sizes=[1] | ||
|
||
query III | ||
select * from test where a in (select a from test where b > 3) order by c desc nulls first; | ||
---- | ||
9 10 NULL | ||
4 5 6 | ||
|
||
query III | ||
select * from test where a in (select a from test where b > 3) order by c desc nulls last; | ||
---- | ||
4 5 6 | ||
9 10 NULL | ||
|
||
statement ok | ||
DROP TABLE test | ||
|
||
statement ok | ||
set datafusion.execution.target_partitions = 1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be great eventually to get this kind of operator specific logic into the operators (e.g. some method in HashJoinExec). Definitely not in this PR, but having assumptions about the operator separate from its implementation gives us a larger chance of introducing inconsistencies I think