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


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/SetOperationCharPadding.scala:
##########
@@ -0,0 +1,160 @@
+/*
+ * 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.{AnalysisHelper, 
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. Only analysis pads the children, so outside of 
it this is true, as
+   * an optimizer rule can make the metadata of the children differ again, 
such as one that pushes
+   * an alias with explicit metadata through a union. True if the children are 
not resolved yet.
+   */
+  def isAligned(children: Seq[LogicalPlan]): Boolean = {
+    !AnalysisHelper.isInAnalyzer || !needsPadding(children)
+  }
+
+  /** Whether the resolved children of a set operation have CHAR lengths to 
pad. */
+  def needsPadding(children: Seq[LogicalPlan]): Boolean = {

Review Comment:
   Thanks, you're right, and my earlier reply that nothing is padded in that 
mode was wrong: the catalog keeps the raw types. `SetOperationCharPadding` now 
checks the same condition as `ApplyCharTypePadding` (`charVarcharAsString && 
!charVarcharStandardSemantics`), and pads nothing then. The set-operations test 
runs your example in the legacy mode: UNION ALL returns the two unpadded 'a' 
values, and UNION one row.



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