shivadarshan-devadiga commented on code in PR #60756:
URL: https://github.com/apache/spark/pull/60756#discussion_r4652708046
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/ApplyCharTypePadding.scala:
##########
@@ -88,10 +89,95 @@ object ApplyCharTypePadding extends Rule[LogicalPlan] {
})
}, skipCond = p => p.analyzed || p.isInstanceOf[DeleteFromTable],
canGetOutput = _.resolved)
padRowLevelConditions(padDeleteConditions(
- ApplyCharTypePaddingHelper.paddingForStringComparison(newPlan,
padCharCol = false)))
+ ApplyCharTypePaddingHelper.paddingForStringComparison(
+ narrowCharColumns(newPlan), padCharCol = false)))
} else {
ApplyCharTypePaddingHelper.paddingForStringComparison(
- plan, padCharCol =
!conf.getConf(SQLConf.LEGACY_NO_CHAR_PADDING_IN_PREDICATE))
+ narrowCharColumns(plan),
+ padCharCol =
!conf.getConf(SQLConf.LEGACY_NO_CHAR_PADDING_IN_PREDICATE))
+ }
+ }
+
+ /**
+ * Set operations of CHAR columns of different lengths have the widest
length, see
+ * [[SetOperationCharPadding]]. This narrows the values of recursive CTEs
and views, whose
+ * columns keep shorter lengths, to those lengths.
+ */
+ private def narrowCharColumns(plan: LogicalPlan): LogicalPlan =
+ narrowViewColumns(narrowRecursiveTerms(plan))
+
+ /**
+ * The columns of a recursive CTE have the CHAR lengths of its anchor. This
narrows values of the
+ * recursive term with longer CHAR lengths, such as those of a set operation
in it, to them, as
+ * [[narrowViewColumns]] does for a view.
+ */
+ private def narrowRecursiveTerms(plan: LogicalPlan): LogicalPlan = {
+ plan.resolveOperatorsUp {
+ case loop: UnionLoop if loop.resolved =>
+ val recursion = loop.recursion
+ val projectList = recursion.output.zip(loop.anchor.output).map { case
(attr, anchorAttr) =>
+ (for {
+ rawType <- CharVarcharUtils.getRawType(attr.metadata,
attr.dataType)
+ anchorType <- CharVarcharUtils.getRawType(anchorAttr.metadata,
anchorAttr.dataType)
+ narrowed <- narrowToLength(attr, rawType, anchorType)
+ } yield Alias(narrowed, attr.name)(
+ qualifier = attr.qualifier,
+ explicitMetadata =
Some(CharVarcharUtils.withRawType(attr.metadata, anchorType))
+ )).getOrElse(attr)
+ }
+ if (projectList == recursion.output) {
+ loop
+ } else {
+ loop.copy(recursion = Project(projectList, recursion))
+ }
+ }
+ }
+
+ // The value narrowed to the CHAR lengths of `toType`, with the data type of
the value. A rebuilt
+ // struct loses the metadata of its fields, which holds their raw types.
+ private def narrowToLength(
+ expr: Expression,
+ fromType: DataType,
+ toType: DataType): Option[Expression] = {
+ CharVarcharUtils.narrowCharToLength(expr, fromType, toType).map { narrowed
=>
+ if (narrowed.dataType == expr.dataType) {
+ narrowed
+ } else {
+ Cast(narrowed, expr.dataType, Some(conf.sessionLocalTimeZone))
+ }
+ }
+ }
+
+ /**
+ * A view stores the CHAR types of its columns, and keeps them for the
values of its query. A view
+ * of a set operation created before the output had the widest length of its
children stores the
+ * length of the first child. This narrows the values back to that length,
as the stored type
+ * says, so that comparisons padded to it still match them. The columns keep
their expression IDs
+ * and metadata.
+ */
+ private def narrowViewColumns(plan: LogicalPlan): LogicalPlan = {
+ def queryAttr(e: Expression): Option[Attribute] = e match {
+ case a: Attribute => Some(a)
+ case c: Cast => queryAttr(c.child)
+ case _ => None
+ }
+ plan.resolveOperatorsUp {
+ case v @ View(_, _, p @ Project(projectList, _), _) =>
+ val newProjectList = projectList.map {
+ case a @ Alias(child, _) =>
+ (for {
+ viewType <- CharVarcharUtils.getRawType(a.metadata, a.dataType)
+ attr <- queryAttr(child)
+ queryType <- CharVarcharUtils.getRawType(attr.metadata,
attr.dataType)
+ narrowed <- narrowToLength(child, queryType, viewType)
Review Comment:
Fixed. The view narrowing now compares the stored type with the raw type of
the query's column after the view's cast (`rawTypeOfCast(queryType,
child.dataType)`), not before it, so an INT field that the view casts to BIGINT
matches its stored BIGINT. The view test has your scenario: a view of a struct
of a union, stored with `k BIGINT` and `c CHAR(3)`, over tables created again
with `k INT`. `WHERE c = 'a'` on its field returns both rows.
--
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]