private static void runOnDriverInternal(String query, HiveConf conf, SessionState sessionState, DriverCreator creator) throws HiveException {
    SessionState.setCurrentSessionState(sessionState);
    boolean isOk = false;
    try {
      Driver driver = creator.createDriver(
          new QueryState.Builder()
              .withHiveConf(conf)
              .withGenerateNewQueryId(true)
              .nonIsolated()
              .build());
      try {
        try {
          driver.run(query);
        } catch (CommandProcessorException e) {
          LOG.error("Failed to run " + query, e);
          throw new HiveException("Failed to run " + query, e);
        }
      } finally {
        driver.close();
        driver.destroy();
      }
      isOk = true;
    } finally {
      if (!isOk) {
        try {
          sessionState.close(); // This also resets SessionState.get.
        } catch (Throwable th) {
          LOG.warn("Failed to close a bad session", th);
          SessionState.detachSession();
        }
      }
    }
  }
For Query Based compaction to run the query to generate the compacted data.
ql/src/java/org/apache/hadoop/hive/ql/DriverUtils.java