-
Notifications
You must be signed in to change notification settings - Fork 29.3k
[SPARK-32941][SQL] Optimize UpdateFields expression chain and put the rule early in Analysis phase #29812
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-32941][SQL] Optimize UpdateFields expression chain and put the rule early in Analysis phase #29812
Changes from 2 commits
74cf2dd
00acff9
cb8872c
82ad8c8
38bdefd
f41900c
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 |
|---|---|---|
|
|
@@ -17,16 +17,32 @@ | |
|
|
||
| package org.apache.spark.sql.catalyst.optimizer | ||
|
|
||
| import org.apache.spark.sql.catalyst.expressions.WithFields | ||
| import scala.collection.mutable | ||
|
|
||
| import org.apache.spark.sql.catalyst.expressions.{Expression, WithFields} | ||
| import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan | ||
| import org.apache.spark.sql.catalyst.rules.Rule | ||
| import org.apache.spark.sql.internal.SQLConf | ||
|
|
||
|
|
||
| /** | ||
| * Combines all adjacent [[WithFields]] expression into a single [[WithFields]] expression. | ||
| * Optimizes [[WithFields]] expression chains. | ||
| */ | ||
| object CombineWithFields extends Rule[LogicalPlan] { | ||
| object OptimizeWithFields extends Rule[LogicalPlan] { | ||
| lazy val resolver = SQLConf.get.resolver | ||
|
|
||
| def apply(plan: LogicalPlan): LogicalPlan = plan transformAllExpressions { | ||
| case WithFields(structExpr, names, values) if names.distinct.length != names.length => | ||
| val newNames = mutable.ArrayBuffer.empty[String] | ||
| val newValues = mutable.ArrayBuffer.empty[Expression] | ||
| names.zip(values).reverse.foreach { case (name, value) => | ||
| if (newNames.find(resolver(_, name)).isEmpty) { | ||
|
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. this is a bit inefficient. Shall we build a set with lowercased names if case sensitivity is false?
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. Added a set for case-sensitive case. |
||
| newNames += name | ||
| newValues += value | ||
| } | ||
| } | ||
| WithFields(structExpr, names = newNames.reverse.toSeq, valExprs = newValues.reverse.toSeq) | ||
|
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. For my understanding, can you explain how we expect to benefit from this optimization? I ask because we do this kind of deduplication inside of
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. You are right. It is eventually the same. But for some cases, before we extend I will run these rules in #29587 to simplify expression tree before entering optimizer.
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. Okay, so I took a look at the PR you linked and left a related comment there. I don't think you actually need this optimization for #29587 This optimization is only useful if someone uses Unfortunately, "bad" end-users might still update the same field multiple times. Assuming we should optimize for such users (not sure), since this batch is only applied half-way through the optimization cycle anyway, I think we could just move up the
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. Actually I'd like to run these rules to simplify
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. ahh I see, yes, in the analysis stage this would likely be helpful! Okay in that case, could this PR wait till #29795 goes in? I'm refactoring
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. I'm fine to wait until #29795. |
||
|
|
||
| case WithFields(WithFields(struct, names1, valExprs1), names2, valExprs2) => | ||
| WithFields(struct, names1 ++ names2, valExprs1 ++ valExprs2) | ||
| } | ||
|
|
||
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.
could this
casestatement be after the nextcasestatement? So that we combine the chains first before deduplicating?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.
We don't run this rule just once, so the order should be fine.