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



##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/basicLogicalOperators.scala
##########
@@ -789,6 +797,40 @@ 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)
+    val lowerIndexInitial = percentileArray.head
+    val lowerBinValueInitial = getRangeValue(0)
+    percentileArray.tail.zipWithIndex
+      .foldLeft[(Double, Long)]((lowerIndexInitial, lowerBinValueInitial)) {
+        case ((lowerIndex, lowerBinValue), (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)
+          // Update the lowerIndex and lowerBinValue with upper ones for the 
next iteration.
+          (upperIndex, upperBinValue)
+      }
+    Histogram(height, binArray)
+  }
+
+  // Utility method to compute histogram
+  private def getRangeValue(index: Int): Long = {
+    assert(index >= 0, "index must be greater than and equal to 0")
+    if (step <= 0) {
+      start + (numElements.toLong - index - 1) * step

Review comment:
       >This tries to step backwards from the end, right? step is negative
   
   Yes, we reverse the range values if the step is negative to compute 
histogram statistics.
   
   >I'm probably missing something, but isn't this more like:
       start + (numElements.toLong - 1) * (-step) + index * step
   
   I am not sure this gives the right range values if the step is negative. 
   For eg: start = 1 , step = -2, and numElements = 4, Range values  = [1, -1, 
-3, -5]
   But for computing histogram we need values like this [-5, -3, -1, 1]. So if 
the index = 0, it should return -5.
   
   With the formula in the PR => 1 + (4 - 0 -1 )* -2= -5
    With the formula you have given above  => 1 + (4 -1) *-(-2) + 0 = 7
   
   So I think the formula in the PR seems correct?
   
   




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