-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-32308][SQL] Move by-name resolution logic of unionByName from API code to analysis phase #29107
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-32308][SQL] Move by-name resolution logic of unionByName from API code to analysis phase #29107
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
3d5c099
Move by-name resolution logic of Union to analysis phase.
viirya 94087b8
Merge remote-tracking branch 'upstream/master' into move-union-by-name
viirya 93c5ea1
Fix merging conflict.
viirya 8e4867a
Fix test.
viirya 9ddd70f
Move to individual analysis rule.
viirya c23898e
Remove unnecessary change.
viirya c827eb2
Address comments.
viirya 1839987
Remove unused import.
viirya eca8fc6
Address comments.
viirya 0381f5d
Address comments.
viirya 2ab990e
For review comments.
viirya 2a9e1e4
Change condition order.
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
100 changes: 100 additions & 0 deletions
100
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ResolveUnion.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,100 @@ | ||
| /* | ||
| * 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.analysis | ||
|
|
||
| import org.apache.spark.sql.AnalysisException | ||
| import org.apache.spark.sql.catalyst.expressions.{Alias, Literal} | ||
| import org.apache.spark.sql.catalyst.optimizer.CombineUnions | ||
| import org.apache.spark.sql.catalyst.plans.logical.{LogicalPlan, Project, Union} | ||
| import org.apache.spark.sql.catalyst.rules.Rule | ||
| import org.apache.spark.sql.internal.SQLConf | ||
| import org.apache.spark.sql.util.SchemaUtils | ||
|
|
||
| /** | ||
| * Resolves different children of Union to a common set of columns. | ||
| */ | ||
| object ResolveUnion extends Rule[LogicalPlan] { | ||
| private def unionTwoSides( | ||
| left: LogicalPlan, | ||
| right: LogicalPlan, | ||
| allowMissingCol: Boolean): LogicalPlan = { | ||
| val resolver = SQLConf.get.resolver | ||
| val leftOutputAttrs = left.output | ||
| val rightOutputAttrs = right.output | ||
|
|
||
| // Builds a project list for `right` based on `left` output names | ||
| val rightProjectList = leftOutputAttrs.map { lattr => | ||
| rightOutputAttrs.find { rattr => resolver(lattr.name, rattr.name) }.getOrElse { | ||
| if (allowMissingCol) { | ||
| Alias(Literal(null, lattr.dataType), lattr.name)() | ||
| } else { | ||
| throw new AnalysisException( | ||
| s"""Cannot resolve column name "${lattr.name}" among """ + | ||
| s"""(${rightOutputAttrs.map(_.name).mkString(", ")})""") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Delegates failure checks to `CheckAnalysis` | ||
| val notFoundAttrs = rightOutputAttrs.diff(rightProjectList) | ||
| val rightChild = Project(rightProjectList ++ notFoundAttrs, right) | ||
|
|
||
| // Builds a project for `logicalPlan` based on `right` output names, if allowing | ||
| // missing columns. | ||
| val leftChild = if (allowMissingCol) { | ||
| val missingAttrs = notFoundAttrs.map { attr => | ||
| Alias(Literal(null, attr.dataType), attr.name)() | ||
| } | ||
| if (missingAttrs.nonEmpty) { | ||
| Project(leftOutputAttrs ++ missingAttrs, left) | ||
| } else { | ||
| left | ||
| } | ||
| } else { | ||
| left | ||
| } | ||
| Union(leftChild, rightChild) | ||
| } | ||
|
|
||
| // Check column name duplication | ||
| private def checkColumnNames(left: LogicalPlan, right: LogicalPlan): Unit = { | ||
| val caseSensitiveAnalysis = SQLConf.get.caseSensitiveAnalysis | ||
| val leftOutputAttrs = left.output | ||
| val rightOutputAttrs = right.output | ||
|
|
||
| SchemaUtils.checkColumnNameDuplication( | ||
| leftOutputAttrs.map(_.name), | ||
| "in the left attributes", | ||
| caseSensitiveAnalysis) | ||
| SchemaUtils.checkColumnNameDuplication( | ||
| rightOutputAttrs.map(_.name), | ||
| "in the right attributes", | ||
| caseSensitiveAnalysis) | ||
| } | ||
|
|
||
| def apply(plan: LogicalPlan): LogicalPlan = plan resolveOperatorsUp { | ||
| case e if !e.childrenResolved => e | ||
|
|
||
| case Union(children, byName, allowMissingCol) if byName => | ||
| val union = children.reduceLeft { (left, right) => | ||
| checkColumnNames(left, right) | ||
| unionTwoSides(left, right, allowMissingCol) | ||
| } | ||
| CombineUnions(union) | ||
| } | ||
| } | ||
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
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
75 changes: 75 additions & 0 deletions
75
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/analysis/ResolveUnionSuite.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,75 @@ | ||
| /* | ||
| * 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.analysis | ||
|
|
||
| import org.apache.spark.sql.catalyst.expressions._ | ||
| import org.apache.spark.sql.catalyst.plans.logical._ | ||
| import org.apache.spark.sql.catalyst.rules.RuleExecutor | ||
| import org.apache.spark.sql.types._ | ||
|
|
||
| class ResolveUnionSuite extends AnalysisTest { | ||
| test("Resolve Union") { | ||
| val table1 = LocalRelation( | ||
| AttributeReference("i", IntegerType)(), | ||
| AttributeReference("u", DecimalType.SYSTEM_DEFAULT)(), | ||
| AttributeReference("b", ByteType)(), | ||
| AttributeReference("d", DoubleType)()) | ||
| val table2 = LocalRelation( | ||
| AttributeReference("u", DecimalType.SYSTEM_DEFAULT)(), | ||
| AttributeReference("b", ByteType)(), | ||
| AttributeReference("d", DoubleType)(), | ||
| AttributeReference("i", IntegerType)()) | ||
| val table3 = LocalRelation( | ||
| AttributeReference("u", DecimalType.SYSTEM_DEFAULT)(), | ||
| AttributeReference("d", DoubleType)(), | ||
| AttributeReference("i", IntegerType)()) | ||
| val table4 = LocalRelation( | ||
| AttributeReference("u", DecimalType.SYSTEM_DEFAULT)(), | ||
| AttributeReference("i", IntegerType)()) | ||
|
|
||
| val rules = Seq(ResolveUnion) | ||
| val analyzer = new RuleExecutor[LogicalPlan] { | ||
| override val batches = Seq(Batch("Resolution", Once, rules: _*)) | ||
| } | ||
|
|
||
| // By name resolution | ||
| val union1 = Union(table1 :: table2 :: Nil, true, false) | ||
| val analyzed1 = analyzer.execute(union1) | ||
| val projected1 = | ||
| Project(Seq(table2.output(3), table2.output(0), table2.output(1), table2.output(2)), table2) | ||
| val expected1 = Union(table1 :: projected1 :: Nil) | ||
| comparePlans(analyzed1, expected1) | ||
|
|
||
| // Allow missing column | ||
| val union2 = Union(table1 :: table3 :: Nil, true, true) | ||
| val analyzed2 = analyzer.execute(union2) | ||
| val nullAttr1 = Alias(Literal(null, ByteType), "b")() | ||
| val projected2 = | ||
| Project(Seq(table2.output(3), table2.output(0), nullAttr1, table2.output(2)), table3) | ||
| val expected2 = Union(table1 :: projected2 :: Nil) | ||
| comparePlans(analyzed2, expected2) | ||
|
|
||
| // Allow missing column + Allow missing column | ||
| val union3 = Union(union2 :: table4 :: Nil, true, true) | ||
| val analyzed3 = analyzer.execute(union3) | ||
| val nullAttr2 = Alias(Literal(null, DoubleType), "d")() | ||
| val projected3 = | ||
| Project(Seq(table2.output(3), table2.output(0), nullAttr1, nullAttr2), table4) | ||
| val expected3 = Union(table1 :: projected2 :: projected3 :: Nil) | ||
| comparePlans(analyzed3, expected3) | ||
| } | ||
| } |
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
Oops, something went wrong.
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.
How about making it consistent (using ` for wrapping a column name)?
https://github.com/apache/spark/pull/29107/files#diff-1d14ac233eac6f233c027dba0bdf871dR341
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.
ok.