dtenedor commented on code in PR #52883: URL: https://github.com/apache/spark/pull/52883#discussion_r2550929459
########## sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/aggregate/tuplesketchesAggregates.scala: ########## @@ -0,0 +1,843 @@ +/* + * 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.tuple.{Intersection, Sketch, Summary, Union, UpdatableSketch, UpdatableSketchBuilder, UpdatableSummary} + +import org.apache.spark.SparkUnsupportedOperationException +import org.apache.spark.sql.catalyst.InternalRow +import org.apache.spark.sql.catalyst.expressions.{ExpectsInputTypes, Expression, ExpressionDescription, Literal} +import org.apache.spark.sql.catalyst.expressions.aggregate.TypedImperativeAggregate +import org.apache.spark.sql.catalyst.trees.{QuaternaryLike, TernaryLike} +import org.apache.spark.sql.catalyst.util.{ArrayData, CollationFactory, ThetaSketchUtils} +import org.apache.spark.sql.errors.QueryExecutionErrors +import org.apache.spark.sql.internal.types.StringTypeWithCollation +import org.apache.spark.sql.types.{AbstractDataType, ArrayType, BinaryType, DataType, DoubleType, FloatType, IntegerType, LongType, StringType, StructType} +import org.apache.spark.unsafe.types.UTF8String + +sealed trait TupleSketchState { + def serialize(): Array[Byte] + def eval(): Array[Byte] +} +case class UpdatableTupleSketchBuffer[U, S <: UpdatableSummary[U]](sketch: UpdatableSketch[U, S]) + extends TupleSketchState { + override def serialize(): Array[Byte] = sketch.compact.toByteArray + override def eval(): Array[Byte] = sketch.compact.toByteArray +} +case class UnionTupleAggregationBuffer[S <: Summary](union: Union[S]) extends TupleSketchState { + override def serialize(): Array[Byte] = union.getResult.toByteArray + override def eval(): Array[Byte] = union.getResult.toByteArray +} +case class IntersectionTupleAggregationBuffer[S <: Summary](intersection: Intersection[S]) + extends TupleSketchState { + override def serialize(): Array[Byte] = intersection.getResult.toByteArray + override def eval(): Array[Byte] = intersection.getResult.toByteArray +} +case class FinalizedTupleSketch[S <: Summary](sketch: Sketch[S]) extends TupleSketchState { + override def serialize(): Array[Byte] = sketch.toByteArray + override def eval(): Array[Byte] = sketch.toByteArray +} + +/** + * The TupleSketchAgg function utilizes a Datasketches TupleSketch instance to count a + * probabilistic approximation of the number of unique values in a given column with associated + * summary values, and outputs the binary representation of the TupleSketch. + * + * See [[https://datasketches.apache.org/docs/Tuple/TupleSketches.html]] for more information. + * + * @param child + * child expression (struct with key and summary value) against which unique counting will occur + * @param lgNomEntriesExpr + * the log-base-2 of nomEntries decides the number of buckets for the sketch + * @param summaryType + * the type of summary (double, integer, string) Review Comment: @cboumalh Thanks! I thought about this. One question I am wondering is that you mentioned these defaults: ``` lgNomEntries (optional, default = 12): Log-base-2 of nominal entries (4–26) summaryType (optional, default = 'double'): Type of summary ('double', 'integer', 'string') mode (optional, default = 'sum'): Aggregation mode ('sum', 'min', 'max', 'alwaysone') ``` How often do you think users might want to override these defaults? If the answer is "infrequently", then it could make sense to add the suffixes for input arg types (like `tuple_sketch_agg_bigint") but leave these extra optional arguments as optional strings. Also, if we do want to support these as optional arguments with defaults, and use strings for the values, we should use SQL named arguments for them. I looked and Spark does have precedence for using strings to parameterize function calls (e.g. `date_trunc`, `round`, `json_tuple`, `from_csv`). But if we want to have multiple such args, it sounds better to make them named arguments, to make the code more readable and reduce chance of mistakes. Splitting the "expr" into separate "key" and "summary" arguments also would make sense to simplify away from struct usage. Something like: ``` select tuple_sketch_agg_bigint(col, 1.0D); select tuple_sketch_agg_bigint(key => col, summary => 1.0D); select tuple_sketch_agg_bigint( key => col, summary => 1.0D, lgNomEmtries => 12); select tuple_sketch_agg_bigint( key => col, summary => 1.0D, lgNomEmtries => 12, summaryType => 'integer', mode => 'sum'); ``` WDYT? -- 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]
