Github user cloud-fan commented on a diff in the pull request:
https://github.com/apache/spark/pull/19783#discussion_r156282461
--- Diff:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/statsEstimation/FilterEstimation.scala
---
@@ -332,8 +332,44 @@ 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.
+ val datum = EstimationUtils.toDecimal(literal.value,
literal.dataType).toDouble
+ val histogram = colStat.histogram.get
+ val hgmBins = histogram.bins
+
+ // find bins where column's current min and max locate. Note that
a column's [min, max]
+ // range may change due to another condition applied earlier.
+ val min = EstimationUtils.toDecimal(colStat.min.get,
literal.dataType).toDouble
+ val max = EstimationUtils.toDecimal(colStat.max.get,
literal.dataType).toDouble
+ val minBinId = EstimationUtils.findFirstBinForValue(min, hgmBins)
+ val maxBinId = EstimationUtils.findLastBinForValue(max, hgmBins)
+
+ // compute how many bins the column's current valid range [min,
max] occupies.
+ // Note that a column's [min, max] range may vary after we apply
some filter conditions.
+ val validRangeBins = EstimationUtils.getOccupationBins(maxBinId,
minBinId, max,
+ min, histogram)
+
+ val lowerBinId = EstimationUtils.findFirstBinForValue(datum,
hgmBins)
+ val higherBinId = EstimationUtils.findLastBinForValue(datum,
hgmBins)
+ assert(lowerBinId <= higherBinId)
+ val lowerBinNdv = hgmBins(lowerBinId).ndv
+ val higherBinNdv = hgmBins(higherBinId).ndv
+ // assume uniform distribution in each bin
+ val occupiedBins = if (lowerBinId == higherBinId) {
--- End diff --
is this just `EstimationUtils.getOccupationBins(higherBinId, lowerBinId,
datum, datum, histogram)`?
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]