Github user cloud-fan commented on a diff in the pull request:

    https://github.com/apache/spark/pull/17148#discussion_r104493019
  
    --- Diff: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/statsEstimation/FilterEstimation.scala
 ---
    @@ -414,53 +436,63 @@ case class FilterEstimation(plan: Filter, 
catalystConf: CatalystConf) extends Lo
           literal: Literal,
           update: Boolean): Option[Double] = {
     
    -    var percent = 1.0
         val colStat = colStatsMap(attr)
    -    val statsRange =
    -      Range(colStat.min, colStat.max, 
attr.dataType).asInstanceOf[NumericRange]
    +    val statsRange = Range(colStat.min, colStat.max, 
attr.dataType).asInstanceOf[NumericRange]
    +    val max = BigDecimal(statsRange.max)
    +    val min = BigDecimal(statsRange.min)
    +    val ndv = BigDecimal(colStat.distinctCount)
     
         // determine the overlapping degree between predicate range and 
column's range
    -    val literalValueBD = BigDecimal(literal.value.toString)
    +    val numericLiteral = if (literal.dataType == BooleanType) {
    +      if (literal.value.asInstanceOf[Boolean]) BigDecimal(1) else 
BigDecimal(0)
    +    } else {
    +      BigDecimal(literal.value.toString)
    +    }
         val (noOverlap: Boolean, completeOverlap: Boolean) = op match {
           case _: LessThan =>
    -        (literalValueBD <= statsRange.min, literalValueBD > statsRange.max)
    +        (numericLiteral <= min, numericLiteral > max)
           case _: LessThanOrEqual =>
    -        (literalValueBD < statsRange.min, literalValueBD >= statsRange.max)
    +        (numericLiteral < min, numericLiteral >= max)
           case _: GreaterThan =>
    -        (literalValueBD >= statsRange.max, literalValueBD < statsRange.min)
    +        (numericLiteral >= max, numericLiteral < min)
           case _: GreaterThanOrEqual =>
    -        (literalValueBD > statsRange.max, literalValueBD <= statsRange.min)
    +        (numericLiteral > max, numericLiteral <= min)
         }
     
    +    var percent = BigDecimal(1.0)
         if (noOverlap) {
           percent = 0.0
         } else if (completeOverlap) {
           percent = 1.0
         } else {
    -      // this is partial overlap case
    -      val literalDouble = literalValueBD.toDouble
    -      val maxDouble = BigDecimal(statsRange.max).toDouble
    -      val minDouble = BigDecimal(statsRange.min).toDouble
    -
    +      // This is the partial overlap case:
           // Without advanced statistics like histogram, we assume uniform 
data distribution.
           // We just prorate the adjusted range over the initial range to 
compute filter selectivity.
    -      // For ease of computation, we convert all relevant numeric values 
to Double.
    +      assert(max > min)
           percent = op match {
             case _: LessThan =>
    -          (literalDouble - minDouble) / (maxDouble - minDouble)
    +          if (numericLiteral == max) {
    --- End diff --
    
    can you add some comments to explain this special case?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to