Search in sources :

Example 1 with SqlExplainLevel

use of org.apache.calcite.sql.SqlExplainLevel in project calcite by apache.

the class Prepare method prepareSql.

public PreparedResult prepareSql(SqlNode sqlQuery, SqlNode sqlNodeOriginal, Class runtimeContextClass, SqlValidator validator, boolean needsValidation) {
    init(runtimeContextClass);
    final SqlToRelConverter.ConfigBuilder builder = SqlToRelConverter.configBuilder().withTrimUnusedFields(true).withExpand(THREAD_EXPAND.get()).withExplain(sqlQuery.getKind() == SqlKind.EXPLAIN);
    final SqlToRelConverter sqlToRelConverter = getSqlToRelConverter(validator, catalogReader, builder.build());
    SqlExplain sqlExplain = null;
    if (sqlQuery.getKind() == SqlKind.EXPLAIN) {
        // dig out the underlying SQL statement
        sqlExplain = (SqlExplain) sqlQuery;
        sqlQuery = sqlExplain.getExplicandum();
        sqlToRelConverter.setDynamicParamCountInExplain(sqlExplain.getDynamicParamCount());
    }
    RelRoot root = sqlToRelConverter.convertQuery(sqlQuery, needsValidation, true);
    Hook.CONVERTED.run(root.rel);
    if (timingTracer != null) {
        timingTracer.traceTime("end sql2rel");
    }
    final RelDataType resultType = validator.getValidatedNodeType(sqlQuery);
    fieldOrigins = validator.getFieldOrigins(sqlQuery);
    assert fieldOrigins.size() == resultType.getFieldCount();
    parameterRowType = validator.getParameterRowType(sqlQuery);
    // storage and decorrelation
    if (sqlExplain != null) {
        SqlExplain.Depth explainDepth = sqlExplain.getDepth();
        SqlExplainFormat format = sqlExplain.getFormat();
        SqlExplainLevel detailLevel = sqlExplain.getDetailLevel();
        switch(explainDepth) {
            case TYPE:
                return createPreparedExplanation(resultType, parameterRowType, null, format, detailLevel);
            case LOGICAL:
                return createPreparedExplanation(null, parameterRowType, root, format, detailLevel);
            default:
        }
    }
    // Structured type flattening, view expansion, and plugging in physical
    // storage.
    root = root.withRel(flattenTypes(root.rel, true));
    if (this.context.config().forceDecorrelate()) {
        // Sub-query decorrelation.
        root = root.withRel(decorrelate(sqlToRelConverter, sqlQuery, root.rel));
    }
    // Trim unused fields.
    root = trimUnusedFields(root);
    Hook.TRIMMED.run(root.rel);
    // Display physical plan after decorrelation.
    if (sqlExplain != null) {
        switch(sqlExplain.getDepth()) {
            case PHYSICAL:
            default:
                root = optimize(root, getMaterializations(), getLattices());
                return createPreparedExplanation(null, parameterRowType, root, sqlExplain.getFormat(), sqlExplain.getDetailLevel());
        }
    }
    root = optimize(root, getMaterializations(), getLattices());
    if (timingTracer != null) {
        timingTracer.traceTime("end optimization");
    }
    // use original kind.
    if (!root.kind.belongsTo(SqlKind.DML)) {
        root = root.withKind(sqlNodeOriginal.getKind());
    }
    return implement(root);
}
Also used : SqlExplainFormat(org.apache.calcite.sql.SqlExplainFormat) SqlExplainLevel(org.apache.calcite.sql.SqlExplainLevel) SqlToRelConverter(org.apache.calcite.sql2rel.SqlToRelConverter) SqlExplain(org.apache.calcite.sql.SqlExplain) RelRoot(org.apache.calcite.rel.RelRoot) RelDataType(org.apache.calcite.rel.type.RelDataType)

Aggregations

RelRoot (org.apache.calcite.rel.RelRoot)1 RelDataType (org.apache.calcite.rel.type.RelDataType)1 SqlExplain (org.apache.calcite.sql.SqlExplain)1 SqlExplainFormat (org.apache.calcite.sql.SqlExplainFormat)1 SqlExplainLevel (org.apache.calcite.sql.SqlExplainLevel)1 SqlToRelConverter (org.apache.calcite.sql2rel.SqlToRelConverter)1