diff --git a/service/src/java/org/apache/hive/service/cli/CLIService.java b/service/src/java/org/apache/hive/service/cli/CLIService.java index 1a7f338..d771684 100644 --- a/service/src/java/org/apache/hive/service/cli/CLIService.java +++ b/service/src/java/org/apache/hive/service/cli/CLIService.java @@ -35,6 +35,7 @@ import org.apache.hive.service.CompositeService; import org.apache.hive.service.ServiceException; import org.apache.hive.service.auth.HiveAuthFactory; +import org.apache.hive.service.cli.operation.OperationManager; import org.apache.hive.service.cli.session.SessionManager; /** @@ -256,11 +257,13 @@ public OperationHandle getFunctions(SessionHandle sessionHandle, * @see org.apache.hive.service.cli.ICLIService#getOperationStatus(org.apache.hive.service.cli.OperationHandle) */ @Override - public OperationState getOperationStatus(OperationHandle opHandle) + public OperationStatus getOperationStatus(OperationHandle opHandle) throws HiveSQLException { - OperationState opState = sessionManager.getOperationManager().getOperationState(opHandle); + OperationManager opManager = sessionManager.getOperationManager(); + OperationState opState = opManager.getOperationState(opHandle); + Exception runException = opManager.getOperationRunException(opHandle); LOG.info(opHandle + ": getOperationStatus()"); - return opState; + return new OperationStatus(opState, runException); } /* (non-Javadoc) diff --git a/service/src/java/org/apache/hive/service/cli/CLIServiceClient.java b/service/src/java/org/apache/hive/service/cli/CLIServiceClient.java index 14ef54f..b9d1489 100644 --- a/service/src/java/org/apache/hive/service/cli/CLIServiceClient.java +++ b/service/src/java/org/apache/hive/service/cli/CLIServiceClient.java @@ -19,8 +19,6 @@ package org.apache.hive.service.cli; import java.util.Collections; -import java.util.List; -import java.util.Map; /** @@ -29,127 +27,12 @@ */ public abstract class CLIServiceClient implements ICLIService { - /* (non-Javadoc) - * @see org.apache.hive.service.cli.ICLIService#openSession(java.lang.String, java.lang.String, java.util.Map) - */ - @Override - public abstract SessionHandle openSession(String username, String password, - Map configuration) throws HiveSQLException; - - public SessionHandle openSession(String username, String password) throws HiveSQLException { return openSession(username, password, Collections.emptyMap()); } /* (non-Javadoc) - * @see org.apache.hive.service.cli.ICLIService#closeSession(org.apache.hive.service.cli.SessionHandle) - */ - @Override - public abstract void closeSession(SessionHandle sessionHandle) throws HiveSQLException; - - /* (non-Javadoc) - * @see org.apache.hive.service.cli.ICLIService#getInfo(org.apache.hive.service.cli.SessionHandle, java.util.List) - */ - @Override - public abstract GetInfoValue getInfo(SessionHandle sessionHandle, GetInfoType getInfoType) - throws HiveSQLException; - - /* (non-Javadoc) - * @see org.apache.hive.service.cli.ICLIService#executeStatement(org.apache.hive.service.cli.SessionHandle, - * java.lang.String, java.util.Map) - */ - @Override - public abstract OperationHandle executeStatement(SessionHandle sessionHandle, String statement, - Map confOverlay) throws HiveSQLException; - - /* (non-Javadoc) - * @see org.apache.hive.service.cli.ICLIService#executeStatementAsync(org.apache.hive.service.cli.SessionHandle, - * java.lang.String, java.util.Map) - */ - @Override - public abstract OperationHandle executeStatementAsync(SessionHandle sessionHandle, String statement, - Map confOverlay) throws HiveSQLException; - - /* (non-Javadoc) - * @see org.apache.hive.service.cli.ICLIService#getTypeInfo(org.apache.hive.service.cli.SessionHandle) - */ - @Override - public abstract OperationHandle getTypeInfo(SessionHandle sessionHandle) throws HiveSQLException; - - /* (non-Javadoc) - * @see org.apache.hive.service.cli.ICLIService#getCatalogs(org.apache.hive.service.cli.SessionHandle) - */ - @Override - public abstract OperationHandle getCatalogs(SessionHandle sessionHandle) throws HiveSQLException; - - /* (non-Javadoc) - * @see org.apache.hive.service.cli.ICLIService#getSchemas(org.apache.hive.service.cli.SessionHandle, java.lang.String, java.lang.String) - */ - @Override - public abstract OperationHandle getSchemas(SessionHandle sessionHandle, String catalogName, - String schemaName) throws HiveSQLException; - - /* (non-Javadoc) - * @see org.apache.hive.service.cli.ICLIService#getTables(org.apache.hive.service.cli.SessionHandle, java.lang.String, java.lang.String, java.lang.String, java.util.List) - */ - @Override - public abstract OperationHandle getTables(SessionHandle sessionHandle, String catalogName, - String schemaName, String tableName, List tableTypes) throws HiveSQLException; - - /* (non-Javadoc) - * @see org.apache.hive.service.cli.ICLIService#getTableTypes(org.apache.hive.service.cli.SessionHandle) - */ - @Override - public abstract OperationHandle getTableTypes(SessionHandle sessionHandle) throws HiveSQLException; - - /* (non-Javadoc) - * @see org.apache.hive.service.cli.ICLIService#getColumns(org.apache.hive.service.cli.SessionHandle, java.lang.String, java.lang.String, java.lang.String, java.lang.String) - */ - @Override - public abstract OperationHandle getColumns(SessionHandle sessionHandle, String catalogName, - String schemaName, String tableName, String columnName) throws HiveSQLException; - - /* (non-Javadoc) - * @see org.apache.hive.service.cli.ICLIService#getFunctions(org.apache.hive.service.cli.SessionHandle, java.lang.String) - */ - @Override - public abstract OperationHandle getFunctions(SessionHandle sessionHandle, - String catalogName, String schemaName, String functionName) - throws HiveSQLException; - - /* (non-Javadoc) - * @see org.apache.hive.service.cli.ICLIService#getOperationStatus(org.apache.hive.service.cli.OperationHandle) - */ - @Override - public abstract OperationState getOperationStatus(OperationHandle opHandle) throws HiveSQLException; - - /* (non-Javadoc) - * @see org.apache.hive.service.cli.ICLIService#cancelOperation(org.apache.hive.service.cli.OperationHandle) - */ - @Override - public abstract void cancelOperation(OperationHandle opHandle) throws HiveSQLException; - - /* (non-Javadoc) - * @see org.apache.hive.service.cli.ICLIService#closeOperation(org.apache.hive.service.cli.OperationHandle) - */ - @Override - public abstract void closeOperation(OperationHandle opHandle) throws HiveSQLException; - - /* (non-Javadoc) - * @see org.apache.hive.service.cli.ICLIService#getResultSetMetadata(org.apache.hive.service.cli.OperationHandle) - */ - @Override - public abstract TableSchema getResultSetMetadata(OperationHandle opHandle) throws HiveSQLException; - - /* (non-Javadoc) - * @see org.apache.hive.service.cli.ICLIService#fetchResults(org.apache.hive.service.cli.OperationHandle, org.apache.hive.service.cli.FetchOrientation, long) - */ - @Override - public abstract RowSet fetchResults(OperationHandle opHandle, FetchOrientation orientation, long maxRows) - throws HiveSQLException; - - /* (non-Javadoc) * @see org.apache.hive.service.cli.ICLIService#fetchResults(org.apache.hive.service.cli.OperationHandle) */ @Override diff --git a/service/src/java/org/apache/hive/service/cli/EmbeddedCLIServiceClient.java b/service/src/java/org/apache/hive/service/cli/EmbeddedCLIServiceClient.java index 9dca874..8abdce9 100644 --- a/service/src/java/org/apache/hive/service/cli/EmbeddedCLIServiceClient.java +++ b/service/src/java/org/apache/hive/service/cli/EmbeddedCLIServiceClient.java @@ -151,7 +151,7 @@ public OperationHandle getFunctions(SessionHandle sessionHandle, * @see org.apache.hive.service.cli.CLIServiceClient#getOperationStatus(org.apache.hive.service.cli.OperationHandle) */ @Override - public OperationState getOperationStatus(OperationHandle opHandle) throws HiveSQLException { + public OperationStatus getOperationStatus(OperationHandle opHandle) throws HiveSQLException { return cliService.getOperationStatus(opHandle); } diff --git a/service/src/java/org/apache/hive/service/cli/ICLIService.java b/service/src/java/org/apache/hive/service/cli/ICLIService.java index f647ce6..2b1712d 100644 --- a/service/src/java/org/apache/hive/service/cli/ICLIService.java +++ b/service/src/java/org/apache/hive/service/cli/ICLIService.java @@ -72,7 +72,7 @@ public abstract OperationHandle getFunctions(SessionHandle sessionHandle, String catalogName, String schemaName, String functionName) throws HiveSQLException; - public abstract OperationState getOperationStatus(OperationHandle opHandle) + public abstract OperationStatus getOperationStatus(OperationHandle opHandle) throws HiveSQLException; public abstract void cancelOperation(OperationHandle opHandle) diff --git a/service/src/java/org/apache/hive/service/cli/OperationStatus.java b/service/src/java/org/apache/hive/service/cli/OperationStatus.java new file mode 100644 index 0000000..7bba9f2 --- /dev/null +++ b/service/src/java/org/apache/hive/service/cli/OperationStatus.java @@ -0,0 +1,43 @@ +/** + * 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.hive.service.cli; + + +/** + * OperationStatus. + * + */ +public class OperationStatus { + + private final OperationState state; + private final Exception runException; + + public OperationStatus(OperationState state, Exception runException) { + this.state = state; + this.runException = runException; + } + + public OperationState getState() { + return state; + } + + public Exception getRunException() { + return runException; + } +} \ No newline at end of file diff --git a/service/src/java/org/apache/hive/service/cli/operation/Operation.java b/service/src/java/org/apache/hive/service/cli/operation/Operation.java index 6f4b8dc..f78fcb0 100644 --- a/service/src/java/org/apache/hive/service/cli/operation/Operation.java +++ b/service/src/java/org/apache/hive/service/cli/operation/Operation.java @@ -70,6 +70,10 @@ public OperationState getState() { return state; } + public Exception getRunException() { + return null; + } + public boolean hasResultSet() { return hasResultSet; } diff --git a/service/src/java/org/apache/hive/service/cli/operation/OperationManager.java b/service/src/java/org/apache/hive/service/cli/operation/OperationManager.java index bcdb67f..d3528b5 100644 --- a/service/src/java/org/apache/hive/service/cli/operation/OperationManager.java +++ b/service/src/java/org/apache/hive/service/cli/operation/OperationManager.java @@ -144,6 +144,10 @@ public OperationState getOperationState(OperationHandle opHandle) throws HiveSQL return getOperation(opHandle).getState(); } + public Exception getOperationRunException(OperationHandle opHandle) throws HiveSQLException { + return getOperation(opHandle).getRunException(); + } + public void cancelOperation(OperationHandle opHandle) throws HiveSQLException { getOperation(opHandle).cancel(); } 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 f6adf92..3df508d 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 @@ -68,6 +68,7 @@ private SerDe serde = null; private final boolean runAsync; private Future backgroundHandle; + private Exception runException = null; public SQLOperation(HiveSession parentSession, String statement, Map confOverlay, boolean runInBackground) { @@ -76,20 +77,11 @@ public SQLOperation(HiveSession parentSession, String statement, Map pollTimeout) { + System.out.println("Polling timed out"); + break; + } + opStatusReq = new TGetOperationStatusReq(); + opStatusReq.setOperationHandle(operationHandle); + assertNotNull(opStatusReq); + opStatusResp = client.GetOperationStatus(opStatusReq); + state = opStatusResp.getOperationState(); + System.out.println("Current state: " + state); + + if (state == TOperationState.CANCELED_STATE || state == TOperationState.CLOSED_STATE + || state == TOperationState.FINISHED_STATE || state == TOperationState.ERROR_STATE) { + isQueryRunning = false; + } + Thread.sleep(1000); + } + + // Expect query to be successfully completed now + assertEquals("Query should be finished", + TOperationState.FINISHED_STATE, state); + + // Execute a malformed query + queryString = "SELECT NON_EXISTING_COLUMN FROM TEST_EXEC_ASYNC_THRIFT"; + System.out.println("Will attempt to execute: " + queryString); + execResp = executeQuery(queryString, sessHandle, true); + operationHandle = execResp.getOperationHandle(); + assertNotNull(operationHandle); + isQueryRunning = true; + while(isQueryRunning) { + // Break if polling times out + if (System.currentTimeMillis() > pollTimeout) { + System.out.println("Polling timed out"); + break; + } + opStatusReq = new TGetOperationStatusReq(); + opStatusReq.setOperationHandle(operationHandle); + assertNotNull(opStatusReq); + opStatusResp = client.GetOperationStatus(opStatusReq); + state = opStatusResp.getOperationState(); + System.out.println("Current state: " + state); + + if (state == TOperationState.CANCELED_STATE || state == TOperationState.CLOSED_STATE + || state == TOperationState.FINISHED_STATE || state == TOperationState.ERROR_STATE) { + isQueryRunning = false; + } + Thread.sleep(1000); + } + + // Expect query to return an error state + assertEquals("Query should be finished", + TOperationState.ERROR_STATE, state); + + // sqlState and errorMessage should be set to appropriate values + assertEquals(opStatusResp.getStatus().getSqlState(), "42000"); + assertEquals(opStatusResp.getStatus().getErrorCode(), 10004); + assertNotNull(opStatusResp.getStatus().getErrorMessage()); + + // Cleanup + queryString = "DROP TABLE TEST_EXEC_ASYNC_THRIFT"; + executeQuery(queryString, sessHandle, false); // Close the session; ignore exception if any TCloseSessionReq closeReq = new TCloseSessionReq(sessHandle); client.CloseSession(closeReq); } - private TExecuteStatementResp executeQuerySync(String queryString, TSessionHandle sessHandle) + private TExecuteStatementResp executeQuery(String queryString, TSessionHandle sessHandle, boolean runAsync) throws Exception { TExecuteStatementReq execReq = new TExecuteStatementReq(); execReq.setSessionHandle(sessHandle); execReq.setStatement(queryString); - execReq.setRunAsync(false); + execReq.setRunAsync(runAsync); TExecuteStatementResp execResp = client.ExecuteStatement(execReq); assertNotNull(execResp); assertFalse(execResp.getStatus().getStatusCode() == TStatusCode.ERROR_STATUS);