cloud-fan commented on a change in pull request #27025: [SPARK-26560][SQL] 
Spark should be able to run Hive UDF using jar regardless of current thread 
context classloader
URL: https://github.com/apache/spark/pull/27025#discussion_r399923532
 
 

 ##########
 File path: 
sql/hive/src/main/scala/org/apache/spark/sql/hive/HiveSessionCatalog.scala
 ##########
 @@ -66,49 +66,52 @@ private[sql] class HiveSessionCatalog(
       name: String,
       clazz: Class[_],
       input: Seq[Expression]): Expression = {
-
-    Try(super.makeFunctionExpression(name, clazz, input)).getOrElse {
-      var udfExpr: Option[Expression] = None
-      try {
-        // When we instantiate hive UDF wrapper class, we may throw exception 
if the input
-        // expressions don't satisfy the hive UDF, such as type mismatch, 
input number
-        // mismatch, etc. Here we catch the exception and throw 
AnalysisException instead.
-        if (classOf[UDF].isAssignableFrom(clazz)) {
-          udfExpr = Some(HiveSimpleUDF(name, new 
HiveFunctionWrapper(clazz.getName), input))
-          udfExpr.get.dataType // Force it to check input data types.
-        } else if (classOf[GenericUDF].isAssignableFrom(clazz)) {
-          udfExpr = Some(HiveGenericUDF(name, new 
HiveFunctionWrapper(clazz.getName), input))
-          udfExpr.get.dataType // Force it to check input data types.
-        } else if 
(classOf[AbstractGenericUDAFResolver].isAssignableFrom(clazz)) {
-          udfExpr = Some(HiveUDAFFunction(name, new 
HiveFunctionWrapper(clazz.getName), input))
-          udfExpr.get.dataType // Force it to check input data types.
-        } else if (classOf[UDAF].isAssignableFrom(clazz)) {
-          udfExpr = Some(HiveUDAFFunction(
-            name,
-            new HiveFunctionWrapper(clazz.getName),
-            input,
-            isUDAFBridgeRequired = true))
-          udfExpr.get.dataType // Force it to check input data types.
-        } else if (classOf[GenericUDTF].isAssignableFrom(clazz)) {
-          udfExpr = Some(HiveGenericUDTF(name, new 
HiveFunctionWrapper(clazz.getName), input))
-          udfExpr.get.asInstanceOf[HiveGenericUDTF].elementSchema // Force it 
to check data types.
+    // Current thread context classloader may not be the one loaded the class. 
Need to switch
+    // context classloader to initialize instance properly.
+    Utils.withContextClassLoader(clazz.getClassLoader) {
+      Try(super.makeFunctionExpression(name, clazz, input)).getOrElse {
+        var udfExpr: Option[Expression] = None
+        try {
+          // When we instantiate hive UDF wrapper class, we may throw 
exception if the input
+          // expressions don't satisfy the hive UDF, such as type mismatch, 
input number
+          // mismatch, etc. Here we catch the exception and throw 
AnalysisException instead.
+          if (classOf[UDF].isAssignableFrom(clazz)) {
+            udfExpr = Some(HiveSimpleUDF(name, new 
HiveFunctionWrapper(clazz.getName), input))
+            udfExpr.get.dataType // Force it to check input data types.
 
 Review comment:
   OK let me put my findings: If you look at 
`HiveFunctionWrapper.createFunction`, it says we don't cache the instance for 
Simple UDF
   ```
       def createFunction[UDFType <: AnyRef](): UDFType = {
         if (instance != null) {
           instance.asInstanceOf[UDFType]
         } else {
           val func = Utils.getContextOrSparkClassLoader
             .loadClass(functionClassName).newInstance.asInstanceOf[UDFType]
           if (!func.isInstanceOf[UDF]) {
             // We cache the function if it's no the Simple UDF,
             // as we always have to create new instance for Simple UDF
             instance = func
           }
           func
         }
       }
   ```
   I don't know the history but I assume "we always have to create new instance 
for Simple UDF" is correct. I think what we can do is to cache the loaded 
`Class` as well as the instance.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to