-
Notifications
You must be signed in to change notification settings - Fork 28.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
[SPARK-26138][SQL] Pushdown limit through InnerLike when condition is empty #31567
Conversation
Kubernetes integration test starting |
Kubernetes integration test status success |
Test build #135154 has finished for PR 31567 at commit
|
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala
Show resolved
Hide resolved
Kubernetes integration test starting |
Kubernetes integration test status success |
Test build #135178 has finished for PR 31567 at commit
|
Looks making sense. cc @cloud-fan, @maropu, @viirya FYI |
@@ -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") { |
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.
Shall we have a e2e test to check answer too?
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.
Looks okay. Better to have e2e test for this.
// 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)) |
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.
It seems that we can also push down limit into left side, for LEFT SEMI and LEFT ANTI join, right?
I can create a minor PR if it's not on your plan @wangyum , thanks.
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.
It seems we can not pushdown LEFT SEMI JOIN
, for example:
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.show
Current:
== Physical Plan ==
AdaptiveSparkPlan isFinalPlan=false
+- CollectLimit 3
+- BroadcastHashJoin [id#10L], [id#11L], LeftSemi, BuildRight, false
:- Filter isnotnull(id#10L)
: +- FileScan parquet default.t1[id#10L] Batched: true, DataFilters: [isnotnull(id#10L)], Format: Parquet, Location: InMemoryFileIndex(1 paths)[file:/Users/yumwang/spark/SPARK-28169/spark-warehouse/org.apache.spark..., PartitionFilters: [], PushedFilters: [IsNotNull(id)], ReadSchema: struct<id:bigint>
+- BroadcastExchange HashedRelationBroadcastMode(List(input[0, bigint, false]),false), [id=#69]
+- Filter isnotnull(id#11L)
+- FileScan parquet default.t2[id#11L] Batched: true, DataFilters: [isnotnull(id#11L)], Format: Parquet, Location: InMemoryFileIndex(1 paths)[file:/Users/yumwang/spark/SPARK-28169/spark-warehouse/org.apache.spark..., PartitionFilters: [], PushedFilters: [IsNotNull(id)], ReadSchema: struct<id:bigint>
+---+
| id|
+---+
| 5|
| 6|
| 7|
+---+
Pushdown:
== Physical Plan ==
AdaptiveSparkPlan isFinalPlan=false
+- CollectLimit 3
+- BroadcastHashJoin [id#10L], [id#11L], LeftSemi, BuildRight, false
:- LocalLimit 3
: +- Filter isnotnull(id#10L)
: +- LocalLimit 3
: +- FileScan parquet default.t1[id#10L] Batched: true, DataFilters: [], Format: Parquet, Location: InMemoryFileIndex(1 paths)[file:/Users/yumwang/spark/SPARK-28169/spark-warehouse/org.apache.spark..., PartitionFilters: [], PushedFilters: [], ReadSchema: struct<id:bigint>
+- BroadcastExchange HashedRelationBroadcastMode(List(input[0, bigint, false]),false), [id=#77]
+- Filter isnotnull(id#11L)
+- FileScan parquet default.t2[id#11L] Batched: true, DataFilters: [isnotnull(id#11L)], Format: Parquet, Location: InMemoryFileIndex(1 paths)[file:/Users/yumwang/spark/SPARK-28169/spark-warehouse/org.apache.spark..., PartitionFilters: [], PushedFilters: [IsNotNull(id)], ReadSchema: struct<id:bigint>
+---+
| id|
+---+
+---+
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.
@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.
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.
Actually, what about LEFT SEMI / LEFT ANTI join without condition? They should be planned into physical operator BroadcastNestedLoopJoin
instead.
okay, it looks fine except for the last @viirya comment. I think it's better to add an end-2-end test just in case, too. |
Kubernetes integration test starting |
Kubernetes integration test status failure |
Test build #135215 has finished for PR 31567 at commit
|
# Conflicts: # sql/core/src/test/scala/org/apache/spark/sql/SQLQuerySuite.scala
Kubernetes integration test starting |
Kubernetes integration test status success |
Test build #135362 has finished for PR 31567 at commit
|
Thank you all. Merged to master. |
late LGTM |
### What changes were proposed in this pull request? I found out during code review of #31567 (comment), where we can push down limit to the left side of LEFT SEMI and LEFT ANTI join, if the join condition is empty. Why it's safe to push down limit: The semantics of LEFT SEMI join without condition: (1). if right side is non-empty, output all rows from left side. (2). if right side is empty, output nothing. The semantics of LEFT ANTI join without condition: (1). if right side is non-empty, output nothing. (2). if right side is empty, output all rows from left side. With the semantics of output all rows from left side or nothing (all or nothing), it's safe to push down limit to left side. NOTE: LEFT SEMI / LEFT ANTI join with non-empty condition is not safe for limit push down, because output can be a portion of left side rows. Reference: physical operator implementation for LEFT SEMI / LEFT ANTI join without condition - https://github.com/apache/spark/blob/master/sql/core/src/main/scala/org/apache/spark/sql/execution/joins/BroadcastNestedLoopJoinExec.scala#L200-L204 . ### Why are the changes needed? Better performance. Save CPU and IO for these joins, as limit being pushed down before join. ### Does this PR introduce _any_ user-facing change? No. ### How was this patch tested? Added unit test in `LimitPushdownSuite.scala` and `SQLQuerySuite.scala`. Closes #31630 from c21/limit-pushdown. Authored-by: Cheng Su <chengsu@fb.com> Signed-off-by: Wenchen Fan <wenchen@databricks.com>
…s empty ### What changes were proposed in this pull request? Similar to #31567. This PR enhances `LimitPushDown` to support push local limit to both sides if it is outer join and join condition is empty. It is safe to push down because without join condition is actually a cross join. ### Why are the changes needed? Improve query performance. For example: <img width="400" alt="image" src="https://user-images.githubusercontent.com/5399861/184052707-ebf50748-6870-4650-84c3-65d79b18ba9d.png"> ### Does this PR introduce _any_ user-facing change? No. ### How was this patch tested? Unit test. Closes #37475 from wangyum/SPARK-40040. Lead-authored-by: Yuming Wang <yumwang@ebay.com> Co-authored-by: Yuming Wang <wgyumg@gmail.com> Signed-off-by: Yuming Wang <yumwang@ebay.com>
What changes were proposed in this pull request?
This pr pushdown limit through InnerLike when condition is empty(Origin pr: #23104). For example:
Before this pr:
After this pr:
Why are the changes needed?
Improve query performance.
Does this PR introduce any user-facing change?
No.
How was this patch tested?
Unit test.