spark-reviews mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hvanhovell <...@git.apache.org>
Subject [GitHub] spark pull request #15877: [SPARK-18429] [SQL] implement a new Aggregate for...
Date Mon, 14 Nov 2016 13:28:28 GMT
Github user hvanhovell commented on a diff in the pull request:

    https://github.com/apache/spark/pull/15877#discussion_r87799629
  
    --- 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)
    +    }
    +  }
    +
    +  def testLowLevelInterface[T: ClassTag](
    +      dataType: DataType,
    +      sampledItemIndices: Array[Int],
    +      allItems: Array[T],
    +      exactFreq: Map[T, Long]): Any = {
    +    test(s"low level interface, update, merge, eval... - ${dataType.typeName}") {
    +      val inputAggregationBufferOffset = 1
    +      val mutableAggregationBufferOffset = 2
    +
    +      // Phase one, partial mode aggregation
    +      val agg = new CountMinSketchAgg(BoundReference(0, dataType, nullable = true),
    +        Literal(epsOfTotalCount), Literal(confidence), Literal(seed))
    +        .withNewInputAggBufferOffset(inputAggregationBufferOffset)
    +        .withNewMutableAggBufferOffset(mutableAggregationBufferOffset)
    +
    +      val mutableAggBuffer = new GenericInternalRow(
    +        new Array[Any](mutableAggregationBufferOffset + 1))
    +      agg.initialize(mutableAggBuffer)
    +
    +      sampledItemIndices.foreach { i =>
    +        agg.update(mutableAggBuffer, InternalRow(allItems(i)))
    +      }
    +      agg.serializeAggregateBufferInPlace(mutableAggBuffer)
    +
    +      // Serialize the aggregation buffer
    +      val serialized = mutableAggBuffer.getBinary(mutableAggregationBufferOffset)
    +      val inputAggBuffer = new GenericInternalRow(Array[Any](null, serialized))
    +
    +      // Phase 2: final mode aggregation
    +      // Re-initialize the aggregation buffer
    +      agg.initialize(mutableAggBuffer)
    +      agg.merge(mutableAggBuffer, inputAggBuffer)
    +      checkResult(agg.eval(mutableAggBuffer), allItems, exactFreq)
    +    }
    +  }
    +
    +  private def checkResult[T: ClassTag](
    +      result: Any,
    +      data: Array[T],
    +      exactFreq: Map[T, Long]): Unit = {
    +    result match {
    +      case arrayData: ArrayData =>
    --- End diff --
    
    Add `case _ => fail("unexpected return type")` to have a nicer error when something
goed wrong there


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Mime
View raw message