dtenedor commented on code in PR #52800: URL: https://github.com/apache/spark/pull/52800#discussion_r2479520065
########## 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()) { Review Comment: Yes, good question. We need separate SQL functions for each supported data type because the sketch buffer formats are different and otherwise the implementation doesn't know which one to use when consuming the sketch buffers later. I started with functions for Bigint, Float, and Double because that is what the Datasketches library provides: https://apache.github.io/datasketches-java/6.1.0/org/apache/datasketches/kll/KllSketch.html Interestingly there is also a `KllItemsSketch` which we could brainstorm about later for e.g. computing quantiles of strings: https://apache.github.io/datasketches-java/6.1.0/org/apache/datasketches/kll/KllItemsSketch.html It seems there is potential value in keeping as much interoperability as possible with the open Datasketches library. Also, looking at prior art, BigQuery supports KLL SQL functions [1]. They also appear to have separate functions for long integers, single-precision floating point values, and double-precision floating point values. [1] https://docs.cloud.google.com/bigquery/docs/reference/standard-sql/kll_functions -- 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]
