RyanBerti commented on code in PR #40615:
URL: https://github.com/apache/spark/pull/40615#discussion_r1156738385


##########
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/aggregate/DatasketchesHllSketchSuite.scala:
##########
@@ -0,0 +1,83 @@
+/*
+ * 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.immutable.NumericRange
+import scala.util.Random
+
+import org.apache.datasketches.hll.HllSketch
+
+import org.apache.spark.SparkFunSuite
+import org.apache.spark.sql.catalyst.InternalRow
+import org.apache.spark.sql.catalyst.expressions.BoundReference
+import org.apache.spark.sql.types.{DataType, IntegerType, LongType, StringType}
+import org.apache.spark.unsafe.types.UTF8String
+
+
+class DatasketchesHllSketchSuite extends SparkFunSuite {
+
+  def simulateUpdateMerge[T](dataType: DataType, input: Seq[Any], numSketches: 
Integer = 5):
+    (Long, NumericRange[Long]) = {
+
+    // create a map of agg function instances
+    val aggFunctionMap = Seq.tabulate(numSketches)(index => {
+      val sketch = HllSketchEstimate(BoundReference(0, dataType, nullable = 
true))
+      index -> (sketch, sketch.createAggregationBuffer())
+    }).toMap
+
+    // randomly update agg function instances
+    input.map(value => {
+      val (aggFunction, aggBuffer) = 
aggFunctionMap(Random.nextInt(numSketches))
+      aggFunction.update(aggBuffer, InternalRow(value))
+    })
+
+    def serializeDeserialize(tuple: (HllSketchEstimate, HllSketch)):
+      (HllSketchEstimate, HllSketch) = {
+      val (agg, buf) = tuple
+      val serialized = agg.serialize(buf)
+      (agg, agg.deserialize(serialized))
+    }
+
+    // simulate serialization -> deserialization -> merge
+    val mapValues = aggFunctionMap.values
+    val (mergedAgg, mergedBuf) = 
mapValues.tail.foldLeft(mapValues.head)((prev, cur) => {
+      val (prevAgg, prevBuf) = serializeDeserialize(prev)
+      val (_, curBuf) = serializeDeserialize(cur)
+
+      (prevAgg, prevAgg.merge(prevBuf, curBuf))
+    })
+
+    (mergedAgg.eval(mergedBuf).asInstanceOf[Long],
+    mergedBuf.getLowerBound(3).toLong to mergedBuf.getUpperBound(3).toLong)

Review Comment:
   I'm allowing tests to pass if the estimate is correct to within 3 std 
deviations. This mimics the HyperLogLogPlusPlus test harness, though I wish 
there was a better mechanism to ensure error is less than what's expected based 
on the HllSketch lgConfigK value. I've read through 
https://datasketches.apache.org/docs/HLL/HLL.html a few times and it's not 
clear to me how to derive expected error from lgConfigK. I'll continue to look 
around in the datasketched library.



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