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


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/statsEstimation/JoinEstimation.scala:
##########
@@ -198,10 +198,14 @@ case class JoinEstimation(join: Join) extends Logging {
       val rightKeyStat = rightStats.attributeStats(rightKey)
       val lInterval = ValueInterval(leftKeyStat.min, leftKeyStat.max, 
leftKey.dataType)
       val rInterval = ValueInterval(rightKeyStat.min, rightKeyStat.max, 
rightKey.dataType)
-      if (ValueInterval.isIntersected(lInterval, rInterval)) {
+      // A distinct count of 0 on both sides means neither has a non-null key 
value.
+      val noValues = leftKeyStat.distinctCount.contains(BigInt(0)) &&
+        rightKeyStat.distinctCount.contains(BigInt(0))
+      if (!noValues && ValueInterval.isIntersected(lInterval, rInterval)) {
         val (newMin, newMax) = ValueInterval.intersect(lInterval, rInterval, 
leftKey.dataType)
         val (card, joinStat) = (leftKeyStat.histogram, rightKeyStat.histogram) 
match {
-          case (Some(l: Histogram), Some(r: Histogram)) =>
+          case (Some(l: Histogram), Some(r: Histogram))
+              if canEstimateWithHistogram(leftKey.dataType) =>

Review Comment:
   Fixed in 7b19c873487. `computeByHistogram` now converts the bounds with 
`EstimationUtils.toDouble`, as the filter's histogram estimate does, and the 
comment is gone. The new `JoinEstimationSuite` test joins BOOLEAN keys with 
histograms. It threw `NumberFormatException` before, and its expected count of 
80 differs from the distinct-count estimate of 72, so it also pins that BOOLEAN 
keys take the histogram path.



##########
docs/sql-migration-guide.md:
##########
@@ -24,6 +24,7 @@ license: |
 
 ## Upgrading from Spark SQL 4.4 to 5.0
 
+- Since Spark 5.0, when `spark.sql.cbo.enabled` is `true`, the optimizer 
estimates plans over analyzed `CHAR` columns from their column statistics, as 
it does for `STRING` columns. Since Spark 3.4, read-side `CHAR` padding 
(`spark.sql.readSideCharPadding`, `true` by default) hid these statistics, so, 
for example, a filter on a `CHAR` partition column was estimated to keep every 
row. The estimate assumes evenly distributed values, so for a skewed partition 
it can now be smaller than the data, and the pruned relation can become small 
enough to be broadcast.

Review Comment:
   Added that sentence in 7b19c873487.



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/statsEstimation/EstimationUtils.scala:
##########
@@ -87,6 +88,23 @@ object EstimationUtils {
     expressions.collect {
       case alias @ Alias(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.
+      case alias @ Alias(CharVarcharUtils.ReadSidePadding(attr: Attribute, n), 
_)

Review Comment:
   Added in 7b19c873487: a `TODO(SPARK-60830)` on the padding case in 
`getAliasStats` that names both checks, says they need an extractor of their 
own because `V2ExpressionBuilder` relies on `ReadSidePadding` never matching 
them, and notes the cast to STRING. The `calculateSingleCondition` scaladoc 
points to it.



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