You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by GitBox <gi...@apache.org> on 2021/10/26 09:34:14 UTC

[GitHub] [spark] AngersZhuuuu commented on a change in pull request #34380: [SPARK-37082][SQL] Implements histogram_numeric aggregation function which supports partial aggregation.

AngersZhuuuu commented on a change in pull request #34380:
URL: https://github.com/apache/spark/pull/34380#discussion_r736340592



##########
File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/aggregate/HistogramNumeric.scala
##########
@@ -0,0 +1,167 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.spark.sql.catalyst.expressions.aggregate
+
+import scala.collection.JavaConverters._
+
+import org.apache.spark.sql.catalyst.InternalRow
+import org.apache.spark.sql.catalyst.analysis.TypeCheckResult
+import org.apache.spark.sql.catalyst.analysis.TypeCheckResult.{TypeCheckFailure, TypeCheckSuccess}
+import org.apache.spark.sql.catalyst.expressions.{Expression, ExpressionDescription, ImplicitCastInputTypes}
+import org.apache.spark.sql.catalyst.trees.BinaryLike
+import org.apache.spark.sql.catalyst.util.{DistributedHistogramSerializer, DistributeHistogram, GenericArrayData}
+import org.apache.spark.sql.errors.QueryExecutionErrors
+import org.apache.spark.sql.types.{AbstractDataType, ArrayType, DataType, DateType, DoubleType, IntegerType, NumericType, StructField, StructType, TimestampNTZType, TimestampType, TypeCollection}
+
+/**
+ * Computes an approximate histogram of a numerical column using a user-specified number of bins.
+ *
+ * The output is an array of (x,y) pairs as struct objects that represents the histogram's
+ * bin centers and heights.
+ */
+@ExpressionDescription(
+  usage = """
+    _FUNC_(expr, nb) - Computes a histogram on numeric 'expr' using nb bins.
+      The return value is an array of (x,y) pairs representing the centers of the
+      histogram's bins. As the value of 'nb' is increased, the histogram approximation
+      gets finer-grained, but may yield artifacts around outliers. In practice, 20-40
+      histogram bins appear to work well, with more bins being required for skewed or
+      smaller datasets. Note that this function creates a histogram with non-uniform
+      bin widths. It offers no guarantees in terms of the mean-squared-error of the
+      histogram, but in practice is comparable to the histograms produced by the R/S-Plus
+      statistical computing packages.
+    """,
+  examples = """
+    Examples:
+      > SELECT _FUNC_(col, 5) FROM VALUES (0), (1), (2), (10) AS tab(col);
+       [{"x":0.0,"y":1.0},{"x":1.0,"y":1.0},{"x":2.0,"y":1.0},{"x":10.0,"y":1.0}]
+  """,
+  group = "agg_funcs",
+  since = "3.3.0")
+case class HistogramNumeric(
+    child: Expression,
+    nBins: Expression,
+    override val mutableAggBufferOffset: Int,
+    override val inputAggBufferOffset: Int)
+  extends TypedImperativeAggregate[DistributeHistogram] with ImplicitCastInputTypes
+  with BinaryLike[Expression] {
+
+  def this(child: Expression, nBins: Expression) = {
+    this(child, nBins, 0, 0)
+  }
+
+  private lazy val nb = nBins.eval() match {
+    case null => null
+    case n: Int => n
+  }
+
+  override def inputTypes: Seq[AbstractDataType] = {
+    // Support NumericType, DateType, TimestampType and TimestampNTZType since their internal types
+    // are all numeric, and can be easily cast to double for processing.

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.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

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



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