This is an automated email from the ASF dual-hosted git repository.

abstractdog pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/hive.git


The following commit(s) were added to refs/heads/master by this push:
     new f0de2032364 HIVE-28312: Save query error message on Driver level 
(#5289) (Laszlo Bodor reviewed by Simhadri Govindappa)
f0de2032364 is described below

commit f0de2032364c46886be6cb14cb48a2acd9fe37d2
Author: Bodor Laszlo <[email protected]>
AuthorDate: Mon Jun 10 11:47:36 2024 +0200

    HIVE-28312: Save query error message on Driver level (#5289) (Laszlo Bodor 
reviewed by Simhadri Govindappa)
---
 ql/src/java/org/apache/hadoop/hive/ql/Driver.java  | 21 +++--
 .../org/apache/hadoop/hive/ql/DriverContext.java   |  9 +++
 .../hadoop/hive/ql/lockmgr/DummyTxnManager.java    |  2 +-
 .../test/org/apache/hadoop/hive/ql/TestDriver.java | 91 ++++++++++++++++++++++
 4 files changed, 115 insertions(+), 8 deletions(-)

diff --git a/ql/src/java/org/apache/hadoop/hive/ql/Driver.java 
b/ql/src/java/org/apache/hadoop/hive/ql/Driver.java
index 0c65ed76c5e..2241baf3f5b 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/Driver.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/Driver.java
@@ -78,8 +78,8 @@ public class Driver implements IDriver {
       "snapshot was outdated when locks were acquired";
 
   private int maxRows = 100;
-
-  private final DriverContext driverContext;
+  @VisibleForTesting
+  final DriverContext driverContext;
   private final DriverState driverState = new DriverState();
   private final DriverTxnHandler driverTxnHandler;
 
@@ -143,7 +143,8 @@ public class Driver implements IDriver {
       return new CommandProcessorResponse(getSchema(), null);
     } catch (CommandProcessorException cpe) {
       processRunException(cpe);
-      throw cpe;
+      saveErrorMessageAndRethrow(cpe);
+      return null;
     }
   }
 
@@ -343,7 +344,7 @@ public class Driver implements IDriver {
       driverTxnHandler.acquireLocksIfNeeded();
     } catch (CommandProcessorException cpe) {
       driverTxnHandler.rollback(cpe);
-      throw cpe;
+      saveErrorMessageAndRethrow(cpe);
     }
   }
 
@@ -354,7 +355,7 @@ public class Driver implements IDriver {
       executor.execute();
     } catch (CommandProcessorException cpe) {
       driverTxnHandler.rollback(cpe);
-      throw cpe;
+      saveErrorMessageAndRethrow(cpe);
     }
   }
 
@@ -423,7 +424,8 @@ public class Driver implements IDriver {
       compileInternal(command, false);
       return new CommandProcessorResponse(getSchema(), null);
     } catch (CommandProcessorException cpe) {
-      throw cpe;
+      saveErrorMessageAndRethrow(cpe);
+      return null;
     } finally {
       if (cleanupTxnList) {
         // Valid txn list might be generated for a query compiled using this 
command, thus we need to reset it
@@ -462,7 +464,7 @@ public class Driver implements IDriver {
         } catch (LockException e) {
           LOG.warn("Exception in releasing locks", e);
         }
-        throw cpe;
+        saveErrorMessageAndRethrow(cpe);
       }
     }
     //Save compile-time PerfLogging for WebUI.
@@ -936,4 +938,9 @@ public class Driver implements IDriver {
   public StatsSource getStatsSource() {
     return driverContext.getStatsSource();
   }
+
+  private void saveErrorMessageAndRethrow(CommandProcessorException cpe) 
throws CommandProcessorException {
+    driverContext.setQueryErrorMessage(cpe.getMessage());
+    throw cpe;
+  }
 }
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/DriverContext.java 
b/ql/src/java/org/apache/hadoop/hive/ql/DriverContext.java
index fd40f12ec18..85a4723a42a 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/DriverContext.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/DriverContext.java
@@ -76,6 +76,7 @@ public class DriverContext {
 
   // HS2 operation handle guid string
   private String operationId;
+  private String queryErrorMessage;
 
   public DriverContext(QueryState queryState, QueryInfo queryInfo, HookRunner 
hookRunner,
       HiveTxnManager initTxnManager) {
@@ -221,4 +222,12 @@ public class DriverContext {
   public void setOperationId(String operationId) {
     this.operationId = operationId;
   }
+
+  public String getQueryErrorMessage() {
+    return queryErrorMessage;
+  }
+
+  public void setQueryErrorMessage(String queryErrorMessage) {
+    this.queryErrorMessage = queryErrorMessage;
+  }
 }
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/lockmgr/DummyTxnManager.java 
b/ql/src/java/org/apache/hadoop/hive/ql/lockmgr/DummyTxnManager.java
index 23064ea1a52..f0f02fca510 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/lockmgr/DummyTxnManager.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/lockmgr/DummyTxnManager.java
@@ -47,7 +47,7 @@ import java.util.*;
  * An implementation of {@link HiveTxnManager} that does not support
  * transactions.  This provides default Hive behavior.
  */
-class DummyTxnManager extends HiveTxnManagerImpl {
+public class DummyTxnManager extends HiveTxnManagerImpl {
   static final private Logger LOG =
       LoggerFactory.getLogger(DummyTxnManager.class.getName());
 
diff --git a/ql/src/test/org/apache/hadoop/hive/ql/TestDriver.java 
b/ql/src/test/org/apache/hadoop/hive/ql/TestDriver.java
new file mode 100644
index 00000000000..c3da12a32ee
--- /dev/null
+++ b/ql/src/test/org/apache/hadoop/hive/ql/TestDriver.java
@@ -0,0 +1,91 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hadoop.hive.ql;
+
+import org.apache.hadoop.hive.conf.HiveConf;
+import org.apache.hadoop.hive.ql.lockmgr.DummyTxnManager;
+import org.apache.hadoop.hive.ql.processors.CommandProcessorException;
+import org.apache.hadoop.hive.ql.session.SessionState;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Driver related unit tests.
+ */
+public class TestDriver {
+
+  private HiveConf conf;
+
+  @Before
+  public void beforeTest() {
+    conf = new HiveConf();
+    conf.setBoolVar(HiveConf.ConfVars.HIVE_SUPPORT_CONCURRENCY, false);
+    conf.setVar(HiveConf.ConfVars.HIVE_AUTHORIZATION_MANAGER,
+        
"org.apache.hadoop.hive.ql.security.authorization.plugin.sqlstd.SQLStdHiveAuthorizerFactory");
+  }
+
+  @Test
+  public void testDriverContextQueryErrorMessageCompileTime() {
+    SessionState.start(conf);
+    Driver driver = getDriver();
+    try {
+      driver.run("wrong sql command");
+      Assert.fail("Should have thrown an exception from compile time");
+    } catch (Exception e) {
+      Assert.assertEquals(CommandProcessorException.class, e.getClass());
+      String message = e.getMessage();
+      // actual assertion: whether the message reached driverContext
+      Assert.assertEquals(message, 
driver.driverContext.getQueryErrorMessage());
+      // sanity check: the message is as expected
+      Assert.assertTrue("Exception message is not as expected, got: " + 
message,
+          e.getMessage().startsWith("FAILED: ParseException line 1:0 cannot 
recognize input near"));
+    } finally {
+      driver.close();
+    }
+  }
+
+  @Test
+  public void testDriverContextQueryErrorMessageRuntime() {
+    conf.setVar(HiveConf.ConfVars.HIVE_EXECUTION_ENGINE, "tez");
+    conf.setBoolVar(HiveConf.ConfVars.HIVE_CLI_TEZ_INITIALIZE_SESSION, false);
+    SessionState.start(conf);
+    Driver driver = getDriver();
+    try {
+      driver.run("create table test_table (id int)");
+      // run a query that most probably goes to Tez execution
+      driver.run("select a.id from test_table a left outer join test_table b 
on a.id = b.id");
+      Assert.fail("Should have thrown an exception from runtime");
+    } catch (Exception e) {
+      Assert.assertEquals(CommandProcessorException.class, e.getClass());
+      String message = e.getMessage();
+      // actual assertion: whether the message reached driverContext
+      Assert.assertEquals(message, 
driver.driverContext.getQueryErrorMessage());
+      // sanity check: the message is as expected
+      Assert.assertTrue("Exception message is not as expected, got: " + 
message,
+          e.getMessage().startsWith("FAILED: Execution Error, return code 1 
from org.apache.hadoop.hive.ql.exec.tez.TezTask"));
+    } finally {
+      driver.close();
+    }
+  }
+
+  private Driver getDriver() {
+    QueryInfo queryInfo = new QueryInfo(null, null, null, null, null);
+    return new Driver(new QueryState.Builder().withHiveConf(conf).build(), 
queryInfo, new DummyTxnManager());
+  }
+}

Reply via email to