private void prepare(QueryState queryState) throws HiveSQLException {
    setState(OperationState.RUNNING);
    try {
      driver = DriverFactory.newDriver(queryState, queryInfo);

      // Start the timer thread for canceling the query when query timeout is reached
      // queryTimeout == 0 means no timeout
      if (queryTimeout > 0L) {
        timeoutExecutor = Executors.newSingleThreadScheduledExecutor();
        timeoutExecutor.schedule(() -> {
          try {
            final String queryId = queryState.getQueryId();
            log.info("Query timed out after: {} seconds. Cancelling the execution now: {}", queryTimeout, queryId);
            SQLOperation.this.cancel(OperationState.TIMEDOUT);
          } catch (HiveSQLException e) {
            log.error("Error cancelling the query after timeout: {} seconds", queryTimeout, e);
          }
          return null;
        }, queryTimeout, TimeUnit.SECONDS);
      }

      queryInfo.setQueryDisplay(driver.getQueryDisplay());
      if (operationLog != null) {
        queryInfo.setOperationLogLocation(operationLog.toString());
      }

      // set the operation handle information in Driver, so that thrift API users
      // can use the operation handle they receive, to lookup query information in
      // Yarn ATS, also used in logging so remove padding for better display
      String guid64 = Base64.getUrlEncoder().withoutPadding()
          .encodeToString(getHandle().getHandleIdentifier().toTHandleIdentifier().getGuid());
      driver.setOperationId(guid64);

      // 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.compileAndRespond(statement);
      if (queryState.getQueryTag() != null && queryState.getQueryId() != null) {
        parentSession.updateQueryTag(queryState.getQueryId(), queryState.getQueryTag());
      }
      setHasResultSet(driver.hasResultSet());
    } catch (CommandProcessorException e) {
      setState(OperationState.ERROR);
      throw toSQLException("Error while compiling statement", e, queryState.getQueryId());
    } catch (Throwable e) {
      setState(OperationState.ERROR);
      if (e instanceof OutOfMemoryError) {
        throw e;
      }
      throw new HiveSQLException("Error running query", e, queryState.getQueryId());
    }
  }
Compile the query and extract metadata

@throws HiveSQLException
service/src/java/org/apache/hive/service/cli/operation/SQLOperation.java