shivadarshan-devadiga commented on code in PR #60756: URL: https://github.com/apache/spark/pull/60756#discussion_r4652421968
########## sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/SetOperationCharPadding.scala: ########## @@ -0,0 +1,161 @@ +/* + * 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 scala.collection.mutable + +import org.apache.spark.sql.catalyst.expressions.{Alias, Attribute, Expression, ExprId, NamedExpression} +import org.apache.spark.sql.catalyst.plans.logical.{CTERelationRef, Except, Intersect, LogicalPlan, Project, SetOperation, Union, UnionLoop, UnionLoopRef} +import org.apache.spark.sql.catalyst.rules.Rule +import org.apache.spark.sql.catalyst.util.CharVarcharUtils +import org.apache.spark.sql.internal.SQLConf +import org.apache.spark.sql.types.DataType + +/** + * The SQL standard gives a set operation of CHAR(n) and CHAR(m) the length max(n, m). When the + * children of a Union, Intersect or Except have CHAR columns or fields of different lengths at + * the same position, this pads the values of the shorter children to the widest length, in a + * Project over each child that needs it, as [[TypeCoercionBase.WidenSetOperationTypes]] casts + * them to the widest type. The output of the set operation, which comes from its first child, + * then has the widest length. Like read-side padding, the padding does not truncate longer values. + * + * During analysis, such a set operation is not resolved until its children are padded, see + * [[isAligned]], so that nothing above it is resolved with the shorter length of its first child. + * The children are padded once the set operation is resolved otherwise, after the widening of + * their types and the deduplication of their output: in [[PadSetOperationCharLengths]] by the + * fixed-point analyzer, and in `SetOperationLikeResolver` by the single-pass resolver, both with + * [[padChildren]]. + */ +object SetOperationCharPadding { + + /** + * Whether the CHAR lengths of the children of a set operation are the same at each position, so + * that it needs no padding. As their types, the CHAR types of the children of a set operation + * stay the same after analysis, which optimizer rules keep. True if the children are not + * resolved yet. + */ + def isAligned(children: Seq[LogicalPlan]): Boolean = !needsPadding(children) Review Comment: Thanks, confirmed. As described above, the gate now applies only during analysis, so the optimizer can make the metadata of the children differ without making the union unresolved. Your query is in the set-operations test and returns its two rows. -- 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. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
