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


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/ApplyCharTypePadding.scala:
##########
@@ -88,10 +88,98 @@ object ApplyCharTypePadding extends Rule[LogicalPlan] {
           })
       }, skipCond = p => p.analyzed || p.isInstanceOf[DeleteFromTable], 
canGetOutput = _.resolved)
       padRowLevelConditions(padDeleteConditions(
-        ApplyCharTypePaddingHelper.paddingForStringComparison(newPlan, 
padCharCol = false)))
+        ApplyCharTypePaddingHelper.paddingForStringComparison(
+          padSetOperations(newPlan, padAllChildren = false), padCharCol = 
false)))
     } else {
       ApplyCharTypePaddingHelper.paddingForStringComparison(
-        plan, padCharCol = 
!conf.getConf(SQLConf.LEGACY_NO_CHAR_PADDING_IN_PREDICATE))
+        padSetOperations(plan, padAllChildren = true),
+        padCharCol = 
!conf.getConf(SQLConf.LEGACY_NO_CHAR_PADDING_IN_PREDICATE))
+    }
+  }
+
+  /**
+   * When the children of a Union, Intersect or Except have CHAR columns or 
fields of different
+   * lengths at the same position, its output has the widest length, see
+   * [[Union.mergeChildOutputs]], as the SQL standard does for set operations 
of CHAR(n) and
+   * CHAR(m). This pads the values of the shorter children to that length, so 
that they match the
+   * values of the other children and comparisons padded to it. Like read-side 
padding, it does not
+   * truncate longer values. Without read-side padding, the values of the 
widest children can be
+   * shorter too, so `padAllChildren` pads them as well. Then it narrows the 
columns of views that
+   * store a shorter length.
+   */
+  private def padSetOperations(plan: LogicalPlan, padAllChildren: Boolean): 
LogicalPlan = {
+    val newPlan = plan.transformUpWithNewOutput({
+      // An unresolved set operation, or one with children of different arity, 
fails in
+      // CheckAnalysis.
+      case op @ (_: Union | _: Intersect | _: Except) if op.resolved =>
+        val rawTypes = 
op.children.map(_.output).transpose.map(CharVarcharUtils.widerCharRawType)
+        if (rawTypes.forall(_.isEmpty)) {
+          op -> Nil
+        } else {
+          val newChildren = op.children.map { child =>
+            val projectList = child.output.zip(rawTypes).map {
+              case (attr, Some(rawType)) =>
+                CharVarcharUtils.padCharToWiderLength(attr, rawType, 
padAllChildren).map {
+                  case (padded, paddedRawType) =>
+                    Alias(padded, attr.name)(
+                      qualifier = attr.qualifier,
+                      explicitMetadata =
+                        Some(CharVarcharUtils.withRawType(attr.metadata, 
paddedRawType)))
+                }.getOrElse(attr)
+              case (attr, _) => attr
+            }
+            if (projectList == child.output) child else Project(projectList, 
child)
+          }
+          val newOp = op.withNewChildren(newChildren)
+          // A child can output a column more than once, which gets a padded 
column for each
+          // position. References to the column above refer to the first one.
+          newOp -> op.output.zip(newOp.output)
+            .filterNot { case (a, b) => a == b }

Review Comment:
   Thanks, you're right. Reordering to `distinctBy` before `filterNot` alone 
would still leave a mismatch: a reference to c#1 can't tell the two positions 
apart, so whichever padding it reads, one of p and q would have values of one 
length and a type of another. So the positions that share a column in any child 
now get one raw type, the widest of all of them 
(`CharVarcharUtils.widerCharRawTypes`). `Union`, `Intersect` and `Except` use 
it for their output, and `ApplyCharTypePadding` for the padding. The column 
then gets one padded column for all its positions, and the mapping, now 
`distinctBy` then `filterNot` as you suggested, is unambiguous. For your query, 
p and q are both CHAR(5) with 'a    ', and `WHERE p = 'a'` keeps the row, for 
INTERSECT and EXCEPT. As you noted, a Union gives the positions of a repeated 
column their own ids first, so in UNION ALL p stays CHAR(3) and q is CHAR(5). 
The set-operations test covers all three.



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