shivadarshan-devadiga commented on code in PR #60756:
URL: https://github.com/apache/spark/pull/60756#discussion_r4653790195
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ApplyCharTypePaddingHelper.scala:
##########
@@ -158,33 +189,130 @@ object ApplyCharTypePaddingHelper {
i.copy(value = newChildren.head, list = newChildren.tail)
}
+ // Like the cases above, for a scalar subquery of a CHAR column compared
with a string
+ // literal, a column, or another scalar subquery.
+ // Unlike the cases above, these don't truncate values longer than their
CHAR length.
+ // A comparison of types that don't match is left to CheckAnalysis.
+ case b @ BinaryComparison(e @ CharScalarSubquery(metadata), lit)
+ if b.resolved && canEvaluate(lit) =>
+ padAttrLitCmp(e, metadata, padCharCol, lit, truncate = false)
+ .map(b.withNewChildren)
+ .getOrElse(b)
+
+ case b @ BinaryComparison(lit, e @ CharScalarSubquery(metadata))
+ if b.resolved && canEvaluate(lit) =>
+ padAttrLitCmp(e, metadata, padCharCol, lit, truncate = false)
+ .map(newChildren => b.withNewChildren(newChildren.reverse))
+ .getOrElse(b)
+
+ case b @ BinaryComparison(e1 @ CharOperand(m1), e2 @ CharOperand(m2))
+ if b.resolved && (e1.isInstanceOf[ScalarSubquery] ||
e2.isInstanceOf[ScalarSubquery]) =>
+ b.withNewChildren(CharVarcharUtils.addPaddingInStringComparison(
+ Seq(e1, e2), Seq(m1, m2).map(CharVarcharUtils.getRawType),
padCharCol, truncate = false))
+
+ case i @ In(e @ CharScalarSubquery(metadata), list)
+ if i.resolved && list.forall(canEvaluate) =>
+ CharVarcharUtils.getRawType(metadata).collect {
+ case c: CharType => padInList(i, e, c, padCharCol)
+ }.getOrElse(i)
+
+ case i: InSubquery if i.resolved => padInSubquery(i, padCharCol)
+
case other => other
}
+ /**
+ * Pads each CHAR value or string literal of an IN subquery and the CHAR
column of the subquery
+ * it is compared with, as a comparison with a column pads them, but without
truncating values
+ * longer than their CHAR length, as read-side padding keeps them. The
columns are padded in a
+ * Project over the subquery, without the raw type, so that they are padded
only once.
+ */
+ private def padInSubquery(i: InSubquery, padCharCol: Boolean): InSubquery = {
+ val padded = i.values.zip(i.query.childOutputs).map {
+ case (value @ CharOperand(metadata), col) =>
+ val Seq(newValue, newCol) =
CharVarcharUtils.addPaddingInStringComparison(
+ Seq(value, col),
+ Seq(CharVarcharUtils.getRawType(metadata),
CharVarcharUtils.getRawType(col.metadata)),
+ padCharCol,
+ truncate = false)
+ (newValue, Some(newCol).filterNot(_ eq col))
+ case (lit, col) if lit.dataType.isInstanceOf[StringType] &&
canEvaluate(lit) =>
+ padAttrLitCmp(col, col.metadata, padCharCol, lit, truncate =
false).map {
+ case Seq(newCol, newLit) => (newLit, Some(newCol).filterNot(_ eq
col))
+ }.getOrElse((lit, None))
+ case (value, _) => (value, None)
+ }
+ if (padded.forall(_._2.isEmpty)) {
+ i.copy(values = padded.map(_._1))
+ } else {
+ val plan = i.query.plan
+ // The plan can have more columns than values after de-correlation.
+ val projectList = plan.output.zipWithIndex.map { case (col, idx) =>
+ padded.lift(idx).flatMap(_._2).map { newCol =>
+ Alias(newCol, col.name)(
+ qualifier = col.qualifier,
+ explicitMetadata =
Some(CharVarcharUtils.cleanMetadata(col.metadata)))
+ }.getOrElse(col)
+ }
+ i.copy(values = padded.map(_._1), query =
i.query.withNewPlan(Project(projectList, plan)))
+ }
+ }
+
+ /**
+ * Like the IN case of a column, for a scalar subquery of a CHAR column
compared with a list of
+ * constants, but without truncating the value of the subquery.
+ */
+ private def padInList(i: In, e: Expression, c: CharType, padCharCol:
Boolean): In = {
+ val literalCharLengths =
+ i.list.map(lit =>
Option(lit.eval().asInstanceOf[UTF8String]).map(_.numChars()))
+ val targetLen = (c.length +: literalCharLengths.flatten).max
+ val newValue = if (targetLen > c.length || padCharCol) {
+ CharVarcharUtils.createReadSidePadding(e, CharType(targetLen,
c.collation))
+ } else {
+ e
+ }
+ i.copy(
+ value = newValue,
+ list = i.list.zip(literalCharLengths).map {
+ case (lit, Some(charLength)) => addPadding(lit, charLength, targetLen,
alwaysPad = false)
+ case (lit, None) => lit
+ })
+ }
+
+ /**
+ * Whether a constant evaluates. As in ConstantFolding, a constant that
fails, such as one in a
+ * branch that is never evaluated, is left to the evaluation of the query.
+ */
+ private def canEvaluate(e: Expression): Boolean =
Review Comment:
Done. `canEvaluate` is gone, and the scalar-subquery and IN-subquery cases
use `ExprUtils.tryEvalFoldable` and pass its value on. `padAttrLitCmp` and
`padInList` now take the value of the constant instead of evaluating it again,
so each constant is evaluated once. The column cases still pass `lit.eval()` as
before, which #60750 changes for them separately.
--
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]