Skip to content

Commit 205e6d5

Browse files
lianchengyhuai
authored andcommitted
[SPARK-18338][SQL][TEST-MAVEN] Fix test case initialization order under Maven builds
## What changes were proposed in this pull request? Test case initialization order under Maven and SBT are different. Maven always creates instances of all test cases and then run them all together. This fails `ObjectHashAggregateSuite` because the randomized test cases there register a temporary Hive function right before creating a test case, and can be cleared while initializing other successive test cases. In SBT, this is fine since the created test case is executed immediately after creating the temporary function. To fix this issue, we should put initialization/destruction code into `beforeAll()` and `afterAll()`. ## How was this patch tested? Existing tests. Author: Cheng Lian <lian@databricks.com> Closes apache#15802 from liancheng/fix-flaky-object-hash-agg-suite.
1 parent 02c5325 commit 205e6d5

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/ObjectHashAggregateSuite.scala

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,10 @@ import org.scalatest.Matchers._
2525
import org.apache.spark.sql._
2626
import org.apache.spark.sql.catalyst.FunctionIdentifier
2727
import org.apache.spark.sql.catalyst.analysis.UnresolvedFunction
28-
import org.apache.spark.sql.catalyst.expressions.{ExpressionEvalHelper, ExpressionInfo, Literal}
28+
import org.apache.spark.sql.catalyst.expressions.{ExpressionEvalHelper, Literal}
2929
import org.apache.spark.sql.catalyst.expressions.aggregate.ApproximatePercentile
3030
import org.apache.spark.sql.execution.aggregate.{HashAggregateExec, ObjectHashAggregateExec, SortAggregateExec}
3131
import org.apache.spark.sql.functions._
32-
import org.apache.spark.sql.hive.HiveSessionCatalog
3332
import org.apache.spark.sql.hive.test.TestHiveSingleton
3433
import org.apache.spark.sql.internal.SQLConf
3534
import org.apache.spark.sql.test.SQLTestUtils
@@ -43,6 +42,14 @@ class ObjectHashAggregateSuite
4342

4443
import testImplicits._
4544

45+
protected override def beforeAll(): Unit = {
46+
sql(s"CREATE TEMPORARY FUNCTION hive_max AS '${classOf[GenericUDAFMax].getName}'")
47+
}
48+
49+
protected override def afterAll(): Unit = {
50+
sql(s"DROP TEMPORARY FUNCTION IF EXISTS hive_max")
51+
}
52+
4653
test("typed_count without grouping keys") {
4754
val df = Seq((1: Integer, 2), (null, 2), (3: Integer, 4)).toDF("a", "b")
4855

@@ -199,10 +206,7 @@ class ObjectHashAggregateSuite
199206
val typed = percentile_approx($"c0", 0.5)
200207

201208
// A Hive UDAF without partial aggregation support
202-
val withoutPartial = {
203-
registerHiveFunction("hive_max", classOf[GenericUDAFMax])
204-
function("hive_max", $"c1")
205-
}
209+
val withoutPartial = function("hive_max", $"c1")
206210

207211
// A Spark SQL native aggregate function with partial aggregation support that can be executed
208212
// by the Tungsten `HashAggregateExec`
@@ -420,13 +424,6 @@ class ObjectHashAggregateSuite
420424
}
421425
}
422426

423-
private def registerHiveFunction(functionName: String, clazz: Class[_]): Unit = {
424-
val sessionCatalog = spark.sessionState.catalog.asInstanceOf[HiveSessionCatalog]
425-
val builder = sessionCatalog.makeFunctionBuilder(functionName, clazz.getName)
426-
val info = new ExpressionInfo(clazz.getName, functionName)
427-
sessionCatalog.createTempFunction(functionName, info, builder, ignoreIfExists = false)
428-
}
429-
430427
private def function(name: String, args: Column*): Column = {
431428
Column(UnresolvedFunction(FunctionIdentifier(name), args.map(_.expr), isDistinct = false))
432429
}

0 commit comments

Comments
 (0)