Skip to content

Commit e5cdc17

Browse files
authored
feat: support push down limit when full join (#12963)
1 parent 747001a commit e5cdc17

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

datafusion/optimizer/src/push_down_limit.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ fn push_down_join(mut join: Join, limit: usize) -> Transformed<Join> {
263263
match join.join_type {
264264
Left => (Some(limit), None),
265265
Right => (None, Some(limit)),
266+
Full => (Some(limit), Some(limit)),
266267
_ => (None, None),
267268
}
268269
};

datafusion/sqllogictest/test_files/joins.slt

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4187,4 +4187,90 @@ physical_plan
41874187
02)--HashJoinExec: mode=CollectLeft, join_type=Inner, on=[(b@1, y@1)], filter=a@0 < x@1
41884188
03)----MemoryExec: partitions=1, partition_sizes=[0]
41894189
04)----SortExec: expr=[x@0 ASC NULLS LAST], preserve_partitioning=[false]
4190-
05)------MemoryExec: partitions=1, partition_sizes=[0]
4190+
05)------MemoryExec: partitions=1, partition_sizes=[0]
4191+
4192+
# Test full join with limit
4193+
statement ok
4194+
CREATE TABLE t0(c1 INT UNSIGNED, c2 INT UNSIGNED)
4195+
AS VALUES
4196+
(1, 1),
4197+
(2, 2),
4198+
(3, 3),
4199+
(4, 4);
4200+
4201+
statement ok
4202+
CREATE TABLE t1(c1 INT UNSIGNED, c2 INT UNSIGNED, c3 BOOLEAN)
4203+
AS VALUES
4204+
(2, 2, true),
4205+
(2, 2, false),
4206+
(3, 3, true),
4207+
(3, 3, false);
4208+
4209+
query IIIIB
4210+
SELECT * FROM t0 FULL JOIN t1 ON t0.c1 = t1.c1 LIMIT 2;
4211+
----
4212+
2 2 2 2 true
4213+
2 2 2 2 false
4214+
4215+
query IIIIB
4216+
SELECT * FROM t0 FULL JOIN t1 ON t0.c2 >= t1.c2 LIMIT 2;
4217+
----
4218+
2 2 2 2 true
4219+
3 3 2 2 true
4220+
4221+
query IIIIB
4222+
SELECT * FROM t0 FULL JOIN t1 ON t0.c1 = t1.c1 AND t0.c2 >= t1.c2 LIMIT 2;
4223+
----
4224+
2 2 2 2 true
4225+
2 2 2 2 false
4226+
4227+
## Test !join.on.is_empty() && join.filter.is_none()
4228+
query TT
4229+
EXPLAIN SELECT * FROM t0 FULL JOIN t1 ON t0.c1 = t1.c1 LIMIT 2;
4230+
----
4231+
logical_plan
4232+
01)Limit: skip=0, fetch=2
4233+
02)--Full Join: t0.c1 = t1.c1
4234+
03)----Limit: skip=0, fetch=2
4235+
04)------TableScan: t0 projection=[c1, c2], fetch=2
4236+
05)----Limit: skip=0, fetch=2
4237+
06)------TableScan: t1 projection=[c1, c2, c3], fetch=2
4238+
physical_plan
4239+
01)CoalesceBatchesExec: target_batch_size=3, fetch=2
4240+
02)--HashJoinExec: mode=CollectLeft, join_type=Full, on=[(c1@0, c1@0)]
4241+
03)----MemoryExec: partitions=1, partition_sizes=[1]
4242+
04)----MemoryExec: partitions=1, partition_sizes=[1]
4243+
4244+
## Test join.on.is_empty() && join.filter.is_some()
4245+
query TT
4246+
EXPLAIN SELECT * FROM t0 FULL JOIN t1 ON t0.c2 >= t1.c2 LIMIT 2;
4247+
----
4248+
logical_plan
4249+
01)Limit: skip=0, fetch=2
4250+
02)--Full Join: Filter: t0.c2 >= t1.c2
4251+
03)----Limit: skip=0, fetch=2
4252+
04)------TableScan: t0 projection=[c1, c2], fetch=2
4253+
05)----Limit: skip=0, fetch=2
4254+
06)------TableScan: t1 projection=[c1, c2, c3], fetch=2
4255+
physical_plan
4256+
01)GlobalLimitExec: skip=0, fetch=2
4257+
02)--NestedLoopJoinExec: join_type=Full, filter=c2@0 >= c2@1
4258+
03)----MemoryExec: partitions=1, partition_sizes=[1]
4259+
04)----MemoryExec: partitions=1, partition_sizes=[1]
4260+
4261+
## Test !join.on.is_empty() && join.filter.is_some()
4262+
query TT
4263+
EXPLAIN SELECT * FROM t0 FULL JOIN t1 ON t0.c1 = t1.c1 AND t0.c2 >= t1.c2 LIMIT 2;
4264+
----
4265+
logical_plan
4266+
01)Limit: skip=0, fetch=2
4267+
02)--Full Join: t0.c1 = t1.c1 Filter: t0.c2 >= t1.c2
4268+
03)----Limit: skip=0, fetch=2
4269+
04)------TableScan: t0 projection=[c1, c2], fetch=2
4270+
05)----Limit: skip=0, fetch=2
4271+
06)------TableScan: t1 projection=[c1, c2, c3], fetch=2
4272+
physical_plan
4273+
01)CoalesceBatchesExec: target_batch_size=3, fetch=2
4274+
02)--HashJoinExec: mode=CollectLeft, join_type=Full, on=[(c1@0, c1@0)], filter=c2@0 >= c2@1
4275+
03)----MemoryExec: partitions=1, partition_sizes=[1]
4276+
04)----MemoryExec: partitions=1, partition_sizes=[1]

0 commit comments

Comments
 (0)