shivadarshan-devadiga commented on code in PR #60756:
URL: https://github.com/apache/spark/pull/60756#discussion_r4643932895
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/ApplyCharTypePadding.scala:
##########
@@ -88,13 +88,47 @@ object ApplyCharTypePadding extends Rule[LogicalPlan] {
})
}, skipCond = p => p.analyzed || p.isInstanceOf[DeleteFromTable],
canGetOutput = _.resolved)
padRowLevelConditions(padDeleteConditions(
- ApplyCharTypePaddingHelper.paddingForStringComparison(newPlan,
padCharCol = false)))
+ ApplyCharTypePaddingHelper.paddingForStringComparison(
+ padUnionChildren(newPlan), padCharCol = false)))
} else {
ApplyCharTypePaddingHelper.paddingForStringComparison(
- plan, padCharCol =
!conf.getConf(SQLConf.LEGACY_NO_CHAR_PADDING_IN_PREDICATE))
+ padUnionChildren(plan),
+ padCharCol =
!conf.getConf(SQLConf.LEGACY_NO_CHAR_PADDING_IN_PREDICATE))
}
}
+ /**
+ * When the children of a Union have CHAR columns or fields of different
lengths at the same
+ * position, the output of the Union has the widest length, see
[[Union.mergeChildOutputs]], as
+ * the SQL standard does for the union of CHAR(n) and CHAR(m). This pads the
values of the
+ * shorter children to that length, so that comparisons padded to it match
them.
+ */
+ private def padUnionChildren(plan: LogicalPlan): LogicalPlan = {
+ plan.transformUpWithNewOutput({
+ // An unresolved Union, or one with children of different arity, fails
in CheckAnalysis.
+ case u: Union if u.resolved =>
+ val rawTypes =
u.children.map(_.output).transpose.map(CharVarcharUtils.widerCharRawType)
+ if (rawTypes.forall(_.isEmpty)) {
+ u -> Nil
+ } else {
+ val newChildren = u.children.map { child =>
+ val projectList = child.output.zip(rawTypes).map {
+ case (attr, Some(rawType)) =>
+ CharVarcharUtils.padCharToWiderLength(attr, rawType).map {
+ case (padded, paddedRawType) =>
+ Alias(padded, attr.name)(explicitMetadata =
Review Comment:
I don't think we should keep the left child's ids here. `a` in
`a.union(b).select(a("c"))` is already analyzed, so `a("c")` carries its
CHAR(3) metadata. If the union output kept that id, the reference would
resolve, but `.where(a("c") === "a")` would pad the literal to 3 against 5-wide
values: the original bug, with no error. Union already behaves this way when
the types are widened: with `x` an INT column and `y` a BIGINT one,
`x.union(y).select(x("c"))` fails today with the same MISSING_ATTRIBUTES,
because `WidenSetOperationTypes` adds `Alias(Cast(...))` with new ids.
`df.union(b).select("c")`, or a reference to the union's own output, works. I
can add a migration-guide note for this if you'd like.
--
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]