Skip to content

Commit a0a0685

Browse files
committed
SPARK-26138: pushdown limit through InnerLike when condition is empty
1 parent 1fbd576 commit a0a0685

2 files changed

Lines changed: 24 additions & 2 deletions

File tree

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,10 +546,14 @@ object LimitPushDown extends Rule[LogicalPlan] {
546546
// on both sides if it is applied multiple times. Therefore:
547547
// - If one side is already limited, stack another limit on top if the new limit is smaller.
548548
// The redundant limit will be collapsed by the CombineLimits rule.
549-
case LocalLimit(exp, join @ Join(left, right, joinType, _, _)) =>
549+
case LocalLimit(exp, join @ Join(left, right, joinType, conditionOpt, _)) =>
550550
val newJoin = joinType match {
551551
case RightOuter => join.copy(right = maybePushLocalLimit(exp, right))
552552
case LeftOuter => join.copy(left = maybePushLocalLimit(exp, left))
553+
case _: InnerLike if conditionOpt.isEmpty =>
554+
join.copy(
555+
left = maybePushLocalLimit(exp, left),
556+
right = maybePushLocalLimit(exp, right))
553557
case _ => join
554558
}
555559
LocalLimit(exp, newJoin)

sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/optimizer/LimitPushdownSuite.scala

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import org.apache.spark.sql.catalyst.analysis.EliminateSubqueryAliases
2222
import org.apache.spark.sql.catalyst.dsl.expressions._
2323
import org.apache.spark.sql.catalyst.dsl.plans._
2424
import org.apache.spark.sql.catalyst.expressions.Add
25-
import org.apache.spark.sql.catalyst.plans.{FullOuter, LeftOuter, PlanTest, RightOuter}
25+
import org.apache.spark.sql.catalyst.plans.{Cross, FullOuter, Inner, LeftOuter, PlanTest, RightOuter}
2626
import org.apache.spark.sql.catalyst.plans.logical._
2727
import org.apache.spark.sql.catalyst.rules._
2828

@@ -194,4 +194,22 @@ class LimitPushdownSuite extends PlanTest {
194194
LocalLimit(1, y.groupBy(Symbol("b"))(count(1))))).analyze
195195
comparePlans(expected2, optimized2)
196196
}
197+
198+
test("SPARK-26138: pushdown limit through InnerLike when condition is empty") {
199+
Seq(Cross, Inner).foreach { joinType =>
200+
val originalQuery = x.join(y, joinType).limit(1)
201+
val optimized = Optimize.execute(originalQuery.analyze)
202+
val correctAnswer = Limit(1, LocalLimit(1, x).join(LocalLimit(1, y), joinType)).analyze
203+
comparePlans(optimized, correctAnswer)
204+
}
205+
}
206+
207+
test("SPARK-26138: Should not pushdown limit through InnerLike when condition is not empty") {
208+
Seq(Cross, Inner).foreach { joinType =>
209+
val originalQuery = x.join(y, joinType, Some("x.a".attr === "y.b".attr)).limit(1)
210+
val optimized = Optimize.execute(originalQuery.analyze)
211+
val correctAnswer = Limit(1, x.join(y, joinType, Some("x.a".attr === "y.b".attr))).analyze
212+
comparePlans(optimized, correctAnswer)
213+
}
214+
}
197215
}

0 commit comments

Comments
 (0)