Skip to content
Snippets Groups Projects
Commit 772e7c18 authored by Yin Huai's avatar Yin Huai Committed by Michael Armbrust
Browse files

[SPARK-9592] [SQL] Fix Last function implemented based on AggregateExpression1.

https://issues.apache.org/jira/browse/SPARK-9592

#8113 has the fundamental fix. But, if we want to minimize the number of changed lines, we can go with this one. Then, in 1.6, we merge #8113.

Author: Yin Huai <yhuai@databricks.com>

Closes #8172 from yhuai/lastFix and squashes the following commits:

b28c42a [Yin Huai] Regression test.
af87086 [Yin Huai] Fix last.
parent b265e282
No related branches found
No related tags found
No related merge requests found
......@@ -650,6 +650,7 @@ case class FirstFunction(expr: Expression, base: AggregateExpression1) extends A
var result: Any = null
override def update(input: InternalRow): Unit = {
// We ignore null values.
if (result == null) {
result = expr.eval(input)
}
......@@ -679,10 +680,14 @@ case class LastFunction(expr: Expression, base: AggregateExpression1) extends Ag
var result: Any = null
override def update(input: InternalRow): Unit = {
result = input
val value = expr.eval(input)
// We ignore null values.
if (value != null) {
result = value
}
}
override def eval(input: InternalRow): Any = {
if (result != null) expr.eval(result.asInstanceOf[InternalRow]) else null
result
}
}
......@@ -480,6 +480,21 @@ abstract class AggregationQuerySuite extends QueryTest with SQLTestUtils with Be
Row(0, null, 1, 1, null, 0) :: Nil)
}
test("test Last implemented based on AggregateExpression1") {
// TODO: Remove this test once we remove AggregateExpression1.
import org.apache.spark.sql.functions._
val df = Seq((1, 1), (2, 2), (3, 3)).toDF("i", "j").repartition(1)
withSQLConf(
SQLConf.SHUFFLE_PARTITIONS.key -> "1",
SQLConf.USE_SQL_AGGREGATE2.key -> "false") {
checkAnswer(
df.groupBy("i").agg(last("j")),
df
)
}
}
test("error handling") {
withSQLConf("spark.sql.useAggregate2" -> "false") {
val errorMessage = intercept[AnalysisException] {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment