diff --git a/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcDriver2.java b/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcDriver2.java index 45aac5fbec..14187cc83b 100644 --- a/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcDriver2.java +++ b/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcDriver2.java @@ -3044,8 +3044,13 @@ private void testInsertOverwrite(HiveStatement stmt) throws SQLException { public void testGetQueryId() throws Exception { HiveStatement stmt = (HiveStatement) con.createStatement(); HiveStatement stmt1 = (HiveStatement) con.createStatement(); - stmt.executeAsync("create database query_id_test with dbproperties ('repl.source.for' = '1, 2, 3')"); + + // Returns null if no query is running. String queryId = stmt.getQueryId(); + assertTrue(queryId == null); + + stmt.executeAsync("create database query_id_test with dbproperties ('repl.source.for' = '1, 2, 3')"); + queryId = stmt.getQueryId(); assertFalse(queryId.isEmpty()); stmt.getUpdateCount(); diff --git a/jdbc/src/java/org/apache/hive/jdbc/HiveStatement.java b/jdbc/src/java/org/apache/hive/jdbc/HiveStatement.java index a9a312c1a2..d9b625466c 100644 --- a/jdbc/src/java/org/apache/hive/jdbc/HiveStatement.java +++ b/jdbc/src/java/org/apache/hive/jdbc/HiveStatement.java @@ -18,8 +18,8 @@ package org.apache.hive.jdbc; -import com.google.common.annotations.VisibleForTesting; import org.apache.commons.codec.binary.Base64; +import org.apache.hadoop.hive.common.classification.InterfaceAudience.LimitedPrivate; import org.apache.hive.jdbc.logs.InPlaceUpdateStream; import org.apache.hive.service.cli.RowSet; import org.apache.hive.service.cli.RowSetFactory; @@ -37,7 +37,6 @@ import org.apache.hive.service.rpc.thrift.TGetOperationStatusResp; import org.apache.hive.service.rpc.thrift.TGetQueryIdReq; import org.apache.hive.service.rpc.thrift.TOperationHandle; -import org.apache.hive.service.rpc.thrift.TOperationState; import org.apache.hive.service.rpc.thrift.TSessionHandle; import org.apache.thrift.TException; import org.slf4j.Logger; @@ -1007,12 +1006,26 @@ public void setInPlaceUpdateStream(InPlaceUpdateStream stream) { this.inPlaceUpdateStream = stream; } - @VisibleForTesting + /** + * Returns the Query ID if it is running. + * This method is a public API for usage outside of Hive, although it is not part of the + * interface java.sql.Statement. + * @return Valid query ID if it is running else returns NULL. + * @throws SQLException If any internal failures. + */ + @LimitedPrivate(value={"Hive and closely related projects."}) public String getQueryId() throws SQLException { + if (stmtHandle == null) { + // If query is not running or already closed. + return null; + } try { return client.GetQueryId(new TGetQueryIdReq(stmtHandle)).getQueryId(); } catch (TException e) { throw new SQLException(e); + } catch (Exception e) { + // If concurrently the query is closed before we fetch queryID. + return null; } } }