shivadarshan-devadiga commented on code in PR #60756:
URL: https://github.com/apache/spark/pull/60756#discussion_r4653787816
##########
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 = {
Review Comment:
Done. Both IN cases now call one `padInList(i, c, values, padCharCol,
truncate)`, which measures and pads the constants. With `truncate = false`, it
pads the value like read-side padding, as `padAttrLitCmp` does. The column case
is unchanged in behavior.
--
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]