org.apache.kylin.rest.request.PrepareSqlRequest Java Examples

The following examples show how to use org.apache.kylin.rest.request.PrepareSqlRequest. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DashboardController.java    From kylin-on-parquet-v2 with Apache License 2.0 6 votes vote down vote up
@RequestMapping(value = "/metric/query", method = RequestMethod.GET)
@ResponseBody
public MetricsResponse getQueryMetrics(@RequestParam(value = "projectName", required = false) String projectName,
        @RequestParam(value = "cubeName", required = false) String cubeName,
        @RequestParam(value = "startTime") String startTime, @RequestParam(value = "endTime") String endTime) {
    checkAuthorization(projectName);
    MetricsResponse queryMetrics = new MetricsResponse();
    PrepareSqlRequest sqlRequest = dashboardService.getQueryMetricsSQLRequest(startTime, endTime, projectName,
            cubeName);
    SQLResponse sqlResponse = queryService.doQueryWithCache(sqlRequest);
    if (!sqlResponse.getIsException()) {
        queryMetrics.increase("queryCount",
                dashboardService.getMetricValue(sqlResponse.getResults().get(0).get(0)));
        queryMetrics.increase("avgQueryLatency",
                dashboardService.getMetricValue(sqlResponse.getResults().get(0).get(1)));
        queryMetrics.increase("maxQueryLatency",
                dashboardService.getMetricValue(sqlResponse.getResults().get(0).get(2)));
        queryMetrics.increase("minQueryLatency",
                dashboardService.getMetricValue(sqlResponse.getResults().get(0).get(3)));
    }
    return queryMetrics;
}
 
Example #2
Source File: DashboardController.java    From kylin-on-parquet-v2 with Apache License 2.0 6 votes vote down vote up
@RequestMapping(value = "/metric/job", method = RequestMethod.GET)
@ResponseBody
public MetricsResponse getJobMetrics(@RequestParam(value = "projectName", required = false) String projectName,
        @RequestParam(value = "cubeName", required = false) String cubeName,
        @RequestParam(value = "startTime") String startTime, @RequestParam(value = "endTime") String endTime) {
    checkAuthorization(projectName);
    MetricsResponse jobMetrics = new MetricsResponse();
    PrepareSqlRequest sqlRequest = dashboardService.getJobMetricsSQLRequest(startTime, endTime, projectName,
            cubeName);
    SQLResponse sqlResponse = queryService.doQueryWithCache(sqlRequest);
    if (!sqlResponse.getIsException()) {
        jobMetrics.increase("jobCount", dashboardService.getMetricValue(sqlResponse.getResults().get(0).get(0)));
        jobMetrics.increase("avgJobBuildTime",
                dashboardService.getMetricValue(sqlResponse.getResults().get(0).get(1)));
        jobMetrics.increase("maxJobBuildTime",
                dashboardService.getMetricValue(sqlResponse.getResults().get(0).get(2)));
        jobMetrics.increase("minJobBuildTime",
                dashboardService.getMetricValue(sqlResponse.getResults().get(0).get(3)));
    }
    return jobMetrics;
}
 
Example #3
Source File: QueryController.java    From Kylin with Apache License 2.0 6 votes vote down vote up
@RequestMapping(value = "/query/prestate", method = RequestMethod.POST, produces = "application/json")
@ResponseBody
@Timed(name = "query")
public SQLResponse prepareQuery(@RequestBody PrepareSqlRequest sqlRequest) {
    long startTimestamp = System.currentTimeMillis();

    SQLResponse response = doQuery(sqlRequest);
    response.setDuration(System.currentTimeMillis() - startTimestamp);

    queryService.logQuery(sqlRequest, response, new Date(startTimestamp), new Date(System.currentTimeMillis()));

    if (response.getIsException()) {
        String errorMsg = response.getExceptionMessage();
        throw new InternalErrorException(QueryUtil.makeErrorMsgUserFriendly(errorMsg));
    }

    return response;
}
 
Example #4
Source File: DashboardController.java    From kylin with Apache License 2.0 6 votes vote down vote up
@RequestMapping(value = "/metric/job", method = RequestMethod.GET)
@ResponseBody
public MetricsResponse getJobMetrics(@RequestParam(value = "projectName", required = false) String projectName,
        @RequestParam(value = "cubeName", required = false) String cubeName,
        @RequestParam(value = "startTime") String startTime, @RequestParam(value = "endTime") String endTime) {
    checkAuthorization(projectName);
    MetricsResponse jobMetrics = new MetricsResponse();
    PrepareSqlRequest sqlRequest = dashboardService.getJobMetricsSQLRequest(startTime, endTime, projectName,
            cubeName);
    SQLResponse sqlResponse = queryService.doQueryWithCache(sqlRequest);
    if (!sqlResponse.getIsException()) {
        jobMetrics.increase("jobCount", dashboardService.getMetricValue(sqlResponse.getResults().get(0).get(0)));
        jobMetrics.increase("avgJobBuildTime",
                dashboardService.getMetricValue(sqlResponse.getResults().get(0).get(1)));
        jobMetrics.increase("maxJobBuildTime",
                dashboardService.getMetricValue(sqlResponse.getResults().get(0).get(2)));
        jobMetrics.increase("minJobBuildTime",
                dashboardService.getMetricValue(sqlResponse.getResults().get(0).get(3)));
    }
    return jobMetrics;
}
 
Example #5
Source File: DashboardController.java    From kylin with Apache License 2.0 6 votes vote down vote up
@RequestMapping(value = "/metric/query", method = RequestMethod.GET)
@ResponseBody
public MetricsResponse getQueryMetrics(@RequestParam(value = "projectName", required = false) String projectName,
        @RequestParam(value = "cubeName", required = false) String cubeName,
        @RequestParam(value = "startTime") String startTime, @RequestParam(value = "endTime") String endTime) {
    checkAuthorization(projectName);
    MetricsResponse queryMetrics = new MetricsResponse();
    PrepareSqlRequest sqlRequest = dashboardService.getQueryMetricsSQLRequest(startTime, endTime, projectName,
            cubeName);
    SQLResponse sqlResponse = queryService.doQueryWithCache(sqlRequest);
    if (!sqlResponse.getIsException()) {
        queryMetrics.increase("queryCount",
                dashboardService.getMetricValue(sqlResponse.getResults().get(0).get(0)));
        queryMetrics.increase("avgQueryLatency",
                dashboardService.getMetricValue(sqlResponse.getResults().get(0).get(1)));
        queryMetrics.increase("maxQueryLatency",
                dashboardService.getMetricValue(sqlResponse.getResults().get(0).get(2)));
        queryMetrics.increase("minQueryLatency",
                dashboardService.getMetricValue(sqlResponse.getResults().get(0).get(3)));
    }
    return queryMetrics;
}
 
Example #6
Source File: DashboardService.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
public PrepareSqlRequest getChartSQLRequest(String startTime, String endTime, String projectName, String cubeName,
        String dimension, String metric, String category) {
    try {
        CategoryEnum categoryEnum = CategoryEnum.valueOf(category);
        String table = "";
        String[] dimensionSQL = null;
        String[] metricSQL = null;

        if (categoryEnum == CategoryEnum.QUERY) {
            dimensionSQL = new String[] { QueryDimensionEnum.valueOf(dimension).toSQL() };
            metricSQL = new String[] { QueryMetricEnum.valueOf(metric).toSQL() };
            table = getMetricsManager().getSystemTableFromSubject(getConfig().getKylinMetricsSubjectQuery());
        } else if (categoryEnum == CategoryEnum.JOB) {
            dimensionSQL = new String[] { JobDimensionEnum.valueOf(dimension).toSQL() };
            metricSQL = new String[] { JobMetricEnum.valueOf(metric).toSQL() };
            table = getMetricsManager().getSystemTableFromSubject(getConfig().getKylinMetricsSubjectJob());
        }

        Map<String, String> filterMap = getBaseFilterMap(categoryEnum, projectName, startTime, endTime);
        filterMap.putAll(getCubeFilterMap(categoryEnum, cubeName));

        return createPrepareSqlRequest(dimensionSQL, metricSQL, table, filterMap);
    } catch (IllegalArgumentException e) {
        String message = "Generate dashboard chart sql failed. Please double check the input parameter: dimension, metric or category.";
        logger.error(message, e);
        throw new BadRequestException(message + " Caused by: " + e.getMessage(), null, e.getCause());
    }
}
 
Example #7
Source File: DashboardService.java    From kylin with Apache License 2.0 5 votes vote down vote up
public PrepareSqlRequest getQueryMetricsSQLRequest(String startTime, String endTime, String projectName,
        String cubeName) {
    String[] metrics = new String[] { QueryMetricEnum.QUERY_COUNT.toSQL(),
            QueryMetricEnum.AVG_QUERY_LATENCY.toSQL(), QueryMetricEnum.MAX_QUERY_LATENCY.toSQL(),
            QueryMetricEnum.MIN_QUERY_LATENCY.toSQL() };
    Map<String, String> filterMap = getBaseFilterMap(CategoryEnum.QUERY, projectName, startTime, endTime);
    filterMap.putAll(getCubeFilterMap(CategoryEnum.QUERY, cubeName));
    return createPrepareSqlRequest(null, metrics,
            getMetricsManager().getSystemTableFromSubject(getConfig().getKylinMetricsSubjectQuery()), filterMap);
}
 
Example #8
Source File: CubeService.java    From kylin with Apache License 2.0 5 votes vote down vote up
private List<List<String>> getPrepareQueryResult(String cubeName, String sql) {
    PrepareSqlRequest sqlRequest = new PrepareSqlRequest();
    sqlRequest.setProject(MetricsManager.SYSTEM_PROJECT);
    PrepareSqlRequest.StateParam[] params = new PrepareSqlRequest.StateParam[1];
    params[0] = new PrepareSqlRequest.StateParam();
    params[0].setClassName("java.lang.String");
    params[0].setValue(cubeName);
    sqlRequest.setParams(params);
    sqlRequest.setSql(sql);

    return queryService.doQueryWithCache(sqlRequest, false).getResults();
}
 
Example #9
Source File: DashboardService.java    From kylin with Apache License 2.0 5 votes vote down vote up
public PrepareSqlRequest getJobMetricsSQLRequest(String startTime, String endTime, String projectName,
        String cubeName) {
    String[] metrics = new String[] { JobMetricEnum.JOB_COUNT.toSQL(), JobMetricEnum.AVG_JOB_BUILD_TIME.toSQL(),
            JobMetricEnum.MAX_JOB_BUILD_TIME.toSQL(), JobMetricEnum.MIN_JOB_BUILD_TIME.toSQL() };
    Map<String, String> filterMap = getBaseFilterMap(CategoryEnum.JOB, projectName, startTime, endTime);
    filterMap.putAll(getCubeFilterMap(CategoryEnum.JOB, cubeName));
    return createPrepareSqlRequest(null, metrics,
            getMetricsManager().getSystemTableFromSubject(getConfig().getKylinMetricsSubjectJob()), filterMap);
}
 
Example #10
Source File: DashboardService.java    From kylin with Apache License 2.0 5 votes vote down vote up
public PrepareSqlRequest getChartSQLRequest(String startTime, String endTime, String projectName, String cubeName,
        String dimension, String metric, String category) {
    try {
        CategoryEnum categoryEnum = CategoryEnum.valueOf(category);
        String table = "";
        String[] dimensionSQL = null;
        String[] metricSQL = null;

        if (categoryEnum == CategoryEnum.QUERY) {
            dimensionSQL = new String[] { QueryDimensionEnum.valueOf(dimension).toSQL() };
            metricSQL = new String[] { QueryMetricEnum.valueOf(metric).toSQL() };
            table = getMetricsManager().getSystemTableFromSubject(getConfig().getKylinMetricsSubjectQuery());
        } else if (categoryEnum == CategoryEnum.JOB) {
            dimensionSQL = new String[] { JobDimensionEnum.valueOf(dimension).toSQL() };
            metricSQL = new String[] { JobMetricEnum.valueOf(metric).toSQL() };
            table = getMetricsManager().getSystemTableFromSubject(getConfig().getKylinMetricsSubjectJob());
        }

        Map<String, String> filterMap = getBaseFilterMap(categoryEnum, projectName, startTime, endTime);
        filterMap.putAll(getCubeFilterMap(categoryEnum, cubeName));

        return createPrepareSqlRequest(dimensionSQL, metricSQL, table, filterMap);
    } catch (IllegalArgumentException e) {
        String message = "Generate dashboard chart sql failed. Please double check the input parameter: dimension, metric or category.";
        logger.error(message, e);
        throw new BadRequestException(message + " Caused by: " + e.getMessage(), null, e.getCause());
    }
}
 
Example #11
Source File: QueryService.java    From kylin with Apache License 2.0 5 votes vote down vote up
private SQLResponse executePrepareRequest(String correctedSql, PrepareSqlRequest sqlRequest,
        PreparedContext preparedContext) throws Exception {
    ResultSet resultSet = null;
    boolean isPushDown = false;

    Pair<List<List<String>>, List<SelectedColumnMeta>> r = null;
    Connection conn = preparedContext.conn;
    try {
        PreparedStatement preparedStatement = preparedContext.preparedStatement;
        processStatementAttr(preparedStatement, sqlRequest);
        for (int i = 0; i < sqlRequest.getParams().length; i++) {
            setParam(preparedStatement, i + 1, (sqlRequest.getParams()[i]));
        }
        resultSet = preparedStatement.executeQuery();
        r = createResponseFromResultSet(resultSet);
    } catch (SQLException sqlException) {
        r = pushDownQuery(sqlRequest, correctedSql, conn, sqlException);
        if (r == null)
            throw sqlException;

        isPushDown = true;
    } finally {
        DBUtils.closeQuietly(resultSet);
    }

    return buildSqlResponse(sqlRequest.getProject(), isPushDown, r.getFirst(), r.getSecond());
}
 
Example #12
Source File: DashboardController.java    From kylin with Apache License 2.0 5 votes vote down vote up
@RequestMapping(value = "/chart/{category}/{metric}/{dimension}", method = RequestMethod.GET)
@ResponseBody
public MetricsResponse getChartData(@PathVariable String dimension, @PathVariable String metric,
        @PathVariable String category, @RequestParam(value = "projectName", required = false) String projectName,
        @RequestParam(value = "cubeName", required = false) String cubeName,
        @RequestParam(value = "startTime") String startTime, @RequestParam(value = "endTime") String endTime) {
    checkAuthorization(projectName);
    PrepareSqlRequest sqlRequest = dashboardService.getChartSQLRequest(startTime, endTime, projectName, cubeName,
            dimension, metric, category);
    return dashboardService.transformChartData(queryService.doQueryWithCache(sqlRequest));
}
 
Example #13
Source File: QueryController.java    From kylin with Apache License 2.0 5 votes vote down vote up
@RequestMapping(value = "/query/prestate", method = RequestMethod.POST, produces = "application/json")
@ResponseBody
public SQLResponse prepareQuery(@RequestBody PrepareSqlRequest sqlRequest) {
    Map<String, String> newToggles = Maps.newHashMap();
    if (sqlRequest.getBackdoorToggles() != null)
        newToggles.putAll(sqlRequest.getBackdoorToggles());
    newToggles.put(BackdoorToggles.DEBUG_TOGGLE_PREPARE_ONLY, "true");
    sqlRequest.setBackdoorToggles(newToggles);

    return queryService.doQueryWithCache(sqlRequest);
}
 
Example #14
Source File: QueryControllerTest.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
@Test(expected = Exception.class)
public void testQueryException() throws Exception {
    PrepareSqlRequest sqlRequest = new PrepareSqlRequest();
    sqlRequest.setSql("select * from not_exist_table");
    sqlRequest.setProject("default");
    SQLResponse response1 = queryController.query(sqlRequest);
    Assert.assertEquals(false, response1.getIsException());

    SQLResponse response2 = queryController.query(sqlRequest);
    Assert.assertEquals(false, response2.getIsException());
}
 
Example #15
Source File: QueryControllerTest.java    From kylin with Apache License 2.0 5 votes vote down vote up
@Test(expected = Exception.class)
public void testQueryException() throws Exception {
    PrepareSqlRequest sqlRequest = new PrepareSqlRequest();
    sqlRequest.setSql("select * from not_exist_table");
    sqlRequest.setProject("default");
    SQLResponse response1 = queryController.query(sqlRequest);
    Assert.assertEquals(false, response1.getIsException());

    SQLResponse response2 = queryController.query(sqlRequest);
    Assert.assertEquals(false, response2.getIsException());
}
 
Example #16
Source File: DashboardService.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
public PrepareSqlRequest getJobMetricsSQLRequest(String startTime, String endTime, String projectName,
        String cubeName) {
    String[] metrics = new String[] { JobMetricEnum.JOB_COUNT.toSQL(), JobMetricEnum.AVG_JOB_BUILD_TIME.toSQL(),
            JobMetricEnum.MAX_JOB_BUILD_TIME.toSQL(), JobMetricEnum.MIN_JOB_BUILD_TIME.toSQL() };
    Map<String, String> filterMap = getBaseFilterMap(CategoryEnum.JOB, projectName, startTime, endTime);
    filterMap.putAll(getCubeFilterMap(CategoryEnum.JOB, cubeName));
    return createPrepareSqlRequest(null, metrics,
            getMetricsManager().getSystemTableFromSubject(getConfig().getKylinMetricsSubjectJob()), filterMap);
}
 
Example #17
Source File: DashboardService.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
public PrepareSqlRequest getQueryMetricsSQLRequest(String startTime, String endTime, String projectName,
        String cubeName) {
    String[] metrics = new String[] { QueryMetricEnum.QUERY_COUNT.toSQL(),
            QueryMetricEnum.AVG_QUERY_LATENCY.toSQL(), QueryMetricEnum.MAX_QUERY_LATENCY.toSQL(),
            QueryMetricEnum.MIN_QUERY_LATENCY.toSQL() };
    Map<String, String> filterMap = getBaseFilterMap(CategoryEnum.QUERY, projectName, startTime, endTime);
    filterMap.putAll(getCubeFilterMap(CategoryEnum.QUERY, cubeName));
    return createPrepareSqlRequest(null, metrics,
            getMetricsManager().getSystemTableFromSubject(getConfig().getKylinMetricsSubjectQuery()), filterMap);
}
 
Example #18
Source File: CubeService.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
private List<List<String>> getPrepareQueryResult(String cubeName, String sql) {
    PrepareSqlRequest sqlRequest = new PrepareSqlRequest();
    sqlRequest.setProject(MetricsManager.SYSTEM_PROJECT);
    PrepareSqlRequest.StateParam[] params = new PrepareSqlRequest.StateParam[1];
    params[0] = new PrepareSqlRequest.StateParam();
    params[0].setClassName("java.lang.String");
    params[0].setValue(cubeName);
    sqlRequest.setParams(params);
    sqlRequest.setSql(sql);

    return queryService.doQueryWithCache(sqlRequest, false).getResults();
}
 
Example #19
Source File: QueryService.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
private SQLResponse executePrepareRequest(String correctedSql, PrepareSqlRequest sqlRequest,
        PreparedContext preparedContext) throws Exception {
    ResultSet resultSet = null;
    boolean isPushDown = false;

    Pair<List<List<String>>, List<SelectedColumnMeta>> r = null;
    Connection conn = preparedContext.conn;
    try {
        PreparedStatement preparedStatement = preparedContext.preparedStatement;
        processStatementAttr(preparedStatement, sqlRequest);
        for (int i = 0; i < sqlRequest.getParams().length; i++) {
            setParam(preparedStatement, i + 1, (sqlRequest.getParams()[i]));
        }
        resultSet = preparedStatement.executeQuery();
        r = createResponseFromResultSet(resultSet);
    } catch (SQLException sqlException) {
        r = pushDownQuery(sqlRequest, correctedSql, conn, sqlException);
        if (r == null)
            throw sqlException;

        isPushDown = true;
    } finally {
        DBUtils.closeQuietly(resultSet);
    }

    return buildSqlResponse(sqlRequest.getProject(), isPushDown, r.getFirst(), r.getSecond());
}
 
Example #20
Source File: DashboardController.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
@RequestMapping(value = "/chart/{category}/{metric}/{dimension}", method = RequestMethod.GET)
@ResponseBody
public MetricsResponse getChartData(@PathVariable String dimension, @PathVariable String metric,
        @PathVariable String category, @RequestParam(value = "projectName", required = false) String projectName,
        @RequestParam(value = "cubeName", required = false) String cubeName,
        @RequestParam(value = "startTime") String startTime, @RequestParam(value = "endTime") String endTime) {
    checkAuthorization(projectName);
    PrepareSqlRequest sqlRequest = dashboardService.getChartSQLRequest(startTime, endTime, projectName, cubeName,
            dimension, metric, category);
    return dashboardService.transformChartData(queryService.doQueryWithCache(sqlRequest));
}
 
Example #21
Source File: QueryController.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
@RequestMapping(value = "/query/prestate", method = RequestMethod.POST, produces = "application/json")
@ResponseBody
public SQLResponse prepareQuery(@RequestBody PrepareSqlRequest sqlRequest) {
    Map<String, String> newToggles = Maps.newHashMap();
    if (sqlRequest.getBackdoorToggles() != null)
        newToggles.putAll(sqlRequest.getBackdoorToggles());
    newToggles.put(BackdoorToggles.DEBUG_TOGGLE_PREPARE_ONLY, "true");
    sqlRequest.setBackdoorToggles(newToggles);

    return queryService.doQueryWithCache(sqlRequest);
}
 
Example #22
Source File: DashboardService.java    From kylin with Apache License 2.0 4 votes vote down vote up
private PrepareSqlRequest createPrepareSqlRequest(String[] dimensions, String[] metrics, String category,
        Map<String, String> filterMap) {
    PrepareSqlRequest sqlRequest = new PrepareSqlRequest();
    sqlRequest.setProject(MetricsManager.SYSTEM_PROJECT);
    StringBuffer baseSQL = new StringBuffer("select ");
    StringBuffer groupBy = new StringBuffer("");
    if (dimensions != null && dimensions.length > 0) {
        groupBy.append(" group by ");
        StringBuffer dimensionSQL = new StringBuffer("");
        for (String dimension : dimensions) {
            dimensionSQL.append(",");
            dimensionSQL.append(dimension);
        }
        baseSQL.append(dimensionSQL.substring(1));
        groupBy.append(dimensionSQL.substring(1));
    }
    if (metrics != null && metrics.length > 0) {
        StringBuffer metricSQL = new StringBuffer("");
        for (String metric : metrics) {
            metricSQL.append(",");
            metricSQL.append(metric);
        }
        if (groupBy.length() > 0) {
            baseSQL.append(metricSQL);
        } else {
            baseSQL.append(metricSQL.substring(1));
        }
    }
    baseSQL.append(" from ");
    baseSQL.append(category);
    if (filterMap != null && filterMap.size() > 0) {
        PrepareSqlRequest.StateParam[] params = new PrepareSqlRequest.StateParam[filterMap.size()];
        int i = 0;
        StringBuffer filterSQL = new StringBuffer(" where ");
        Iterator<String> it = filterMap.keySet().iterator();
        String filter = it.next();
        filterSQL.append(filter);
        params[i] = new PrepareSqlRequest.StateParam();
        params[i].setClassName("java.lang.String");
        params[i++].setValue(filterMap.get(filter));

        while (it.hasNext()) {
            filter = it.next();
            filterSQL.append(" and ");
            filterSQL.append(filter);
            params[i] = new PrepareSqlRequest.StateParam();
            params[i].setClassName("java.lang.String");
            params[i++].setValue(filterMap.get(filter));
        }
        baseSQL.append(filterSQL.toString());
        sqlRequest.setParams(params);
    }
    baseSQL.append(groupBy);
    sqlRequest.setSql(baseSQL.toString());
    return sqlRequest;
}
 
Example #23
Source File: QueryController.java    From kylin with Apache License 2.0 4 votes vote down vote up
@RequestMapping(value = "/query", method = RequestMethod.POST, produces = { "application/json" })
@ResponseBody
public SQLResponse query(@RequestBody PrepareSqlRequest sqlRequest) {
    return queryService.doQueryWithCache(sqlRequest);
}
 
Example #24
Source File: QueryService.java    From Kylin with Apache License 2.0 4 votes vote down vote up
/**
 * @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;
    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 LinkedList<String>(oneRow));
            oneRow.clear();
        }
    } 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);

    return response;
}
 
Example #25
Source File: QueryService.java    From Kylin with Apache License 2.0 4 votes vote down vote up
/**
 * @param preparedState
 * @param param
 * @throws SQLException
 */
private void setParam(PreparedStatement preparedState, int index, PrepareSqlRequest.StateParam param) throws SQLException {
    boolean isNull = (null == param.getValue());

    Class<?> clazz;
    try {
        clazz = Class.forName(param.getClassName());
    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e.getMessage(), e);
    }

    Rep rep = Rep.of(clazz);

    switch (rep) {
    case PRIMITIVE_CHAR:
    case CHARACTER:
    case STRING:
        preparedState.setString(index, isNull ? null : String.valueOf(param.getValue()));
        break;
    case PRIMITIVE_INT:
    case INTEGER:
        preparedState.setInt(index, isNull ? 0 : Integer.valueOf(param.getValue()));
        break;
    case PRIMITIVE_SHORT:
    case SHORT:
        preparedState.setShort(index, isNull ? 0 : Short.valueOf(param.getValue()));
        break;
    case PRIMITIVE_LONG:
    case LONG:
        preparedState.setLong(index, isNull ? 0 : Long.valueOf(param.getValue()));
        break;
    case PRIMITIVE_FLOAT:
    case FLOAT:
        preparedState.setFloat(index, isNull ? 0 : Float.valueOf(param.getValue()));
        break;
    case PRIMITIVE_DOUBLE:
    case DOUBLE:
        preparedState.setDouble(index, isNull ? 0 : Double.valueOf(param.getValue()));
        break;
    case PRIMITIVE_BOOLEAN:
    case BOOLEAN:
        preparedState.setBoolean(index, !isNull && Boolean.parseBoolean(param.getValue()));
        break;
    case PRIMITIVE_BYTE:
    case BYTE:
        preparedState.setByte(index, isNull ? 0 : Byte.valueOf(param.getValue()));
        break;
    case JAVA_UTIL_DATE:
    case JAVA_SQL_DATE:
        preparedState.setDate(index, isNull ? null : java.sql.Date.valueOf(param.getValue()));
        break;
    case JAVA_SQL_TIME:
        preparedState.setTime(index, isNull ? null : Time.valueOf(param.getValue()));
        break;
    case JAVA_SQL_TIMESTAMP:
        preparedState.setTimestamp(index, isNull ? null : Timestamp.valueOf(param.getValue()));
        break;
    default:
        preparedState.setObject(index, isNull ? null : param.getValue());
    }
}
 
Example #26
Source File: QueryService.java    From kylin with Apache License 2.0 4 votes vote down vote up
/**
 * @param preparedState
 * @param param
 * @throws SQLException
 */
private void setParam(PreparedStatement preparedState, int index, PrepareSqlRequest.StateParam param)
        throws SQLException {
    boolean isNull = (null == param.getValue());

    Class<?> clazz;
    try {
        clazz = Class.forName(param.getClassName());
    } catch (ClassNotFoundException e) {
        throw new InternalErrorException(e);
    }

    Rep rep = Rep.of(clazz);

    switch (rep) {
    case PRIMITIVE_CHAR:
    case CHARACTER:
    case STRING:
        preparedState.setString(index, isNull ? null : String.valueOf(param.getValue()));
        break;
    case PRIMITIVE_INT:
    case INTEGER:
        preparedState.setInt(index, isNull ? 0 : Integer.parseInt(param.getValue()));
        break;
    case PRIMITIVE_SHORT:
    case SHORT:
        preparedState.setShort(index, isNull ? 0 : Short.parseShort(param.getValue()));
        break;
    case PRIMITIVE_LONG:
    case LONG:
        preparedState.setLong(index, isNull ? 0 : Long.parseLong(param.getValue()));
        break;
    case PRIMITIVE_FLOAT:
    case FLOAT:
        preparedState.setFloat(index, isNull ? 0 : Float.parseFloat(param.getValue()));
        break;
    case PRIMITIVE_DOUBLE:
    case DOUBLE:
        preparedState.setDouble(index, isNull ? 0 : Double.parseDouble(param.getValue()));
        break;
    case PRIMITIVE_BOOLEAN:
    case BOOLEAN:
        preparedState.setBoolean(index, !isNull && Boolean.parseBoolean(param.getValue()));
        break;
    case PRIMITIVE_BYTE:
    case BYTE:
        preparedState.setByte(index, isNull ? 0 : Byte.parseByte(param.getValue()));
        break;
    case JAVA_UTIL_DATE:
    case JAVA_SQL_DATE:
        preparedState.setDate(index, isNull ? null : java.sql.Date.valueOf(param.getValue()));
        break;
    case JAVA_SQL_TIME:
        preparedState.setTime(index, isNull ? null : Time.valueOf(param.getValue()));
        break;
    case JAVA_SQL_TIMESTAMP:
        preparedState.setTimestamp(index, isNull ? null : Timestamp.valueOf(param.getValue()));
        break;
    default:
        preparedState.setObject(index, isNull ? null : param.getValue());
    }
}
 
Example #27
Source File: QueryService.java    From kylin with Apache License 2.0 4 votes vote down vote up
private boolean isPrepareStatementWithParams(SQLRequest sqlRequest) {
    if (sqlRequest instanceof PrepareSqlRequest && ((PrepareSqlRequest) sqlRequest).getParams() != null
            && ((PrepareSqlRequest) sqlRequest).getParams().length > 0)
        return true;
    return false;
}
 
Example #28
Source File: DashboardService.java    From kylin-on-parquet-v2 with Apache License 2.0 4 votes vote down vote up
private PrepareSqlRequest createPrepareSqlRequest(String[] dimensions, String[] metrics, String category,
        Map<String, String> filterMap) {
    PrepareSqlRequest sqlRequest = new PrepareSqlRequest();
    sqlRequest.setProject(MetricsManager.SYSTEM_PROJECT);
    StringBuffer baseSQL = new StringBuffer("select ");
    StringBuffer groupBy = new StringBuffer("");
    if (dimensions != null && dimensions.length > 0) {
        groupBy.append(" group by ");
        StringBuffer dimensionSQL = new StringBuffer("");
        for (String dimension : dimensions) {
            dimensionSQL.append(",");
            dimensionSQL.append(dimension);
        }
        baseSQL.append(dimensionSQL.substring(1));
        groupBy.append(dimensionSQL.substring(1));
    }
    if (metrics != null && metrics.length > 0) {
        StringBuffer metricSQL = new StringBuffer("");
        for (String metric : metrics) {
            metricSQL.append(",");
            metricSQL.append(metric);
        }
        if (groupBy.length() > 0) {
            baseSQL.append(metricSQL);
        } else {
            baseSQL.append(metricSQL.substring(1));
        }
    }
    baseSQL.append(" from ");
    baseSQL.append(category);
    if (filterMap != null && filterMap.size() > 0) {
        PrepareSqlRequest.StateParam[] params = new PrepareSqlRequest.StateParam[filterMap.size()];
        int i = 0;
        StringBuffer filterSQL = new StringBuffer(" where ");
        Iterator<String> it = filterMap.keySet().iterator();
        String filter = it.next();
        filterSQL.append(filter);
        params[i] = new PrepareSqlRequest.StateParam();
        params[i].setClassName("java.lang.String");
        params[i++].setValue(filterMap.get(filter));

        while (it.hasNext()) {
            filter = it.next();
            filterSQL.append(" and ");
            filterSQL.append(filter);
            params[i] = new PrepareSqlRequest.StateParam();
            params[i].setClassName("java.lang.String");
            params[i++].setValue(filterMap.get(filter));
        }
        baseSQL.append(filterSQL.toString());
        sqlRequest.setParams(params);
    }
    baseSQL.append(groupBy);
    sqlRequest.setSql(baseSQL.toString());
    return sqlRequest;
}
 
Example #29
Source File: QueryService.java    From kylin-on-parquet-v2 with Apache License 2.0 4 votes vote down vote up
/**
 * @param preparedState
 * @param param
 * @throws SQLException
 */
private void setParam(PreparedStatement preparedState, int index, PrepareSqlRequest.StateParam param)
        throws SQLException {
    boolean isNull = (null == param.getValue());

    Class<?> clazz;
    try {
        clazz = Class.forName(param.getClassName());
    } catch (ClassNotFoundException e) {
        throw new InternalErrorException(e);
    }

    Rep rep = Rep.of(clazz);

    switch (rep) {
    case PRIMITIVE_CHAR:
    case CHARACTER:
    case STRING:
        preparedState.setString(index, isNull ? null : String.valueOf(param.getValue()));
        break;
    case PRIMITIVE_INT:
    case INTEGER:
        preparedState.setInt(index, isNull ? 0 : Integer.parseInt(param.getValue()));
        break;
    case PRIMITIVE_SHORT:
    case SHORT:
        preparedState.setShort(index, isNull ? 0 : Short.parseShort(param.getValue()));
        break;
    case PRIMITIVE_LONG:
    case LONG:
        preparedState.setLong(index, isNull ? 0 : Long.parseLong(param.getValue()));
        break;
    case PRIMITIVE_FLOAT:
    case FLOAT:
        preparedState.setFloat(index, isNull ? 0 : Float.parseFloat(param.getValue()));
        break;
    case PRIMITIVE_DOUBLE:
    case DOUBLE:
        preparedState.setDouble(index, isNull ? 0 : Double.parseDouble(param.getValue()));
        break;
    case PRIMITIVE_BOOLEAN:
    case BOOLEAN:
        preparedState.setBoolean(index, !isNull && Boolean.parseBoolean(param.getValue()));
        break;
    case PRIMITIVE_BYTE:
    case BYTE:
        preparedState.setByte(index, isNull ? 0 : Byte.parseByte(param.getValue()));
        break;
    case JAVA_UTIL_DATE:
    case JAVA_SQL_DATE:
        preparedState.setDate(index, isNull ? null : java.sql.Date.valueOf(param.getValue()));
        break;
    case JAVA_SQL_TIME:
        preparedState.setTime(index, isNull ? null : Time.valueOf(param.getValue()));
        break;
    case JAVA_SQL_TIMESTAMP:
        preparedState.setTimestamp(index, isNull ? null : Timestamp.valueOf(param.getValue()));
        break;
    default:
        preparedState.setObject(index, isNull ? null : param.getValue());
    }
}
 
Example #30
Source File: QueryService.java    From kylin-on-parquet-v2 with Apache License 2.0 4 votes vote down vote up
private boolean isPrepareStatementWithParams(SQLRequest sqlRequest) {
    if (sqlRequest instanceof PrepareSqlRequest && ((PrepareSqlRequest) sqlRequest).getParams() != null
            && ((PrepareSqlRequest) sqlRequest).getParams().length > 0)
        return true;
    return false;
}