Beispiel #1
0
  /**
   * @param sql
   * @param sqlRequest
   * @return
   * @throws Exception
   */
  private SQLResponse execute(String sql, SQLRequest sqlRequest) throws Exception {
    Connection conn = null;
    Statement stat = null;
    ResultSet resultSet = null;
    long startTime = System.currentTimeMillis();

    List<List<String>> results = new LinkedList<List<String>>();
    List<SelectedColumnMeta> columnMetas = new LinkedList<SelectedColumnMeta>();

    try {
      conn = getOLAPDataSource(sqlRequest.getProject()).getConnection();

      if (sqlRequest instanceof PrepareSqlRequest) {
        PreparedStatement preparedState = conn.prepareStatement(sql);

        for (int i = 0; i < ((PrepareSqlRequest) sqlRequest).getParams().length; i++) {
          setParam(preparedState, i + 1, ((PrepareSqlRequest) sqlRequest).getParams()[i]);
        }

        resultSet = preparedState.executeQuery();
      } else {
        stat = conn.createStatement();
        resultSet = stat.executeQuery(sql);
      }

      ResultSetMetaData metaData = resultSet.getMetaData();
      int columnCount = metaData.getColumnCount();

      // Fill in selected column meta
      for (int i = 1; i <= columnCount; ++i) {
        columnMetas.add(
            new SelectedColumnMeta(
                metaData.isAutoIncrement(i),
                metaData.isCaseSensitive(i),
                metaData.isSearchable(i),
                metaData.isCurrency(i),
                metaData.isNullable(i),
                metaData.isSigned(i),
                metaData.getColumnDisplaySize(i),
                metaData.getColumnLabel(i),
                metaData.getColumnName(i),
                metaData.getSchemaName(i),
                metaData.getCatalogName(i),
                metaData.getTableName(i),
                metaData.getPrecision(i),
                metaData.getScale(i),
                metaData.getColumnType(i),
                metaData.getColumnTypeName(i),
                metaData.isReadOnly(i),
                metaData.isWritable(i),
                metaData.isDefinitelyWritable(i)));
      }

      List<String> oneRow = new LinkedList<String>();

      // fill in results
      while (resultSet.next()) {
        for (int i = 0; i < columnCount; i++) {
          oneRow.add((resultSet.getString(i + 1)));
        }

        results.add(new ArrayList<String>(oneRow));
        oneRow.clear();
      }
    } catch (SQLException sqlException) {
      // hive maybe able to answer where kylin failed
      HiveReroute reroute = new HiveReroute();
      if (reroute.shouldReroute(sqlException) == false) {
        throw sqlException;
      }
      try {
        reroute.query(sql, results, columnMetas);
      } catch (Throwable ex) {
        logger.error("Reroute hive failed", ex);
        throw sqlException;
      }
    } finally {
      close(resultSet, stat, conn);
    }

    boolean isPartialResult = false;
    String cube = "";
    long totalScanCount = 0;
    if (OLAPContext.getThreadLocalContexts()
        != null) { // contexts can be null in case of 'explain plan for'
      for (OLAPContext ctx : OLAPContext.getThreadLocalContexts()) {
        isPartialResult |= ctx.storageContext.isPartialResultReturned();
        cube = ctx.realization.getName();
        totalScanCount += ctx.storageContext.getTotalScanCount();
      }
    }

    SQLResponse response =
        new SQLResponse(columnMetas, results, cube, 0, false, null, isPartialResult);
    response.setTotalScanCount(totalScanCount);
    response.setDuration(System.currentTimeMillis() - startTime);

    return response;
  }