-
Notifications
You must be signed in to change notification settings - Fork 29k
[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
Closed
Closed
[SPARK-32941][SQL] Optimize UpdateFields expression chain and put the rule early in Analysis phase #29812
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
74cf2dd
Optimize WithFields expression chain.
viirya 00acff9
Use resolver.
viirya cb8872c
Address comment.
viirya 82ad8c8
Merge remote-tracking branch 'upstream/master' into SPARK-32941
viirya 38bdefd
Simplify UpdateFields in analysis too.
viirya f41900c
Skip the rule if possible.
viirya File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 0 additions & 76 deletions
76
...alyst/src/test/scala/org/apache/spark/sql/catalyst/optimizer/CombineWithFieldsSuite.scala
This file was deleted.
Oops, something went wrong.
154 changes: 154 additions & 0 deletions
154
...lyst/src/test/scala/org/apache/spark/sql/catalyst/optimizer/OptimizeWithFieldsSuite.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.spark.sql.catalyst.optimizer | ||
|
|
||
| import org.apache.spark.sql.catalyst.dsl.expressions._ | ||
| import org.apache.spark.sql.catalyst.dsl.plans._ | ||
| import org.apache.spark.sql.catalyst.expressions.{Alias, GetStructField, Literal, WithFields} | ||
| import org.apache.spark.sql.catalyst.plans.PlanTest | ||
| import org.apache.spark.sql.catalyst.plans.logical._ | ||
| import org.apache.spark.sql.catalyst.rules._ | ||
| import org.apache.spark.sql.internal.SQLConf | ||
|
|
||
| class OptimizeWithFieldsSuite extends PlanTest { | ||
|
|
||
| object Optimize extends RuleExecutor[LogicalPlan] { | ||
| val batches = Batch("OptimizeWithFields", FixedPoint(10), | ||
| OptimizeWithFields, SimplifyExtractValueOps) :: Nil | ||
| } | ||
|
|
||
| private val testRelation = LocalRelation('a.struct('a1.int)) | ||
| private val testRelation2 = LocalRelation('a.struct('a1.int).notNull) | ||
|
|
||
| test("combines two WithFields") { | ||
| val originalQuery = testRelation | ||
| .select(Alias( | ||
| WithFields( | ||
| WithFields( | ||
| 'a, | ||
| Seq("b1"), | ||
| Seq(Literal(4))), | ||
| Seq("c1"), | ||
| Seq(Literal(5))), "out")()) | ||
|
|
||
| val optimized = Optimize.execute(originalQuery.analyze) | ||
| val correctAnswer = testRelation | ||
| .select(Alias(WithFields('a, Seq("b1", "c1"), Seq(Literal(4), Literal(5))), "out")()) | ||
| .analyze | ||
|
|
||
| comparePlans(optimized, correctAnswer) | ||
| } | ||
|
|
||
| test("combines three WithFields") { | ||
| val originalQuery = testRelation | ||
| .select(Alias( | ||
| WithFields( | ||
| WithFields( | ||
| WithFields( | ||
| 'a, | ||
| Seq("b1"), | ||
| Seq(Literal(4))), | ||
| Seq("c1"), | ||
| Seq(Literal(5))), | ||
| Seq("d1"), | ||
| Seq(Literal(6))), "out")()) | ||
|
|
||
| val optimized = Optimize.execute(originalQuery.analyze) | ||
| val correctAnswer = testRelation | ||
| .select(Alias(WithFields('a, Seq("b1", "c1", "d1"), Seq(4, 5, 6).map(Literal(_))), "out")()) | ||
| .analyze | ||
|
|
||
| comparePlans(optimized, correctAnswer) | ||
| } | ||
|
|
||
| test("SPARK-32941: optimize WithFields followed by GetStructField") { | ||
| val originalQuery = testRelation2 | ||
| .select(Alias( | ||
| GetStructField(WithFields( | ||
| 'a, | ||
| Seq("b1"), | ||
| Seq(Literal(4))), 1), "out")()) | ||
|
|
||
| val optimized = Optimize.execute(originalQuery.analyze) | ||
| val correctAnswer = testRelation2 | ||
| .select(Alias(Literal(4), "out")()) | ||
| .analyze | ||
|
|
||
| comparePlans(optimized, correctAnswer) | ||
| } | ||
|
|
||
| test("SPARK-32941: optimize WithFields chain - case insensitive") { | ||
| val originalQuery = testRelation | ||
| .select(Alias( | ||
| WithFields( | ||
| WithFields( | ||
| 'a, | ||
| Seq("b1"), | ||
| Seq(Literal(4))), | ||
| Seq("b1"), | ||
| Seq(Literal(5))), "out1")(), | ||
| Alias( | ||
| WithFields( | ||
| WithFields( | ||
| 'a, | ||
| Seq("b1"), | ||
| Seq(Literal(4))), | ||
| Seq("B1"), | ||
| Seq(Literal(5))), "out2")()) | ||
|
|
||
| val optimized = Optimize.execute(originalQuery.analyze) | ||
| val correctAnswer = testRelation | ||
| .select( | ||
| Alias(WithFields('a, Seq("b1"), Seq(Literal(5))), "out1")(), | ||
| Alias(WithFields('a, Seq("B1"), Seq(Literal(5))), "out2")()) | ||
| .analyze | ||
|
|
||
| comparePlans(optimized, correctAnswer) | ||
| } | ||
|
|
||
| test("SPARK-32941: optimize WithFields chain - case sensitive") { | ||
| withSQLConf(SQLConf.CASE_SENSITIVE.key -> "true") { | ||
| val originalQuery = testRelation | ||
| .select(Alias( | ||
| WithFields( | ||
| WithFields( | ||
| 'a, | ||
| Seq("b1"), | ||
| Seq(Literal(4))), | ||
| Seq("b1"), | ||
| Seq(Literal(5))), "out1")(), | ||
| Alias( | ||
| WithFields( | ||
| WithFields( | ||
| 'a, | ||
| Seq("b1"), | ||
| Seq(Literal(4))), | ||
| Seq("B1"), | ||
| Seq(Literal(5))), "out2")()) | ||
|
|
||
| val optimized = Optimize.execute(originalQuery.analyze) | ||
| val correctAnswer = testRelation | ||
| .select( | ||
| Alias(WithFields('a, Seq("b1"), Seq(Literal(5))), "out1")(), | ||
| Alias(WithFields('a, Seq("b1", "B1"), Seq(Literal(4), Literal(5))), "out2")()) | ||
| .analyze | ||
|
|
||
| comparePlans(optimized, correctAnswer) | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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
WithFieldsalready as part of thefoldLeftoperation here. It will only keep the lastvalExprfor eachname. So I think the optimized logical plan will be the same with or without this optimization in all scenarios? CMIIWUh oh!
There was an error while loading. Please reload this page.
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.
You are right. It is eventually the same. But for some cases, before we extend
WithFields, the expression tree might be very complex. This is coming from improving scalability of #29587. This is applied during I fixed the scalability issue. I found this is useful to reduce the complex ofWithFieldsexpression tree.I will run these rules in #29587 to simplify expression tree before entering optimizer.
Uh oh!
There was an error while loading. Please reload this page.
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.
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
WithFieldsto update the same field multiple times. However, it would simply be better to not update the same field multiple times. At the very least, we should not do this when we re-use this Expression internally within Spark.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
Batch("ReplaceWithFieldsExpression", Once, ReplaceWithFieldsExpression)to get the same benefit (which is just simplified tree). What do you reckon?Uh oh!
There was an error while loading. Please reload this page.
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 I'd like to run these rules to simplify
WithFieldstree early in analysis stage. During fixing scale issue of #29587, I thought that it is very likely to write badWithFieldstree. Once hitting that, it is very hard to debug and the analyzer/optimizer spend a lot of time traversing expression tree. So I think it is very useful keep this rule to simplify the expression tree, but I don't think we want to doReplaceWithFieldsExpressionin analysis stage.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.
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
WithFieldsso this optimization would need to change anyway.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.
I'm fine to wait until #29795.