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


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/statsEstimation/JoinEstimation.scala:
##########
@@ -201,7 +201,9 @@ case class JoinEstimation(join: Join) extends Logging {
       if (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)) =>
+          // The intersection has no bounds for string and binary keys, which 
have no
+          // equi-height histograms even if a connector reported one.
+          case (Some(l: Histogram), Some(r: Histogram)) if newMin.isDefined && 
newMax.isDefined =>

Review Comment:
   Done in 1d5d0aa1721. Both estimators now call 
`EstimationUtils.canEstimateWithHistogram(dataType)`, which is false for string 
and binary types. For the inputs that reach this code today it behaves like the 
bounds check, so the test pins that the guard exists rather than how it is 
written.



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/statsEstimation/FilterEstimation.scala:
##########
@@ -366,7 +373,14 @@ case class FilterEstimation(
 
     if (!valueInRange) return Some(0.0)
 
-    val percent = if (colStat.histogram.isDefined) {
+    // Only numeric-like types have equi-height histograms: the histogram path 
converts the
+    // literal to a double. A string or binary column uses its distinct count 
even if a connector
+    // reported a histogram for it.
+    val histogram = attr.dataType match {
+      case _: StringType | BinaryType => None
+      case _ => colStat.histogram
+    }
+    val percent = if (histogram.isDefined) {

Review Comment:
   Updated the description: the failure needed min and max as well, and the 
user-facing section now says that an equality over histogram-only stats moves 
from 1.0 to the distinct count for STRING and BINARY columns too.



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/statsEstimation/FilterEstimation.scala:
##########
@@ -366,7 +373,14 @@ case class FilterEstimation(
 
     if (!valueInRange) return Some(0.0)
 
-    val percent = if (colStat.histogram.isDefined) {
+    // Only numeric-like types have equi-height histograms: the histogram path 
converts the

Review Comment:
   I went with the shared helper. Dropping the histogram in `transformV2Stats` 
would cover DSv2 only, while catalog statistics can carry one as well (set 
through table properties), so checking in the two estimators covers every 
source.



##########
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, 
Literal(n: Int, _)), _)
+          if attributeStats.contains(attr) =>
+        val stat = attributeStats(attr)
+        val padded = if (stat.distinctCount.contains(BigInt(0))) {

Review Comment:
   Done in 1d5d0aa1721. The floor is also skipped when the null count covers 
every row, and `ProjectEstimationSuite` covers a column reported with only a 
null count.



##########
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/statsEstimation/JoinEstimationSuite.scala:
##########
@@ -560,6 +560,20 @@ class JoinEstimationSuite extends StatsEstimationTestBase {
     assert(join.stats == expectedStats)
   }
 
+  test("SPARK-60810: string join keys with reported histograms use the 
distinct count") {

Review Comment:
   Done in 1d5d0aa1721. The join test now covers STRING and BINARY keys and 
CHAR keys joined through the padding `Project`.



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