cboumalh commented on code in PR #52800:
URL: https://github.com/apache/spark/pull/52800#discussion_r2482434941


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/aggregate/kllAggregates.scala:
##########
@@ -0,0 +1,405 @@
+/*
+ * 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 org.apache.datasketches.kll._
+import org.apache.datasketches.memory.Memory
+
+import org.apache.spark.SparkUnsupportedOperationException
+import org.apache.spark.sql.catalyst.InternalRow
+import org.apache.spark.sql.catalyst.expressions.{ExpectsInputTypes, 
Expression, ExpressionDescription}
+import org.apache.spark.sql.catalyst.trees.UnaryLike
+import org.apache.spark.sql.errors.QueryExecutionErrors
+import org.apache.spark.sql.types._
+
+/** This represents the aggregation buffer for the below aggregate functions. 
*/
+case class KllBufferBigint(sketch: KllLongsSketch = 
KllLongsSketch.newHeapInstance()) {
+  def serialize(): Array[Byte] = sketch.toByteArray
+  def eval(): Array[Byte] = sketch.toByteArray
+}
+case class KllBufferFloat(sketch: KllFloatsSketch = 
KllFloatsSketch.newHeapInstance()) {
+  def serialize(): Array[Byte] = sketch.toByteArray
+  def eval(): Array[Byte] = sketch.toByteArray
+}
+case class KllBufferDouble(sketch: KllDoublesSketch = 
KllDoublesSketch.newHeapInstance()) {
+  def serialize(): Array[Byte] = sketch.toByteArray
+  def eval(): Array[Byte] = sketch.toByteArray
+}
+
+/**
+ * The KllSketchAggBigint function utilizes an Apache DataSketches 
KllLongsSketch instance to
+ * compute quantiles of the values of an input expression (such as an input 
column in a table).
+ * It outputs the binary representation of the KllLongsSketch.
+ *
+ * See [[https://datasketches.apache.org/docs/KLL/KLLSketch.html]] for more 
information.
+ *
+ * @param child
+ *   child expression against which quantile computation will occur
+ * @param mutableAggBufferOffset
+ *   offset for mutable aggregation buffer
+ * @param inputAggBufferOffset
+ *   offset for input aggregation buffer
+ */
+// scalastyle:off line.size.limit
+@ExpressionDescription(
+  usage = """
+    _FUNC_(expr) - Returns the KllLongsSketch compact binary representation.
+  """,
+  examples = """
+    Examples:
+      > SELECT LENGTH(kll_sketch_to_string_bigint(_FUNC_(col))) > 0 FROM 
VALUES (1), (2), (3), (4), (5) tab(col);
+       true
+  """,
+  group = "agg_funcs",
+  since = "4.1.0")
+// scalastyle:on line.size.limit
+case class KllSketchAggBigint(
+    override val child: Expression,
+    override val mutableAggBufferOffset: Int = 0,
+    override val inputAggBufferOffset: Int = 0)
+    extends TypedImperativeAggregate[KllBufferBigint]
+    with UnaryLike[Expression]
+    with ExpectsInputTypes {
+  def this(child: Expression) = this(child, 0, 0)
+
+  override def withNewMutableAggBufferOffset(newMutableAggBufferOffset: Int): 
KllSketchAggBigint =
+    copy(mutableAggBufferOffset = newMutableAggBufferOffset)
+  override def withNewInputAggBufferOffset(newInputAggBufferOffset: Int): 
KllSketchAggBigint =
+    copy(inputAggBufferOffset = newInputAggBufferOffset)
+  override protected def withNewChildInternal(newInput: Expression): 
KllSketchAggBigint =
+    copy(child = newInput)
+
+  override def dataType: DataType = BinaryType
+  override def inputTypes: Seq[AbstractDataType] =
+    Seq(
+      TypeCollection(
+        ByteType,
+        IntegerType,
+        LongType,
+        ShortType))
+  override def nullable: Boolean = false
+  override def prettyName: String = "kll_sketch_agg_bigint"
+  override def createAggregationBuffer(): KllBufferBigint = KllBufferBigint()

Review Comment:
   Good point, yes we should probably add that check there and in the HLL code. 
I can create a PR soon if that works?



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

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