shivadarshan-devadiga commented on code in PR #60756:
URL: https://github.com/apache/spark/pull/60756#discussion_r4650951653
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/ApplyCharTypePadding.scala:
##########
@@ -88,10 +90,99 @@ 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. One that reads a recursive reference keeps its CHAR
lengths.
+ case op @ (_: Union | _: Intersect | _: Except)
+ if op.resolved && !op.children.exists(Union.readsRecursiveReference)
=>
+ val rawTypes =
CharVarcharUtils.widerCharRawTypes(op.children.map(_.output))
+ if (rawTypes.forall(_.isEmpty)) {
+ op -> Nil
+ } else {
+ val newChildren = op.children.map { child =>
+ // 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 = child.output.zip(rawTypes).map {
+ case (attr, Some(rawType)) =>
paddedColumns.getOrElseUpdate(attr.exprId,
+ CharVarcharUtils.padCharToWiderLength(attr, rawType,
padAllChildren).map {
+ case (padded, paddedRawType) =>
+ Alias(padded, attr.name)(
Review Comment:
Fixed. A column is now padded where it is produced (`padAtSource`): in the
alias of a Project, under operators that pass it through (SubqueryAlias,
Filter, Sort, limits), whose references to it get the new raw type. The column
keeps its expression ID, so the set operation keeps its first child's output,
and `a("c")` resolves. A column produced elsewhere is still padded in a Project
over the child, as before. `a("c")` itself still carries the CHAR(3) it had in
`a`. So that `filter(a("c") === "a")` doesn't pad the literal to 3, comparison
padding now takes the raw type of a CHAR reference from the operator's input,
which is the set operation's CHAR(5). New checks: `a.union(b).select(a("c"))`,
`a.union(b).filter(a("c") === "a")`, and the same for `intersect` and `except`.
--
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]