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



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

Review comment:
       nit: how about rewriting it like this?
   ```
   
       val lowerIndexInitial = percentileArray.head
       val lowerBinValueInitial = getRangeValue(0)
       val (_, _, binArray) = percentileArray.tail
         .foldLeft((lowerIndexInitial, lowerBinValueInitial, 
Seq.empty[HistogramBin])) {
         case ((lowerIndex, lowerBinValue, binAr), upperIndex) =>
           // 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)
           // Update the lowerIndex and lowerBinValue with upper ones for the 
next iteration.
           (upperIndex, upperBinValue, binAr :+ HistogramBin(lowerBinValue, 
upperBinValue, ndv))
       }
       Histogram(height, binArray.toArray)
   ```




-- 
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:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to