LuciferYang commented on code in PR #60616:
URL: https://github.com/apache/spark/pull/60616#discussion_r4638550645


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/statsEstimation/EstimationUtils.scala:
##########
@@ -86,31 +86,21 @@ object EstimationUtils {
       attributeStats: AttributeMap[ColumnStat],
       rowCount: BigInt): Seq[(Attribute, ColumnStat)] = {
     expressions.collect {
-      case alias @ Alias(attr: Attribute, _) if attributeStats.contains(attr) 
=>
+      // A cast to a STRING of the same collation keeps the value and the 
column's statistics,
+      // e.g. the cast that a union with a STRING adds to a first-class 
VARCHAR column.
+      case alias @ Alias(WithoutCastToPlainString(attr: Attribute), _)
+          if attributeStats.contains(attr) =>
         alias.toAttribute -> attributeStats(attr)
-      // Read-side CHAR padding wraps each CHAR column of a scan. With padding 
on, ANALYZE TABLE
-      // reads the table through it as well, so the statistics already 
describe padded values.
-      // Statistics collected otherwise (with padding off, or reported by a 
connector) can
-      // describe shorter values, and every non-null padded value has at least 
N characters.
-      // A distinct count of 0 means no non-null values, so there is nothing 
to pad (ANALYZE
-      // then stores the type's default size as the lengths). The distinct 
count is left as is.
-      // It is too high only when stored values differ just in trailing 
spaces, which the
-      // padding makes equal.
-      // TODO(SPARK-60830): under 
spark.sql.charVarchar.standardSemantics.enabled the scan wraps
-      // CHAR and VARCHAR columns in charTypeReadSideCheck and 
varcharTypeReadSideCheck instead,
-      // so they still lose their statistics here and in 
FilterEstimation.calculateSingleCondition.
-      // They need an extractor of their own, because V2ExpressionBuilder 
relies on
-      // ReadSidePadding never matching the checks. Under first-class CHAR 
types the filter unwrap
-      // also has to see through the cast to STRING.
-      case alias @ Alias(CharVarcharUtils.ReadSidePadding(attr: Attribute, n), 
_)
+      // A scan wraps each CHAR column in read-side padding, or under standard 
CHAR/VARCHAR
+      // semantics each CHAR and VARCHAR column in a read-side check. ANALYZE 
TABLE reads the table
+      // through them as well, so the statistics already describe the values 
they return.
+      // Statistics collected otherwise (with padding off, or reported by a 
connector) can describe
+      // shorter values. The VARCHAR check never makes a value longer.

Review Comment:
   Done. The comment now says that the padding and the checks can make stored 
values equal that differ only in trailing spaces, and that the distinct count 
is kept as is. The `unwrapCharVarcharRead` doc says it uses the same statistics.
   



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/DistinctKeyVisitor.scala:
##########
@@ -20,12 +20,21 @@ package org.apache.spark.sql.catalyst.plans.logical
 import org.apache.spark.sql.catalyst.expressions.{Alias, Expression, 
ExpressionSet, NamedExpression}
 import org.apache.spark.sql.catalyst.planning.ExtractEquiJoinKeys
 import org.apache.spark.sql.catalyst.plans.{Inner, LeftOuter, LeftSemiOrAnti, 
RightOuter}
+import org.apache.spark.sql.catalyst.util.CharVarcharUtils
 
 /**
  * A visitor pattern for traversing a [[LogicalPlan]] tree and propagate the 
distinct attributes.
  */
 object DistinctKeyVisitor extends LogicalPlanVisitor[Set[ExpressionSet]] {
 
+  /**
+   * The join keys that a side's distinct keys are compared with to decide 
whether the side is
+   * unique on them. A key cast to a STRING of its own collation, as a 
comparison of a CHAR or
+   * VARCHAR column with a STRING adds, is unique where the column is.
+   */
+  def joinKeySet(keys: Seq[Expression]): ExpressionSet =

Review Comment:
   Fixed here. `projectDistinctKeys` now maps the keys a second time, with the 
aliases also keyed on `CharVarcharUtils.stripCastToPlainString(alias.child)`, 
and keeps both results, so an alias of the column itself still gives its own 
key. Your example now removes the join. `DistinctKeyVisitorSuite`, 
`OuterJoinEliminationSuite` and the end-to-end test cover it, and the migration 
guide entry now says the cast can be a join key or a projected column.
   



-- 
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]

Reply via email to