diff --git a/jdbc/src/java/org/apache/hive/jdbc/HiveQueryResultSet.java b/jdbc/src/java/org/apache/hive/jdbc/HiveQueryResultSet.java index 8f67209..9984ea9 100644 --- a/jdbc/src/java/org/apache/hive/jdbc/HiveQueryResultSet.java +++ b/jdbc/src/java/org/apache/hive/jdbc/HiveQueryResultSet.java @@ -42,6 +42,8 @@ import org.apache.hive.service.rpc.thrift.TFetchOrientation; import org.apache.hive.service.rpc.thrift.TFetchResultsReq; import org.apache.hive.service.rpc.thrift.TFetchResultsResp; +import org.apache.hive.service.rpc.thrift.TGetOperationStatusReq; +import org.apache.hive.service.rpc.thrift.TGetOperationStatusResp; import org.apache.hive.service.rpc.thrift.TGetResultSetMetadataReq; import org.apache.hive.service.rpc.thrift.TGetResultSetMetadataResp; import org.apache.hive.service.rpc.thrift.TOperationHandle; @@ -334,6 +336,7 @@ private void closeOperationHandle(TOperationHandle stmtHandle) throws SQLExcepti /** * Moves the cursor down one row from its current position. + * Blocks till results are available. * * @see java.sql.ResultSet#next() * @throws SQLException @@ -347,7 +350,46 @@ public boolean next() throws SQLException { return false; } + // Poll on the operation status, till the operation is complete try { + TGetOperationStatusReq statusReq = new TGetOperationStatusReq(stmtHandle); + boolean operationComplete = false; + TGetOperationStatusResp statusResp; + while (!operationComplete) { + try { + /** + * For an async SQLOperation, GetOperationStatus will use the long polling approach It + * will essentially return after the HIVE_SERVER2_LONG_POLLING_TIMEOUT (a server config) + * expires + */ + statusResp = client.GetOperationStatus(statusReq); + Utils.verifySuccessWithInfo(statusResp.getStatus()); + if (statusResp.isSetOperationState()) { + switch (statusResp.getOperationState()) { + case CLOSED_STATE: + case FINISHED_STATE: + operationComplete = true; + break; + case CANCELED_STATE: + // 01000 -> warning + throw new SQLException("Query was cancelled", "01000"); + case ERROR_STATE: + // Get the error details from the underlying exception + throw new SQLException(statusResp.getErrorMessage(), statusResp.getSqlState(), + statusResp.getErrorCode()); + case UKNOWN_STATE: + throw new SQLException("Unknown query", "HY000"); + case INITIALIZED_STATE: + case PENDING_STATE: + case RUNNING_STATE: + break; + } + } + } catch (Exception e) { + throw new SQLException(e.toString(), "08S01", e); + } + } + TFetchOrientation orientation = TFetchOrientation.FETCH_NEXT; if (fetchFirst) { // If we are asked to start from begining, clear the current fetched resultset @@ -362,7 +404,6 @@ public boolean next() throws SQLException { TFetchResultsResp fetchResp; fetchResp = client.FetchResults(fetchReq); Utils.verifySuccessWithInfo(fetchResp.getStatus()); - TRowSet results = fetchResp.getResults(); fetchedRows = RowSetFactory.create(results, protocol); fetchedRowsItr = fetchedRows.iterator(); @@ -383,7 +424,6 @@ public boolean next() throws SQLException { } catch (SQLException eS) { throw eS; } catch (Exception ex) { - ex.printStackTrace(); throw new SQLException("Error retrieving next row", ex); } // NOTE: fetchOne dosn't throw new SQLException("Method not supported"). diff --git a/jdbc/src/java/org/apache/hive/jdbc/HiveStatement.java b/jdbc/src/java/org/apache/hive/jdbc/HiveStatement.java index b4dba44..086026c 100644 --- a/jdbc/src/java/org/apache/hive/jdbc/HiveStatement.java +++ b/jdbc/src/java/org/apache/hive/jdbc/HiveStatement.java @@ -235,7 +235,6 @@ public void closeOnCompletion() throws SQLException { * * @see java.sql.Statement#execute(java.lang.String) */ - @Override public boolean execute(String sql) throws SQLException { checkConnection("execute"); @@ -266,51 +265,8 @@ public boolean execute(String sql) throws SQLException { throw new SQLException(ex.toString(), "08S01", ex); } - TGetOperationStatusReq statusReq = new TGetOperationStatusReq(stmtHandle); - boolean operationComplete = false; - TGetOperationStatusResp statusResp; - - // Poll on the operation status, till the operation is complete - while (!operationComplete) { - try { - /** - * For an async SQLOperation, GetOperationStatus will use the long polling approach - * It will essentially return after the HIVE_SERVER2_LONG_POLLING_TIMEOUT (a server config) expires - */ - statusResp = client.GetOperationStatus(statusReq); - Utils.verifySuccessWithInfo(statusResp.getStatus()); - if (statusResp.isSetOperationState()) { - switch (statusResp.getOperationState()) { - case CLOSED_STATE: - case FINISHED_STATE: - operationComplete = true; - break; - case CANCELED_STATE: - // 01000 -> warning - throw new SQLException("Query was cancelled", "01000"); - case ERROR_STATE: - // Get the error details from the underlying exception - throw new SQLException(statusResp.getErrorMessage(), - statusResp.getSqlState(), statusResp.getErrorCode()); - case UKNOWN_STATE: - throw new SQLException("Unknown query", "HY000"); - case INITIALIZED_STATE: - case PENDING_STATE: - case RUNNING_STATE: - break; - } - } - } catch (SQLException e) { - isLogBeingGenerated = false; - throw e; - } catch (Exception e) { - isLogBeingGenerated = false; - throw new SQLException(e.toString(), "08S01", e); - } - } isLogBeingGenerated = false; - // The query should be completed by now if (!stmtHandle.isHasResultSet()) { return false; }