maropu commented on a change in pull request #29107: URL: https://github.com/apache/spark/pull/29107#discussion_r458470991
########## File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ResolveUnion.scala ########## @@ -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 """ + Review comment: How about making it consistent (using ` for wrapping a column name)? ``` throw new AnalysisException( s"Cannot resolve column name `${lattr.name}` among " + s"(${rightOutputAttrs.map(_.name).mkString(", ")})") ``` https://github.com/apache/spark/pull/29107/files#diff-1d14ac233eac6f233c027dba0bdf871dR341 ########## File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ResolveUnion.scala ########## @@ -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: LogicalPlan, right: LogicalPlan) => Review comment: nit: `{ (left: LogicalPlan, right: LogicalPlan) =>` -> `{ (left, right) =>` ########## File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/TypeCoercion.scala ########## @@ -342,9 +342,10 @@ object TypeCoercion { Intersect(newChildren.head, newChildren.last, isAll) case s: Union if s.childrenResolved && - s.children.forall(_.output.length == s.children.head.output.length) && !s.resolved => + s.children.forall(_.output.length == s.children.head.output.length) && !s.resolved && + !s.byName => Review comment: nit: Since `s.byName` is constant, how about evaluating it first? ``` case s @ Union(_, false, _) if s.childrenResolved && !s.byName && s.children.forall(_.output.length == s.children.head.output.length) && !s.resolved => ``` ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
