You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hive.apache.org by th...@apache.org on 2013/10/26 00:50:46 UTC

svn commit: r1535889 - in /hive/trunk/service/src: java/org/apache/hive/service/cli/operation/ExecuteStatementOperation.java java/org/apache/hive/service/cli/operation/SQLOperation.java test/org/apache/hive/service/cli/CLIServiceTest.java

Author: thejas
Date: Fri Oct 25 22:50:46 2013
New Revision: 1535889

URL: http://svn.apache.org/r1535889
Log:
HIVE-5440: HiveServer2 doesn't apply SQL operation's config property (Prasad Mujumdar via Thejas Nair)

Modified:
    hive/trunk/service/src/java/org/apache/hive/service/cli/operation/ExecuteStatementOperation.java
    hive/trunk/service/src/java/org/apache/hive/service/cli/operation/SQLOperation.java
    hive/trunk/service/src/test/org/apache/hive/service/cli/CLIServiceTest.java

Modified: hive/trunk/service/src/java/org/apache/hive/service/cli/operation/ExecuteStatementOperation.java
URL: http://svn.apache.org/viewvc/hive/trunk/service/src/java/org/apache/hive/service/cli/operation/ExecuteStatementOperation.java?rev=1535889&r1=1535888&r2=1535889&view=diff
==============================================================================
--- hive/trunk/service/src/java/org/apache/hive/service/cli/operation/ExecuteStatementOperation.java (original)
+++ hive/trunk/service/src/java/org/apache/hive/service/cli/operation/ExecuteStatementOperation.java Fri Oct 25 22:50:46 2013
@@ -40,7 +40,7 @@ public abstract class ExecuteStatementOp
   public ExecuteStatementOperation(HiveSession parentSession, String statement, Map<String, String> confOverlay) {
     super(parentSession, OperationType.EXECUTE_STATEMENT);
     this.statement = statement;
-    this.confOverlay = confOverlay;
+    setConfOverlay(confOverlay);
   }
 
   public String getStatement() {
@@ -63,4 +63,14 @@ public abstract class ExecuteStatementOp
     }
     return new HiveCommandOperation(parentSession, statement, processor, confOverlay);
   }
+
+  protected Map<String, String> getConfOverlay() {
+    return confOverlay;
+  }
+
+  protected void setConfOverlay(Map<String, String> confOverlay) {
+    if (confOverlay != null) {
+      this.confOverlay = confOverlay;
+    }
+  }
 }

Modified: hive/trunk/service/src/java/org/apache/hive/service/cli/operation/SQLOperation.java
URL: http://svn.apache.org/viewvc/hive/trunk/service/src/java/org/apache/hive/service/cli/operation/SQLOperation.java?rev=1535889&r1=1535888&r2=1535889&view=diff
==============================================================================
--- hive/trunk/service/src/java/org/apache/hive/service/cli/operation/SQLOperation.java (original)
+++ hive/trunk/service/src/java/org/apache/hive/service/cli/operation/SQLOperation.java Fri Oct 25 22:50:46 2013
@@ -80,7 +80,7 @@ public class SQLOperation extends Execut
   public void prepare() throws HiveSQLException {
   }
 
-  private void runInternal() throws HiveSQLException {
+  private void runInternal(HiveConf sqlOperationConf) throws HiveSQLException {
     setState(OperationState.RUNNING);
     String statement_trimmed = statement.trim();
     String[] tokens = statement_trimmed.split("\\s");
@@ -91,13 +91,13 @@ public class SQLOperation extends Execut
     String SQLState = null;
 
     try {
-      driver = new Driver(getParentSession().getHiveConf(), getParentSession().getUserName());
+      driver = new Driver(sqlOperationConf, getParentSession().getUserName());
       // In Hive server mode, we are not able to retry in the FetchTask
       // case, when calling fetch queries since execute() has returned.
       // For now, we disable the test attempts.
       driver.setTryCount(Integer.MAX_VALUE);
 
-      String subStatement = new VariableSubstitution().substitute(getParentSession().getHiveConf(), statement);
+      String subStatement = new VariableSubstitution().substitute(sqlOperationConf, statement);
 
       response = driver.run(subStatement);
       if (0 != response.getResponseCode()) {
@@ -143,7 +143,7 @@ public class SQLOperation extends Execut
   public void run() throws HiveSQLException {
     setState(OperationState.PENDING);
     if (!shouldRunAsync()) {
-      runInternal();
+      runInternal(getConfigForOperation());
     } else {
       Runnable backgroundOperation = new Runnable() {
         SessionState ss = SessionState.get();
@@ -151,7 +151,7 @@ public class SQLOperation extends Execut
         public void run() {
           SessionState.start(ss);
           try {
-            runInternal();
+            runInternal(getConfigForOperation());
           } catch (HiveSQLException e) {
             LOG.error("Error: ", e);
             // TODO: Return a more detailed error to the client,
@@ -318,4 +318,32 @@ public class SQLOperation extends Execut
     return runAsync;
   }
 
+  /**
+   * If there are query specific settings to overlay, then create a copy of config
+   * There are two cases we need to clone the session config that's being passed to hive driver
+   * 1. Async query -
+   *    If the client changes a config setting, that shouldn't reflect in the execution already underway
+   * 2. confOverlay -
+   *    The query specific settings should only be applied to the query config and not session
+   * @return new configuration
+   * @throws HiveSQLException
+   */
+  private HiveConf getConfigForOperation() throws HiveSQLException {
+    HiveConf sqlOperationConf = getParentSession().getHiveConf();
+    if (!getConfOverlay().isEmpty() || shouldRunAsync()) {
+      // clone the partent session config for this query
+      sqlOperationConf = new HiveConf(sqlOperationConf);
+
+      // apply overlay query specific settings, if any
+      for (Map.Entry<String, String> confEntry : getConfOverlay().entrySet()) {
+        try {
+          sqlOperationConf.verifyAndSet(confEntry.getKey(), confEntry.getValue());
+        } catch (IllegalArgumentException e) {
+          throw new HiveSQLException("Error applying statement specific settings", e);
+        }
+      }
+    }
+    return sqlOperationConf;
+  }
+
 }

Modified: hive/trunk/service/src/test/org/apache/hive/service/cli/CLIServiceTest.java
URL: http://svn.apache.org/viewvc/hive/trunk/service/src/test/org/apache/hive/service/cli/CLIServiceTest.java?rev=1535889&r1=1535888&r2=1535889&view=diff
==============================================================================
--- hive/trunk/service/src/test/org/apache/hive/service/cli/CLIServiceTest.java (original)
+++ hive/trunk/service/src/test/org/apache/hive/service/cli/CLIServiceTest.java Fri Oct 25 22:50:46 2013
@@ -20,10 +20,13 @@ package org.apache.hive.service.cli;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.fail;
 
 import java.util.Collections;
 import java.util.HashMap;
+import java.util.Map;
 
+import org.apache.hadoop.hive.conf.HiveConf;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
@@ -224,4 +227,68 @@ public abstract class CLIServiceTest {
     System.out.println(ophandle + " after cancelling, state= " + state);
     assertEquals("Query should be cancelled", OperationState.CANCELED, state);
   }
+
+  /**
+   * Test per statement configuration overlay.
+   * Create a table using hiveconf: var substitution, with the conf var passed
+   * via confOverlay.Verify the confOverlay works for the query and does set the
+   * value in the session configuration
+   * @throws Exception
+   */
+  @Test
+  public void testConfOverlay() throws Exception {
+    SessionHandle sessionHandle = client.openSession("tom", "password", new HashMap<String, String>());
+    assertNotNull(sessionHandle);
+    String tabName = "TEST_CONF_EXEC";
+    String tabNameVar = "tabNameVar";
+
+    String setLockMgr = "SET " + HiveConf.ConfVars.HIVE_SUPPORT_CONCURRENCY.varname
+        + " = false";
+    OperationHandle opHandle = client.executeStatement(sessionHandle, setLockMgr, null);
+    client.closeOperation(opHandle);
+
+    String dropTable = "DROP TABLE IF EXISTS " + tabName;
+    opHandle = client.executeStatement(sessionHandle, dropTable, null);
+    client.closeOperation(opHandle);
+
+    // set a pass a property to operation and check if its set the query config
+    Map <String, String> confOverlay = new HashMap<String, String>();
+    confOverlay.put(tabNameVar, tabName);
+
+    // execute statement with the conf overlay
+    String createTab = "CREATE TABLE ${hiveconf:" + tabNameVar + "} (id int)";
+    opHandle = client.executeStatement(sessionHandle, createTab, confOverlay);
+    assertNotNull(opHandle);
+    // query should pass and create the table
+    assertEquals("Query should be finished",
+        OperationState.FINISHED, client.getOperationStatus(opHandle));
+    client.closeOperation(opHandle);
+
+    // select from  the new table should pass
+    String selectTab = "SELECT * FROM " + tabName;
+    opHandle = client.executeStatement(sessionHandle, selectTab, null);
+    assertNotNull(opHandle);
+    // query should pass and create the table
+    assertEquals("Query should be finished",
+        OperationState.FINISHED, client.getOperationStatus(opHandle));
+    client.closeOperation(opHandle);
+
+    // the settings in confoverly should not be part of session config
+    // another query referring that property with the conf overlay should fail
+    selectTab = "SELECT * FROM ${hiveconf:" + tabNameVar + "}";
+    try {
+      opHandle = client.executeStatement(sessionHandle, selectTab, null);
+      fail("Query should fail");
+    } catch (HiveSQLException e) {
+      // Expected exception
+    }
+
+    // cleanup
+    dropTable = "DROP TABLE IF EXISTS " + tabName;
+    opHandle = client.executeStatement(sessionHandle, dropTable, null);
+    client.closeOperation(opHandle);
+
+
+    client.closeSession(sessionHandle);
+  }
 }