Skip to content

Commit a4df82b

Browse files
committed
resolve comments.
1 parent 86e7f9d commit a4df82b

File tree

2 files changed

+51
-30
lines changed

2 files changed

+51
-30
lines changed

sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/catalog/SessionCatalogSuite.scala

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -919,6 +919,34 @@ class SessionCatalogSuite extends SparkFunSuite {
919919
catalog.lookupFunction(FunctionIdentifier("temp1"), arguments) === Literal(arguments.length))
920920
}
921921

922+
test("isTemporaryFunction") {
923+
val externalCatalog = newBasicCatalog()
924+
val sessionCatalog = new SessionCatalog(externalCatalog)
925+
926+
// Returns false when the function does not exist
927+
assert(!sessionCatalog.isTemporaryFunction(FunctionIdentifier("temp1")))
928+
929+
val tempFunc1 = (e: Seq[Expression]) => e.head
930+
val info1 = new ExpressionInfo("tempFunc1", "temp1")
931+
sessionCatalog.createTempFunction("temp1", info1, tempFunc1, ignoreIfExists = false)
932+
933+
// Returns true when the function is temporary
934+
assert(sessionCatalog.isTemporaryFunction(FunctionIdentifier("temp1")))
935+
936+
// Returns false when the function is permanent
937+
assert(externalCatalog.listFunctions("db2", "*").toSet == Set("func1"))
938+
assert(!sessionCatalog.isTemporaryFunction(FunctionIdentifier("func1", Some("db2"))))
939+
assert(!sessionCatalog.isTemporaryFunction(FunctionIdentifier("db2.func1")))
940+
sessionCatalog.setCurrentDatabase("db2")
941+
assert(!sessionCatalog.isTemporaryFunction(FunctionIdentifier("func1")))
942+
943+
// Returns false when the function is built-in or hive
944+
assert(FunctionRegistry.builtin.functionExists("sum"))
945+
assert(!sessionCatalog.isTemporaryFunction(FunctionIdentifier("sum")))
946+
assert(!sessionCatalog.isTemporaryFunction(FunctionIdentifier("histogram_numeric")))
947+
assert(!sessionCatalog.isTemporaryFunction(FunctionIdentifier("percentile")))
948+
}
949+
922950
test("drop function") {
923951
val externalCatalog = newBasicCatalog()
924952
val sessionCatalog = new SessionCatalog(externalCatalog)

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

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -488,37 +488,30 @@ class SQLViewSuite extends QueryTest with SQLTestUtils with TestHiveSingleton {
488488
}
489489
}
490490

491-
test("create a permanent/temp view using a hive function") {
492-
withView("view1", "tempView1") {
493-
sql(s"CREATE VIEW tempView1 AS SELECT histogram_numeric(id, 5) from jt")
494-
checkAnswer(sql("select count(*) FROM tempView1"), Row(1))
495-
sql(s"CREATE VIEW view1 AS SELECT histogram_numeric(id, 5) from jt")
496-
checkAnswer(sql("select count(*) FROM view1"), Row(1))
497-
}
498-
}
499-
500-
test("create a permanent/temp view using a built-in function") {
501-
withView("view1", "tempView1") {
502-
sql(s"CREATE TEMPORARY VIEW tempView1 AS SELECT abs(id) from jt")
503-
checkAnswer(sql("select count(*) FROM tempView1"), Row(9))
504-
sql(s"CREATE VIEW view1 AS SELECT abs(id) from jt")
505-
checkAnswer(sql("select count(*) FROM view1"), Row(9))
506-
}
507-
}
508-
509-
test("create a permanent/temp view using a permanent function") {
510-
val functionName = "myUpper"
511-
val functionClass =
491+
test("create a permanent/temp view using a hive, built-in, and permanent user function") {
492+
val permanentFuncName = "myUpper"
493+
val permanentFuncClass =
512494
classOf[org.apache.hadoop.hive.ql.udf.generic.GenericUDFUpper].getCanonicalName
513-
withUserDefinedFunction(functionName -> false) {
514-
sql(s"CREATE FUNCTION $functionName AS '$functionClass'")
515-
withView("view1", "tempView1") {
516-
withTable("tab1") {
517-
(1 to 10).map(i => s"$i").toDF("id").write.saveAsTable("tab1")
518-
sql(s"CREATE TEMPORARY VIEW tempView1 AS SELECT $functionName(id) from tab1")
519-
checkAnswer(sql("select count(*) FROM tempView1"), Row(10))
520-
sql(s"CREATE VIEW view1 AS SELECT $functionName(id) from tab1")
521-
checkAnswer(sql("select count(*) FROM view1"), Row(10))
495+
val builtInFuncName = "abs"
496+
val hiveFuncName = "histogram_numeric"
497+
498+
withUserDefinedFunction(permanentFuncName -> false) {
499+
sql(s"CREATE FUNCTION $permanentFuncName AS '$permanentFuncClass'")
500+
withTable("tab1") {
501+
(1 to 10).map(i => (s"$i", i)).toDF("str", "id").write.saveAsTable("tab1")
502+
Seq("VIEW", "TEMPORARY VIEW").foreach { viewMode =>
503+
withView("view1") {
504+
sql(
505+
s"""
506+
|CREATE $viewMode view1
507+
|AS SELECT
508+
|$permanentFuncName(str),
509+
|$builtInFuncName(id),
510+
|$hiveFuncName(id, 5) over()
511+
|FROM tab1
512+
""".stripMargin)
513+
checkAnswer(sql("select count(*) FROM view1"), Row(10))
514+
}
522515
}
523516
}
524517
}

0 commit comments

Comments
 (0)