shivadarshan-devadiga commented on code in PR #60756:
URL: https://github.com/apache/spark/pull/60756#discussion_r4651837648


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/SetOperationCharPadding.scala:
##########
@@ -0,0 +1,150 @@
+/*
+ * 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, 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.
+ *
+ * Until its children are padded, such a set operation is not resolved, see 
[[isAligned]], so that
+ * nothing above it is resolved with the shorter length of its first child. 
The fixed-point
+ * analyzer pads them in [[PadSetOperationCharLengths]], and the single-pass 
resolver in
+ * `SetOperationLikeResolver`, 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. True if its children are not resolved yet.
+   */
+  def isAligned(children: Seq[LogicalPlan]): Boolean = {
+    !children.forall(_.resolved) ||
+      widerCharRawTypes(children, children.map(_.output)).forall(_.isEmpty)
+  }
+
+  /**
+   * Pads the columns of the children of a set operation that have CHAR 
lengths shorter than the
+   * widest at their position, see [[CharVarcharUtils.widerCharRawTypes]]. 
Without read-side
+   * padding, the values of the widest children can be shorter too, so they 
are padded as well.
+   * `newAlias` builds the alias of a padded column from the padded value, the 
column, and the
+   * raw type of the padded value. Returns the children and their outputs, 
which are the given
+   * ones if no column needs padding.
+   */
+  def padChildren(
+      children: Seq[LogicalPlan],
+      childOutputs: Seq[Seq[Attribute]],
+      newAlias: (Expression, Attribute, DataType) => Alias)
+      : (Seq[LogicalPlan], Seq[Seq[Attribute]]) = {
+    val rawTypes = widerCharRawTypes(children, childOutputs)
+    if (rawTypes.forall(_.isEmpty)) {
+      (children, childOutputs)
+    } else {
+      val alwaysPad = !SQLConf.get.readSideCharPadding
+      children.zip(childOutputs).map { case (child, output) =>
+        // A column at more than one position has one raw type, and gets one 
padded column.
+        val paddedColumns = mutable.HashMap.empty[ExprId, NamedExpression]
+        val projectList = output.zip(rawTypes).map {
+          case (attr, Some(rawType)) =>
+            paddedColumns.getOrElseUpdate(attr.exprId,

Review Comment:
   Fixed. `PadSetOperationCharLengths` now waits until the set operation is 
resolved except for its CHAR lengths. For a Union, that includes the 
deduplication of each child's output that `ResolveReferences` does once the 
types are widened. So the fixed-point analyzer pads after widening and 
deduplication, in the order the single-pass resolver uses. Your `SELECT c, c, 1 
FROM t3 UNION ALL SELECT c3, c5, 1L FROM t35` is in the set-operations test: 
column 0 stays CHAR(3) with 'a  ', column 1 is CHAR(5), and the dual run in the 
tests agrees.



-- 
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]

Reply via email to