-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[SPARK-26138][SQL] Pushdown limit through InnerLike when condition is empty #31567
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
Changes from all commits
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 |
|---|---|---|
|
|
@@ -539,17 +539,22 @@ object LimitPushDown extends Rule[LogicalPlan] { | |
| // pushdown Limit. | ||
| case LocalLimit(exp, u: Union) => | ||
| LocalLimit(exp, u.copy(children = u.children.map(maybePushLocalLimit(exp, _)))) | ||
| // Add extra limits below OUTER JOIN. For LEFT OUTER and RIGHT OUTER JOIN we push limits to | ||
| // the left and right sides, respectively. It's not safe to push limits below FULL OUTER | ||
| // JOIN in the general case without a more invasive rewrite. | ||
| // Add extra limits below JOIN. For LEFT OUTER and RIGHT OUTER JOIN we push limits to | ||
| // the left and right sides, respectively. For INNER and CROSS JOIN we push limits to | ||
| // both the left and right sides if join condition is empty. It's not safe to push limits | ||
| // below FULL OUTER JOIN in the general case without a more invasive rewrite. | ||
| // We also need to ensure that this limit pushdown rule will not eventually introduce limits | ||
| // on both sides if it is applied multiple times. Therefore: | ||
| // - If one side is already limited, stack another limit on top if the new limit is smaller. | ||
| // The redundant limit will be collapsed by the CombineLimits rule. | ||
| case LocalLimit(exp, join @ Join(left, right, joinType, _, _)) => | ||
| case LocalLimit(exp, join @ Join(left, right, joinType, conditionOpt, _)) => | ||
| val newJoin = joinType match { | ||
| case RightOuter => join.copy(right = maybePushLocalLimit(exp, right)) | ||
| case LeftOuter => join.copy(left = maybePushLocalLimit(exp, left)) | ||
|
Contributor
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. It seems that we can also push down limit into left side, for LEFT SEMI and LEFT ANTI join, right?
Member
Author
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. It seems we can not pushdown spark.range(20).selectExpr("id % 10 as id").repartition(1).write.saveAsTable("t1")
spark.range(5, 9, 1).repartition(1).write.saveAsTable("t2")
val df = spark.sql("select * from t1 LEFT SEMI JOIN t2 on t1.id = t2.id limit 3")
df.explain()
df.showCurrent: Pushdown:
Contributor
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. @wangyum - yes, you are right. My bad. LEFT OUTER/RIGHT OUTER join will output the left/right side rows anyway no matter of being matched or not. So they are safe for limit push down, but not LEFT SEMI/LEFT ANTI.
Contributor
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. Actually, what about LEFT SEMI / LEFT ANTI join without condition? They should be planned into physical operator |
||
| case _: InnerLike if conditionOpt.isEmpty => | ||
| join.copy( | ||
| left = maybePushLocalLimit(exp, left), | ||
| right = maybePushLocalLimit(exp, right)) | ||
| case _ => join | ||
| } | ||
| LocalLimit(exp, newJoin) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,7 +22,7 @@ import org.apache.spark.sql.catalyst.analysis.EliminateSubqueryAliases | |
| import org.apache.spark.sql.catalyst.dsl.expressions._ | ||
| import org.apache.spark.sql.catalyst.dsl.plans._ | ||
| import org.apache.spark.sql.catalyst.expressions.Add | ||
| import org.apache.spark.sql.catalyst.plans.{FullOuter, LeftOuter, PlanTest, RightOuter} | ||
| import org.apache.spark.sql.catalyst.plans.{Cross, FullOuter, Inner, LeftOuter, PlanTest, RightOuter} | ||
| import org.apache.spark.sql.catalyst.plans.logical._ | ||
| import org.apache.spark.sql.catalyst.rules._ | ||
|
|
||
|
|
@@ -194,4 +194,22 @@ class LimitPushdownSuite extends PlanTest { | |
| LocalLimit(1, y.groupBy(Symbol("b"))(count(1))))).analyze | ||
| comparePlans(expected2, optimized2) | ||
| } | ||
|
|
||
| test("SPARK-26138: pushdown limit through InnerLike when condition is empty") { | ||
|
Member
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. Shall we have a e2e test to check answer too? |
||
| Seq(Cross, Inner).foreach { joinType => | ||
| val originalQuery = x.join(y, joinType).limit(1) | ||
| val optimized = Optimize.execute(originalQuery.analyze) | ||
| val correctAnswer = Limit(1, LocalLimit(1, x).join(LocalLimit(1, y), joinType)).analyze | ||
| comparePlans(optimized, correctAnswer) | ||
| } | ||
| } | ||
|
|
||
| test("SPARK-26138: Should not pushdown limit through InnerLike when condition is not empty") { | ||
| Seq(Cross, Inner).foreach { joinType => | ||
| val originalQuery = x.join(y, joinType, Some("x.a".attr === "y.b".attr)).limit(1) | ||
| val optimized = Optimize.execute(originalQuery.analyze) | ||
| val correctAnswer = Limit(1, x.join(y, joinType, Some("x.a".attr === "y.b".attr))).analyze | ||
| comparePlans(optimized, correctAnswer) | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.