Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,15 @@ class HiveContext private[hive](
// Note that HiveUDFs will be overridden by functions registered in this context.
@transient
override protected[sql] lazy val functionRegistry: FunctionRegistry =
new HiveFunctionRegistry(FunctionRegistry.builtin.copy())
new HiveFunctionRegistry(FunctionRegistry.builtin.copy()) {
override def lookupFunction(name: String, children: Seq[Expression]): Expression = {
// Hive Registry need current database to lookup function
// TODO: the current database of executionHive should be consistent with metadataHive
executionHive.withHiveState {
super.lookupFunction(name, children)
Copy link
Contributor

Choose a reason for hiding this comment

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

Just a note. In HiveQLDialect, when we parse a query, we use sqlContext.executionHive.withHiveState. Right now, create function is still broken because the metadata of the function is in metadata hive. However, it is the work of another PR.

}
}
}

// The Hive UDF current_database() is foldable, will be evaluated by optimizer, but the optimizer
// can't access the SessionState of metadataHive.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,19 @@ package org.apache.spark.sql.hive.execution
import java.io.File
import java.util.{Locale, TimeZone}

import org.apache.spark.sql.execution.joins.BroadcastNestedLoopJoin

import scala.util.Try

import org.scalatest.BeforeAndAfter

import org.apache.hadoop.hive.conf.HiveConf.ConfVars
import org.scalatest.BeforeAndAfter

import org.apache.spark.{SparkFiles, SparkException}
import org.apache.spark.sql.{AnalysisException, DataFrame, Row}
import org.apache.spark.sql.catalyst.expressions.Cast
import org.apache.spark.sql.catalyst.plans.logical.Project
import org.apache.spark.sql.execution.joins.BroadcastNestedLoopJoin
import org.apache.spark.sql.hive._
import org.apache.spark.sql.hive.test.TestHiveContext
import org.apache.spark.sql.hive.test.TestHive
import org.apache.spark.sql.hive.test.TestHive._
import org.apache.spark.sql.hive.test.{TestHive, TestHiveContext}
import org.apache.spark.sql.{AnalysisException, DataFrame, Row}
import org.apache.spark.{SparkException, SparkFiles}

case class TestData(a: Int, b: String)

Expand Down Expand Up @@ -1235,6 +1232,26 @@ class HiveQuerySuite extends HiveComparisonTest with BeforeAndAfter {

}

test("lookup hive UDF in another thread") {
val e = intercept[AnalysisException] {
range(1).selectExpr("not_a_udf()")
}
assert(e.getMessage.contains("undefined function not_a_udf"))
var success = false
val t = new Thread("test") {
override def run(): Unit = {
val e = intercept[AnalysisException] {
range(1).selectExpr("not_a_udf()")
Copy link
Contributor

Choose a reason for hiding this comment

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

Without the fix, this line will throw NPE, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes

Copy link
Contributor

Choose a reason for hiding this comment

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

How about we add a comment to let readers know what's the original exception without this fix? We can add this comment when we merge the PR.

}
assert(e.getMessage.contains("undefined function not_a_udf"))
success = true
}
}
t.start()
t.join()
assert(success)
}

createQueryTest("select from thrift based table",
"SELECT * from src_thrift")

Expand Down