shivadarshan-devadiga commented on code in PR #60756:
URL: https://github.com/apache/spark/pull/60756#discussion_r4642997998
##########
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 }
+ newUnion -> attrMapping
+ }
+ }, skipCond = p => p.analyzed || !p.containsPattern(UNION), canGetOutput =
_.resolved)
+ if (newRawTypes.isEmpty) {
+ newPlan
+ } else {
+ // Not pruned by pattern: a subquery above the Union refers to its
outputs without one.
+ newPlan.resolveOperatorsUpWithSubqueriesAndPruning(_ => true) {
+ case p =>
+ p.transformExpressions {
+ case a: AttributeReference if newRawTypes.get(a.exprId).exists(_
!= a.metadata) =>
Review Comment:
Fixed by the same change. The Alias takes CHAR(5) metadata from the union
output when it is resolved, so nothing has to be propagated afterwards. Covered
by the `SELECT c AS k ... WHERE k = 'a'` case.
--
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]