Github user cloud-fan commented on a diff in the pull request:
https://github.com/apache/spark/pull/19783#discussion_r155564302
--- Diff:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/statsEstimation/FilterEstimation.scala
---
@@ -332,8 +332,41 @@ case class FilterEstimation(plan: Filter) extends
Logging {
colStatsMap.update(attr, newStats)
}
- Some(1.0 / BigDecimal(ndv))
- } else {
+ if (colStat.histogram.isEmpty) {
+ // returns 1/ndv if there is no histogram
+ Some(1.0 / BigDecimal(ndv))
+ } else {
+ // We compute filter selectivity using Histogram information.
+ // Here we traverse histogram bins to locate the range of bins the
literal values falls
+ // into. For skewed distribution, a literal value can occupy
multiple bins.
+ val hgmBins = colStat.histogram.get.bins
+ val datum = EstimationUtils.toDecimal(literal.value,
literal.dataType).toDouble
+ var lowerId, higherId = -1
+ for (i <- hgmBins.indices) {
+ // if datum > upperBound, just traverse to next bin
+ if (datum <= hgmBins(i).hi && lowerId < 0) lowerId = i
+ if (higherId < 0) {
+ if ((datum < hgmBins(i).hi || i == hgmBins.length - 1) ||
+ ((datum == hgmBins(i).hi) && (datum < hgmBins(i + 1).hi))) {
+ higherId = i
+ }
+ }
--- End diff --
how about
```
var lowerId = -1
var highIdFound = false
var i = 0
while (i < hgmBins.length || highIdFound) {
if (datum <= hgmBins(i).hi && lowerId < 0) lowerId = i
if (datum >= hgmBins(i).lo) highIdFound = true
}
val highId = i
```
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]