Github user hvanhovell commented on a diff in the pull request: https://github.com/apache/spark/pull/15877#discussion_r87799293 --- Diff: sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/aggregate/CountMinSketchAggSuite.scala --- @@ -0,0 +1,284 @@ +/* + * 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 java.io.ByteArrayInputStream + +import scala.reflect.ClassTag +import scala.util.Random + +import org.apache.spark.SparkFunSuite +import org.apache.spark.sql.catalyst.InternalRow +import org.apache.spark.sql.catalyst.analysis.TypeCheckResult.TypeCheckFailure +import org.apache.spark.sql.catalyst.expressions.{AttributeReference, BoundReference, Cast, GenericInternalRow, Literal} +import org.apache.spark.sql.catalyst.util.ArrayData +import org.apache.spark.sql.types._ +import org.apache.spark.unsafe.types.UTF8String +import org.apache.spark.util.sketch.CountMinSketch + +class CountMinSketchAggSuite extends SparkFunSuite { + private val childExpression = BoundReference(0, IntegerType, nullable = true) + private val epsOfTotalCount = 0.0001 + private val confidence = 0.99 + private val seed = 42 + + test("serialize and de-serialize") { + // Check empty serialize and de-serialize + val agg = new CountMinSketchAgg(childExpression, Literal(epsOfTotalCount), Literal(confidence), + Literal(seed)) + val buffer = CountMinSketch.create(epsOfTotalCount, confidence, seed) + assert(buffer.equals(agg.deserialize(agg.serialize(buffer)))) + + // Check non-empty serialize and de-serialize + val random = new Random(31) + (0 until 10000).map(_ => random.nextInt(100)).foreach { value => + buffer.add(value) + } + assert(buffer.equals(agg.deserialize(agg.serialize(buffer)))) + } + + def testHighLevelInterface[T: ClassTag]( + dataType: DataType, + sampledItemIndices: Array[Int], + allItems: Array[T], + exactFreq: Map[T, Long]): Any = { + test(s"high level interface, update, merge, eval... - $dataType") { + val agg = new CountMinSketchAgg(BoundReference(0, dataType, nullable = true), + Literal(epsOfTotalCount), Literal(confidence), Literal(seed)) + assert(!agg.nullable) + + val group1 = 0 until sampledItemIndices.length / 2 + val group1Buffer = agg.createAggregationBuffer() + group1.foreach { index => + val input = InternalRow(allItems(sampledItemIndices(index))) + agg.update(group1Buffer, input) + } + + val group2 = sampledItemIndices.length / 2 until sampledItemIndices.length + val group2Buffer = agg.createAggregationBuffer() + group2.foreach { index => + val input = InternalRow(allItems(sampledItemIndices(index))) + agg.update(group2Buffer, input) + } + + val mergeBuffer = agg.createAggregationBuffer() + agg.merge(mergeBuffer, group1Buffer) + agg.merge(mergeBuffer, group2Buffer) + checkResult(agg.eval(mergeBuffer), allItems, exactFreq) + } --- End diff -- This might also be a good place to test merging in a different order, and the merging of an empty partition.
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. --- --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org