diff --git a/service/src/java/org/apache/hive/service/cli/operation/SQLOperation.java b/service/src/java/org/apache/hive/service/cli/operation/SQLOperation.java index 46946ab..df3f46c 100644 --- a/service/src/java/org/apache/hive/service/cli/operation/SQLOperation.java +++ b/service/src/java/org/apache/hive/service/cli/operation/SQLOperation.java @@ -36,6 +36,8 @@ import org.apache.hadoop.hive.ql.Driver; import org.apache.hadoop.hive.ql.exec.ExplainTask; import org.apache.hadoop.hive.ql.exec.Task; +import org.apache.hadoop.hive.ql.metadata.Hive; +import org.apache.hadoop.hive.ql.metadata.HiveException; import org.apache.hadoop.hive.ql.parse.VariableSubstitution; import org.apache.hadoop.hive.ql.processors.CommandProcessorResponse; import org.apache.hadoop.hive.ql.session.SessionState; @@ -167,11 +169,17 @@ public void run() throws HiveSQLException { if (!shouldRunAsync()) { runInternal(getConfigForOperation()); } else { + final SessionState parentSessionState = SessionState.get(); + final Hive sessionHive = getCurrentHive(); + // Runnable impl to call runInternal asynchronously, + // from a different thread Runnable backgroundOperation = new Runnable() { - SessionState ss = SessionState.get(); @Override public void run() { - SessionState.setCurrentSessionState(ss); + // Storing the current Hive object necessary when doAs is enabled + // User information is part of the metastore client member in Hive + Hive.set(sessionHive); + SessionState.setCurrentSessionState(parentSessionState); try { runInternal(getConfigForOperation()); } catch (HiveSQLException e) { @@ -193,6 +201,14 @@ public void run() { } } + private Hive getCurrentHive() { + try { + return Hive.get(); + } catch (HiveException e) { + throw new RuntimeException("Failed to get current Hive object", e); + } + } + private void cleanup(OperationState state) throws HiveSQLException { setState(state); if (shouldRunAsync()) {