Exemple #1
0
  private SQLResponse queryWithSqlMassage(SQLRequest sqlRequest) throws Exception {
    SQLResponse fakeResponse = QueryUtil.tableauIntercept(sqlRequest.getSql());
    if (null != fakeResponse) {
      logger.debug("Return fake response, is exception? " + fakeResponse.getIsException());
      return fakeResponse;
    }

    String correctedSql = QueryUtil.massageSql(sqlRequest);
    if (correctedSql.equals(sqlRequest.getSql()) == false)
      logger.debug("The corrected query: " + correctedSql);

    // add extra parameters into olap context, like acceptPartial
    Map<String, String> parameters = new HashMap<String, String>();
    parameters.put(
        OLAPContext.PRM_ACCEPT_PARTIAL_RESULT, String.valueOf(sqlRequest.isAcceptPartial()));
    OLAPContext.setParameters(parameters);

    try {
      BackdoorToggles.setToggles(sqlRequest.getBackdoorToggles());

      return execute(correctedSql, sqlRequest);

    } finally {
      BackdoorToggles.cleanToggles();
    }
  }
Exemple #2
0
  public void logQuery(final SQLRequest request, final SQLResponse response) {
    final String user = SecurityContextHolder.getContext().getAuthentication().getName();
    final Set<String> realizationNames = new HashSet<String>();
    final Set<Long> cuboidIds = new HashSet<Long>();
    float duration = response.getDuration() / (float) 1000;

    if (!response.isHitCache() && null != OLAPContext.getThreadLocalContexts()) {
      for (OLAPContext ctx : OLAPContext.getThreadLocalContexts()) {
        Cuboid cuboid = ctx.storageContext.getCuboid();
        if (cuboid != null) {
          // Some queries do not involve cuboid, e.g. lookup table query
          cuboidIds.add(cuboid.getId());
        }

        if (ctx.realization != null) {
          String realizationName = ctx.realization.getName();
          realizationNames.add(realizationName);
        }
      }
    }

    int resultRowCount = 0;
    if (!response.getIsException() && response.getResults() != null) {
      resultRowCount = response.getResults().size();
    }

    String newLine = System.getProperty("line.separator");
    StringBuilder stringBuilder = new StringBuilder();
    stringBuilder.append(newLine);
    stringBuilder
        .append("==========================[QUERY]===============================")
        .append(newLine);
    stringBuilder.append("SQL: ").append(request.getSql()).append(newLine);
    stringBuilder.append("User: "******"Success: ")
        .append((null == response.getExceptionMessage()))
        .append(newLine);
    stringBuilder.append("Duration: ").append(duration).append(newLine);
    stringBuilder.append("Project: ").append(request.getProject()).append(newLine);
    stringBuilder.append("Realization Names: ").append(realizationNames).append(newLine);
    stringBuilder.append("Cuboid Ids: ").append(cuboidIds).append(newLine);
    stringBuilder.append("Total scan count: ").append(response.getTotalScanCount()).append(newLine);
    stringBuilder.append("Result row count: ").append(resultRowCount).append(newLine);
    stringBuilder.append("Accept Partial: ").append(request.isAcceptPartial()).append(newLine);
    stringBuilder.append("Is Partial Result: ").append(response.isPartial()).append(newLine);
    stringBuilder.append("Hit Cache: ").append(response.isHitCache()).append(newLine);
    stringBuilder.append("Message: ").append(response.getExceptionMessage()).append(newLine);
    stringBuilder
        .append("==========================[QUERY]===============================")
        .append(newLine);

    logger.info(stringBuilder.toString());
  }
Exemple #3
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;
  }