gengliangwang commented on code in PR #60756:
URL: https://github.com/apache/spark/pull/60756#discussion_r4646391535
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/ApplyCharTypePadding.scala:
##########
@@ -88,10 +88,98 @@ 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.
+ case op @ (_: Union | _: Intersect | _: Except) if op.resolved =>
+ val rawTypes =
op.children.map(_.output).transpose.map(CharVarcharUtils.widerCharRawType)
+ if (rawTypes.forall(_.isEmpty)) {
+ op -> Nil
+ } else {
+ val newChildren = op.children.map { child =>
+ val projectList = child.output.zip(rawTypes).map {
+ case (attr, Some(rawType)) =>
+ CharVarcharUtils.padCharToWiderLength(attr, rawType,
padAllChildren).map {
+ case (padded, paddedRawType) =>
+ Alias(padded, attr.name)(
+ qualifier = attr.qualifier,
+ explicitMetadata =
+ Some(CharVarcharUtils.withRawType(attr.metadata,
paddedRawType)))
+ }.getOrElse(attr)
+ case (attr, _) => attr
+ }
+ if (projectList == child.output) child else Project(projectList,
child)
+ }
+ val newOp = op.withNewChildren(newChildren)
+ // A child can output a column more than once, which gets a padded
column for each
+ // position. References to the column above refer to the first one.
+ newOp -> op.output.zip(newOp.output)
+ .filterNot { case (a, b) => a == b }
Review Comment:
**[P1] Repeated set-op column: references above remap to the padded
position, not the first**
`padSetOperations` builds the attribute mapping with `filterNot { case (a,
b) => a == b }` and only then `distinctBy(exprId)`. Take a first child that
outputs the same column twice, where only a later position gets padded. The
unpadded first pair (c#1 -> c#1) is filtered out, so the only mapping left is
c#1 -> padded alias. `transformUpWithNewOutput` then rewrites every reference
to c#1 above the set operation to the padded column, including the reference
for the first position. This contradicts the code comment "References to the
column above refer to the first one" and the PR's claim that repeated columns
are handled. With the default
`unionIsResolvedWhenDuplicatesPerChildResolved=true`,
`DeduplicateUnionChildOutput` aliases duplicates in a Union, so Union is
protected. Intersect and Except get no such deduplication. Running `distinctBy`
before `filterNot`, or mapping only when the first occurrence changed, fixes it.
**Failure scenario:** t3(c CHAR(3)) and t35(x CHAR(3), y CHAR(5)), both
holding 'a'. `SELECT * FROM (SELECT c, c FROM t3 INTERSECT SELECT x, y FROM
t35) s(p, q)`: position 0 is not padded (c#1) and position 1 becomes c#10,
padded to 5. The mapping is c#1 -> c#10, so both p and q read c#10. p returns
'a ' (5 chars) while its type says CHAR(3), instead of 'a '. Adding `WHERE
p = 'a'` pads the literal to 'a ' and drops the row. EXCEPT has the same
problem.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/cteOperators.scala:
##########
@@ -51,9 +51,9 @@ case class UnionLoop(
copy(anchor = newChildren(0), recursion = newChildren(1))
override protected def computeOutput(): Seq[Attribute] =
- Union.mergeChildOutputs(children.map(_.output)).zip(outputAttrIds).map {
case (x, id) =>
- x.withExprId(id)
- }
+ // ApplyCharTypePadding does not pad the anchor and recursion to the same
CHAR lengths.
+ Union.mergeChildOutputs(children.map(_.output), widenCharLengths = false)
Review Comment:
**[P2] Union inside a recursive term is padded wider than the UnionLoop's
CHAR length**
UnionLoop opts out of widening because its anchor and recursion are not
padded to one length. A plain Union inside the recursive term that reads the
recursive reference (UnionLoopRef, which keeps the anchor's CHAR length) is
still widened by `Union.mergeChildOutputs` and padded by `padSetOperations`.
Recursion rows that come from the recursive reference then carry the wider
padding, while the UnionLoop output, and the literal padding of filters above
it, keep the anchor's shorter CHAR length. Rows that matched before the PR no
longer match. UNION ALL inside a recursive term is allowed:
INVALID_RECURSIVE_REFERENCE.PLACE does not forbid it.
**Failure scenario:** t3(c CHAR(3)) and t5(c CHAR(5)) with 'a'. `WITH
RECURSIVE r(c, n) AS (SELECT c, 0 FROM t3 UNION ALL SELECT c, n + 1 FROM
(SELECT c, n FROM r UNION ALL SELECT c, n FROM t5) WHERE n < 2) SELECT * FROM r
WHERE c = 'a'`. The inner union now pads the r branch to 'a '. The UnionLoop
output stays CHAR(3), so the filter's literal is 'a ', and the recursion rows
derived from r, which matched before the PR, are now dropped.
--
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]