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



##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/basicLogicalOperators.scala
##########
@@ -789,6 +793,36 @@ case class Range(
     }
   }
 
+  private def getHistogramStatistics = {
+    val numBins = conf.histogramNumBins
+    val height = numElements.toDouble / numBins
+    val percentileArray = (0 to numBins).map(i => i * height).toArray
+
+    var binId = 0

Review comment:
       This is not performance-intensive code, so could you use 
`zipWithIndex+map` instead of `while`?

##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/basicLogicalOperators.scala
##########
@@ -789,6 +793,36 @@ case class Range(
     }
   }
 
+  private def getHistogramStatistics = {
+    val numBins = conf.histogramNumBins
+    val height = numElements.toDouble / numBins
+    val percentileArray = (0 to numBins).map(i => i * height).toArray
+
+    var binId = 0
+    val binArray = new Array[HistogramBin](numBins)
+    var lowerIndex = percentileArray(0)
+    var lowerBinValue = getRangeValue(0)
+    while (binId < numBins) {
+      val upperIndex = percentileArray(binId + 1)
+      val upperBinValue = getRangeValue(math.max(math.ceil(upperIndex).toInt - 
1, 0))
+      val ndv = math.max(math.ceil(upperIndex).toInt - 
math.ceil(lowerIndex).toInt, 1)

Review comment:
       Please do not repeat `math.ceil(upperIndex)` here.

##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/basicLogicalOperators.scala
##########
@@ -789,6 +793,36 @@ case class Range(
     }
   }
 
+  private def getHistogramStatistics = {

Review comment:
       `getHistogramStatistics` -> `computeHistogramStatistics()`

##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/basicLogicalOperators.scala
##########
@@ -774,13 +774,17 @@ case class Range(
       } else {
         (start + (numElements - 1) * step, start)
       }
+
+      val histogram = getHistogramStatistics

Review comment:
       I feel its better to wrap a return value of `computeHistogramStatistics 
` with `Some` in the caller side because the method always return a value.
   
https://github.com/apache/spark/pull/32498/files#diff-0ac2c89f8cc0d00e8fe7717b01697f36f20fe8abf2def09bcfde0ad84b30e467R814

##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/basicLogicalOperators.scala
##########
@@ -774,13 +774,17 @@ case class Range(
       } else {
         (start + (numElements - 1) * step, start)
       }
+
+      val histogram = getHistogramStatistics

Review comment:
       Let's check `conf.histogramEnabled` here;
   ```
   val histogram = if (conf.histogramEnabled) {
     Some(computeHistogramStatistics())
   } else {
     None
   }
   ```




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