@@ -33,6 +33,8 @@ import org.apache.commons.logging.Log
3333import  org .apache .hadoop .hive .conf .HiveConf 
3434import  org .apache .hadoop .hive .conf .HiveConf .ConfVars 
3535import  org .apache .hadoop .hive .metastore .api .FieldSchema 
36+ import  org .apache .hadoop .hive .ql .metadata .Hive 
37+ import  org .apache .hadoop .hive .ql .metadata .HiveException 
3638import  org .apache .hadoop .hive .ql .session .SessionState 
3739import  org .apache .hadoop .hive .shims .ShimLoader 
3840import  org .apache .hadoop .security .UserGroupInformation 
@@ -149,8 +151,9 @@ private[hive] class SparkExecuteStatementOperation(
149151      runInternal()
150152    } else  {
151153      val  parentSessionState  =  SessionState .get()
152-       val  hiveConf  =  new   HiveConf (getParentSession().getHiveConf() )
154+       val  hiveConf  =  getConfigForOperation( )
153155      val  sparkServiceUGI  =  ShimLoader .getHadoopShims.getUGIForConf(hiveConf)
156+       val  sessionHive  =  getCurrentHive()
154157
155158      //  Runnable impl to call runInternal asynchronously,
156159      //  from a different thread
@@ -161,6 +164,7 @@ private[hive] class SparkExecuteStatementOperation(
161164            override  def  run ():  Object  =  {
162165
163166              //  User information is part of the metastore client member in Hive
167+               Hive .set(sessionHive)
164168              SessionState .setCurrentSessionState(parentSessionState)
165169              try  {
166170                runInternal()
@@ -270,4 +274,42 @@ private[hive] class SparkExecuteStatementOperation(
270274      }
271275    }
272276  }
277+ 
278+   /**  
279+    * If there are query specific settings to overlay, then create a copy of config 
280+    * There are two cases we need to clone the session config that's being passed to hive driver 
281+    * 1. Async query - 
282+    *    If the client changes a config setting, that shouldn't reflect in the execution already underway 
283+    * 2. confOverlay - 
284+    *    The query specific settings should only be applied to the query config and not session 
285+    * @return  new configuration 
286+    * @throws  HiveSQLException  
287+    */  
288+   private  def  getConfigForOperation ():  HiveConf  =  {
289+     var  sqlOperationConf  =  getParentSession().getHiveConf()
290+     if  (! getConfOverlay().isEmpty() ||  runInBackground) {
291+       //  clone the partent session config for this query
292+       sqlOperationConf =  new  HiveConf (sqlOperationConf)
293+ 
294+       //  apply overlay query specific settings, if any
295+       getConfOverlay().foreach { case  (k, v) => 
296+         try  {
297+           sqlOperationConf.verifyAndSet(k, v)
298+         } catch  {
299+           case  e : IllegalArgumentException  => 
300+             throw  new  HiveSQLException (" Error applying statement specific settings" 
301+         }
302+       }
303+     }
304+     return  sqlOperationConf
305+   }
306+ 
307+   private  def  getCurrentHive ():  Hive  =  {
308+     try  {
309+       return  Hive .get()
310+     } catch  {
311+       case  e : HiveException  => 
312+         throw  new  HiveSQLException (" Failed to get current Hive object" 
313+     }
314+   }
273315}
0 commit comments