Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
resolve comments.
  • Loading branch information
gatorsmile committed Nov 4, 2016
commit a4df82b255089e9daf4167846186f25879a60da3
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,34 @@ class SessionCatalogSuite extends SparkFunSuite {
catalog.lookupFunction(FunctionIdentifier("temp1"), arguments) === Literal(arguments.length))
}

test("isTemporaryFunction") {
val externalCatalog = newBasicCatalog()
val sessionCatalog = new SessionCatalog(externalCatalog)

// Returns false when the function does not exist
assert(!sessionCatalog.isTemporaryFunction(FunctionIdentifier("temp1")))

val tempFunc1 = (e: Seq[Expression]) => e.head
val info1 = new ExpressionInfo("tempFunc1", "temp1")
sessionCatalog.createTempFunction("temp1", info1, tempFunc1, ignoreIfExists = false)

// Returns true when the function is temporary
assert(sessionCatalog.isTemporaryFunction(FunctionIdentifier("temp1")))

// Returns false when the function is permanent
assert(externalCatalog.listFunctions("db2", "*").toSet == Set("func1"))
assert(!sessionCatalog.isTemporaryFunction(FunctionIdentifier("func1", Some("db2"))))
assert(!sessionCatalog.isTemporaryFunction(FunctionIdentifier("db2.func1")))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's not a permanent function right? it's a function called db2.func1 which doesn't exist

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The related codes in lookupFunction looks confusing.

Here, you are right, functionRegistry does not have such a function. Let me remove it. Thanks!

sessionCatalog.setCurrentDatabase("db2")
assert(!sessionCatalog.isTemporaryFunction(FunctionIdentifier("func1")))

// Returns false when the function is built-in or hive
assert(FunctionRegistry.builtin.functionExists("sum"))
assert(!sessionCatalog.isTemporaryFunction(FunctionIdentifier("sum")))
assert(!sessionCatalog.isTemporaryFunction(FunctionIdentifier("histogram_numeric")))
assert(!sessionCatalog.isTemporaryFunction(FunctionIdentifier("percentile")))
}

test("drop function") {
val externalCatalog = newBasicCatalog()
val sessionCatalog = new SessionCatalog(externalCatalog)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -488,37 +488,30 @@ class SQLViewSuite extends QueryTest with SQLTestUtils with TestHiveSingleton {
}
}

test("create a permanent/temp view using a hive function") {
withView("view1", "tempView1") {
sql(s"CREATE VIEW tempView1 AS SELECT histogram_numeric(id, 5) from jt")
checkAnswer(sql("select count(*) FROM tempView1"), Row(1))
sql(s"CREATE VIEW view1 AS SELECT histogram_numeric(id, 5) from jt")
checkAnswer(sql("select count(*) FROM view1"), Row(1))
}
}

test("create a permanent/temp view using a built-in function") {
withView("view1", "tempView1") {
sql(s"CREATE TEMPORARY VIEW tempView1 AS SELECT abs(id) from jt")
checkAnswer(sql("select count(*) FROM tempView1"), Row(9))
sql(s"CREATE VIEW view1 AS SELECT abs(id) from jt")
checkAnswer(sql("select count(*) FROM view1"), Row(9))
}
}

test("create a permanent/temp view using a permanent function") {
val functionName = "myUpper"
val functionClass =
test("create a permanent/temp view using a hive, built-in, and permanent user function") {
val permanentFuncName = "myUpper"
val permanentFuncClass =
classOf[org.apache.hadoop.hive.ql.udf.generic.GenericUDFUpper].getCanonicalName
withUserDefinedFunction(functionName -> false) {
sql(s"CREATE FUNCTION $functionName AS '$functionClass'")
withView("view1", "tempView1") {
withTable("tab1") {
(1 to 10).map(i => s"$i").toDF("id").write.saveAsTable("tab1")
sql(s"CREATE TEMPORARY VIEW tempView1 AS SELECT $functionName(id) from tab1")
checkAnswer(sql("select count(*) FROM tempView1"), Row(10))
sql(s"CREATE VIEW view1 AS SELECT $functionName(id) from tab1")
checkAnswer(sql("select count(*) FROM view1"), Row(10))
val builtInFuncName = "abs"
val hiveFuncName = "histogram_numeric"

withUserDefinedFunction(permanentFuncName -> false) {
sql(s"CREATE FUNCTION $permanentFuncName AS '$permanentFuncClass'")
withTable("tab1") {
(1 to 10).map(i => (s"$i", i)).toDF("str", "id").write.saveAsTable("tab1")
Seq("VIEW", "TEMPORARY VIEW").foreach { viewMode =>
withView("view1") {
sql(
s"""
|CREATE $viewMode view1
|AS SELECT
|$permanentFuncName(str),
|$builtInFuncName(id),
|$hiveFuncName(id, 5) over()
|FROM tab1
""".stripMargin)
checkAnswer(sql("select count(*) FROM view1"), Row(10))
}
}
}
}
Expand Down