Github user cloud-fan commented on a diff in the pull request:

    https://github.com/apache/spark/pull/14753#discussion_r76161895
  
    --- Diff: 
sql/core/src/test/scala/org/apache/spark/sql/TypedImperativeAggregateSuite.scala
 ---
    @@ -0,0 +1,261 @@
    +/*
    + * 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
    +
    +import com.google.common.primitives.Ints
    +
    +import org.apache.spark.sql.TypedImperativeAggregateSuite.TypedMax
    +import org.apache.spark.sql.catalyst.InternalRow
    +import org.apache.spark.sql.catalyst.expressions.{BoundReference, 
Expression, GenericMutableRow, SpecificMutableRow, UnsafeRow}
    +import 
org.apache.spark.sql.catalyst.expressions.aggregate.TypedImperativeAggregate
    +import org.apache.spark.sql.execution.aggregate.SortAggregateExec
    +import org.apache.spark.sql.expressions.Window
    +import org.apache.spark.sql.functions._
    +import org.apache.spark.sql.test.SharedSQLContext
    +import org.apache.spark.sql.types.{AbstractDataType, BinaryType, DataType, 
IntegerType, LongType}
    +
    +class TypedImperativeAggregateSuite extends QueryTest with 
SharedSQLContext {
    +
    +  import testImplicits._
    +
    +  private val random = new java.util.Random()
    +
    +  private val data = (0 until 1000).map { _ =>
    +    (random.nextInt(10), random.nextInt(100))
    +  }
    +
    +  test("aggregate with object aggregate buffer") {
    +    val agg = new TypedMax(BoundReference(0, IntegerType, nullable = 
false))
    +
    +    val group1 = (0 until data.length / 2)
    +    val group1Buffer = agg.createAggregationBuffer()
    +    group1.foreach { index =>
    +      val input = InternalRow(data(index)._1, data(index)._2)
    +      agg.update(group1Buffer, input)
    +    }
    +
    +    val group2 = (data.length / 2 until data.length)
    +    val group2Buffer = agg.createAggregationBuffer()
    +    group2.foreach { index =>
    +      val input = InternalRow(data(index)._1, data(index)._2)
    +      agg.update(group2Buffer, input)
    +    }
    +
    +    val mergeBuffer = agg.createAggregationBuffer()
    +    agg.merge(mergeBuffer, group1Buffer)
    +    agg.merge(mergeBuffer, group2Buffer)
    +
    +    assert(mergeBuffer.value == data.map(_._1).max)
    +    assert(agg.eval(mergeBuffer) == data.map(_._1).max)
    +
    +    // Tests low level eval(row: InternalRow) API.
    +    val row = new GenericMutableRow(Array(mergeBuffer): Array[Any])
    +
    +    // Evaluates directly on row consist of aggregation buffer object.
    +    assert(agg.eval(row) == data.map(_._1).max)
    +  }
    +
    +  test("supports SpecificMutableRow as mutable row") {
    +    val aggregationBufferSchema = Seq(IntegerType, LongType, BinaryType, 
IntegerType)
    +    val aggBufferOffset = 2
    +    val buffer = new SpecificMutableRow(aggregationBufferSchema)
    +    val agg = new TypedMax(BoundReference(ordinal = 1, dataType = 
IntegerType, nullable = false))
    +      .withNewMutableAggBufferOffset(aggBufferOffset)
    +
    +    agg.initialize(buffer)
    +    data.foreach { kv =>
    +      val input = InternalRow(kv._1, kv._2)
    +      agg.update(buffer, input)
    +    }
    +    assert(agg.eval(buffer) == data.map(_._2).max)
    +  }
    +
    +  test("dataframe aggregate with object aggregate buffer, should not use 
HashAggregate") {
    +    val df = data.toDF("a", "b")
    +    val max = new TypedMax($"a".expr)
    +
    +    // Always uses SortAggregateExec
    +    val sparkPlan = 
df.select(Column(max.toAggregateExpression())).queryExecution.sparkPlan
    +    assert(sparkPlan.isInstanceOf[SortAggregateExec])
    +  }
    +
    +  test("dataframe aggregate with object aggregate buffer, no group by") {
    +    val df = data.toDF("key", "value").coalesce(2)
    +    val query = df.select(typedMax($"key"), count($"key"), 
typedMax($"value"), count($"value"))
    +    val maxKey = data.map(_._1).max
    +    val countKey = data.size
    +    val maxValue = data.map(_._2).max
    +    val countValue = data.size
    +    val expected = Seq(Row(maxKey, countKey, maxValue, countValue))
    +    checkAnswer(query, expected)
    +  }
    +
    +  test("dataframe aggregate with object aggregate buffer, null expression, 
no group by") {
    +    val df = data.toDF("key", "value").coalesce(2)
    +    val query = df.select(typedMax(lit(null)), count($"key"), 
typedMax(lit(null)),
    +      count($"value"))
    +    val maxNull = Int.MinValue
    +    val countKey = data.size
    +    val countValue = data.size
    +    val expected = Seq(Row(maxNull, countKey, maxNull, countValue))
    --- End diff --
    
    > I think it is not the expected behavior. typedMax(lit(null)) should 
return null.
    
    Well, I think aggregate function is free to decide the semantic of null 
inputs.


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

Reply via email to