shivadarshan-devadiga commented on code in PR #60756:
URL: https://github.com/apache/spark/pull/60756#discussion_r4642994963
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/ApplyCharTypePadding.scala:
##########
@@ -88,10 +91,58 @@ 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))
+ }
+ }
+
+ /**
+ * The output of a Union takes the raw type of its first child. When its
children have CHAR
+ * columns or fields of different lengths at the same position, this pads
the shorter ones to the
+ * widest length and gives the output that length, as the SQL standard does
for the union of
+ * CHAR(n) and CHAR(m). Otherwise the output keeps the length of the first
child, and comparisons
+ * pad to it, so they do not match the values of a child with longer values.
+ */
+ private def padUnionChildren(plan: LogicalPlan): LogicalPlan = {
+ // The raw types of the outputs that change. transformUpWithNewOutput
gives the references to
+ // them the new expression IDs, but keeps the metadata they had.
+ val newRawTypes = mutable.HashMap.empty[ExprId, Metadata]
+ val newPlan = plan.transformUpWithNewOutput({
+ case u: Union =>
+ val columns =
u.children.map(_.output).transpose.map(CharVarcharUtils.padToWiderCharLength)
Review Comment:
Fixed by computing the width in `Union.mergeChildOutputs`. The inner union's
output is now CHAR(5) when it is resolved, so the outer union with CHAR(4)
picks 5, and 'abcde' is kept. Covered by the nested case in `SPARK-61972: every
reference to a union of char columns gets the widest length`.
--
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]