Search in sources :

Example 1 with SqlExplain

use of org.apache.calcite.sql.SqlExplain 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)

Example 2 with SqlExplain

use of org.apache.calcite.sql.SqlExplain in project druid by druid-io.

the class DruidPlanner method plan.

public PlannerResult plan(final String sql) throws SqlParseException, ValidationException, RelConversionException {
    SqlExplain explain = null;
    SqlNode parsed = planner.parse(sql);
    if (parsed.getKind() == SqlKind.EXPLAIN) {
        explain = (SqlExplain) parsed;
        parsed = explain.getExplicandum();
    }
    final SqlNode validated = planner.validate(parsed);
    final RelRoot root = planner.rel(validated);
    try {
        return planWithDruidConvention(explain, root);
    } catch (RelOptPlanner.CannotPlanException e) {
        // Try again with BINDABLE convention. Used for querying Values, metadata tables, and fallback.
        try {
            return planWithBindableConvention(explain, root);
        } catch (Exception e2) {
            e.addSuppressed(e2);
            throw e;
        }
    }
}
Also used : SqlExplain(org.apache.calcite.sql.SqlExplain) RelRoot(org.apache.calcite.rel.RelRoot) RelOptPlanner(org.apache.calcite.plan.RelOptPlanner) ValidationException(org.apache.calcite.tools.ValidationException) RelConversionException(org.apache.calcite.tools.RelConversionException) SqlParseException(org.apache.calcite.sql.parser.SqlParseException) SqlNode(org.apache.calcite.sql.SqlNode)

Example 3 with SqlExplain

use of org.apache.calcite.sql.SqlExplain in project drill by axbaretto.

the class ExplainHandler method rewrite.

@Override
public SqlNode rewrite(SqlNode sqlNode) throws RelConversionException, ForemanSetupException {
    SqlExplain node = unwrap(sqlNode, SqlExplain.class);
    SqlLiteral op = node.operand(2);
    SqlExplain.Depth depth = (SqlExplain.Depth) op.getValue();
    if (node.getDetailLevel() != null) {
        level = node.getDetailLevel();
    }
    switch(depth) {
        case LOGICAL:
            mode = ResultMode.LOGICAL;
            break;
        case PHYSICAL:
            mode = ResultMode.PHYSICAL;
            break;
        default:
            throw new UnsupportedOperationException("Unknown depth " + depth);
    }
    return node.operand(0);
}
Also used : SqlExplain(org.apache.calcite.sql.SqlExplain) SqlLiteral(org.apache.calcite.sql.SqlLiteral)

Example 4 with SqlExplain

use of org.apache.calcite.sql.SqlExplain in project flink by apache.

the class SqlValidatorImpl method getFieldOrigins.

public List<List<String>> getFieldOrigins(SqlNode sqlQuery) {
    if (sqlQuery instanceof SqlExplain) {
        return Collections.emptyList();
    }
    final RelDataType rowType = getValidatedNodeType(sqlQuery);
    final int fieldCount = rowType.getFieldCount();
    if (!sqlQuery.isA(SqlKind.QUERY)) {
        return Collections.nCopies(fieldCount, null);
    }
    final List<List<String>> list = new ArrayList<>();
    for (int i = 0; i < fieldCount; i++) {
        list.add(getFieldOrigin(sqlQuery, i));
    }
    return ImmutableNullableList.copyOf(list);
}
Also used : ArrayList(java.util.ArrayList) SqlExplain(org.apache.calcite.sql.SqlExplain) RelDataType(org.apache.calcite.rel.type.RelDataType) ArrayList(java.util.ArrayList) AbstractList(java.util.AbstractList) ImmutableIntList(org.apache.calcite.util.ImmutableIntList) ImmutableNullableList(org.apache.calcite.util.ImmutableNullableList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) SqlNodeList(org.apache.calcite.sql.SqlNodeList)

Example 5 with SqlExplain

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

the class SqlValidatorImpl method getFieldOrigins.

public List<List<String>> getFieldOrigins(SqlNode sqlQuery) {
    if (sqlQuery instanceof SqlExplain) {
        return Collections.emptyList();
    }
    final RelDataType rowType = getValidatedNodeType(sqlQuery);
    final int fieldCount = rowType.getFieldCount();
    if (!sqlQuery.isA(SqlKind.QUERY)) {
        return Collections.nCopies(fieldCount, null);
    }
    final List<List<String>> list = new ArrayList<>();
    for (int i = 0; i < fieldCount; i++) {
        list.add(getFieldOrigin(sqlQuery, i));
    }
    return ImmutableNullableList.copyOf(list);
}
Also used : ArrayList(java.util.ArrayList) SqlExplain(org.apache.calcite.sql.SqlExplain) RelDataType(org.apache.calcite.rel.type.RelDataType) ArrayList(java.util.ArrayList) AbstractList(java.util.AbstractList) ImmutableNullableList(org.apache.calcite.util.ImmutableNullableList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) SqlNodeList(org.apache.calcite.sql.SqlNodeList)

Aggregations

SqlExplain (org.apache.calcite.sql.SqlExplain)6 RelDataType (org.apache.calcite.rel.type.RelDataType)3 ImmutableList (com.google.common.collect.ImmutableList)2 AbstractList (java.util.AbstractList)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 RelRoot (org.apache.calcite.rel.RelRoot)2 SqlLiteral (org.apache.calcite.sql.SqlLiteral)2 SqlNodeList (org.apache.calcite.sql.SqlNodeList)2 ImmutableNullableList (org.apache.calcite.util.ImmutableNullableList)2 RelOptPlanner (org.apache.calcite.plan.RelOptPlanner)1 SqlExplainFormat (org.apache.calcite.sql.SqlExplainFormat)1 SqlExplainLevel (org.apache.calcite.sql.SqlExplainLevel)1 SqlNode (org.apache.calcite.sql.SqlNode)1 SqlParseException (org.apache.calcite.sql.parser.SqlParseException)1 SqlToRelConverter (org.apache.calcite.sql2rel.SqlToRelConverter)1 RelConversionException (org.apache.calcite.tools.RelConversionException)1 ValidationException (org.apache.calcite.tools.ValidationException)1 ImmutableIntList (org.apache.calcite.util.ImmutableIntList)1