shivadarshan-devadiga commented on code in PR #60756:
URL: https://github.com/apache/spark/pull/60756#discussion_r4643000491
##########
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)
+ if (columns.forall(_.isEmpty)) {
+ u -> Nil
+ } else {
+ val newChildren = u.children.zipWithIndex.map { case (child, i) =>
+ val projectList = child.output.zip(columns).map {
+ case (attr, Some((rawType, padded))) if !(padded(i) eq attr) =>
+ Alias(padded(i), attr.name)(
+ explicitMetadata =
Some(CharVarcharUtils.withRawType(attr.metadata, rawType)))
+ case (attr, _) => attr
+ }
+ if (projectList == child.output) child else Project(projectList,
child)
+ }
+ val newUnion = u.withNewChildren(newChildren)
+ val attrMapping = u.output.zip(newUnion.output).filterNot { case (a,
b) => a == b }
+ attrMapping.foreach { case (_, attr) => newRawTypes(attr.exprId) =
attr.metadata }
Review Comment:
Fixed: `CTERelationRef` takes its output from the definition when it is
resolved, and the definition's union output is CHAR(5) by then. Covered by a
CTE referenced twice (a self-join, so it isn't inlined before the rule runs).
--
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]