shahidki31 commented on a change in pull request #32498:
URL: https://github.com/apache/spark/pull/32498#discussion_r631564612



##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/basicLogicalOperators.scala
##########
@@ -789,6 +797,38 @@ case class Range(
     }
   }
 
+  private def computeHistogramStatistics() = {
+    val numBins = conf.histogramNumBins
+    val height = numElements.toDouble / numBins
+    val percentileArray = (0 to numBins).map(i => i * height).toArray
+
+    val binArray = new Array[HistogramBin](numBins)
+    var lowerIndex = percentileArray.head
+    var lowerBinValue = getRangeValue(0)
+    percentileArray.tail.zipWithIndex.foreach { case (upperIndex, binId) =>
+      // Integer index for upper and lower values in the bin.
+      val upperIndexPos = math.ceil(upperIndex).toInt - 1
+      val lowerIndexPos = math.ceil(lowerIndex).toInt - 1
+
+      val upperBinValue = getRangeValue(math.max(upperIndexPos, 0))
+      val ndv = math.max(upperIndexPos - lowerIndexPos, 1)
+      binArray(binId) = HistogramBin(lowerBinValue, upperBinValue, ndv)
+
+      lowerBinValue = upperBinValue
+      lowerIndex = upperIndex
+    }
+    Histogram(height, binArray)
+  }
+
+  // Utility method to compute histogram
+  private def getRangeValue(index: Int): Long = {

Review comment:
       Done

##########
File path: 
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/statsEstimation/BasicStatsEstimationSuite.scala
##########
@@ -97,12 +121,24 @@ class BasicStatsEstimationSuite extends PlanTest with 
StatsEstimationTestBase {
                 max = Some(-10),
                 nullCount = Some(0),
                 maxLen = Some(LongType.defaultSize),
-                avgLen = Some(LongType.defaultSize))))))
-    checkStats(range, expectedStatsCboOn = rangeStats, expectedStatsCboOff = 
rangeStats)
+                avgLen = Some(LongType.defaultSize),
+                histogram = histogram)))))
+    val extraConfig = Map(SQLConf.HISTOGRAM_ENABLED.key -> "true",
+      SQLConf.HISTOGRAM_NUM_BINS.key -> "3")
+    checkStats(range, expectedStatsCboOn = rangeStats,
+      expectedStatsCboOff = rangeStats, extraConfig)
   }
 
   test("range with negative step where end minus start not divisible by step") 
{
+

Review comment:
       Done




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

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org



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

Reply via email to