Search in sources :

Example 71 with RelRoot

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelRoot in project druid by apache.

the class DruidPlanner method plan.

/**
 * Plan an SQL query for execution, returning a {@link PlannerResult} which can be used to actually execute the query.
 *
 * Ideally, the query can be planned into a native Druid query, using {@link #planWithDruidConvention}, but will
 * fall-back to {@link #planWithBindableConvention} if this is not possible.
 *
 * In some future this could perhaps re-use some of the work done by {@link #validate()}
 * instead of repeating it, but that day is not today.
 */
public PlannerResult plan() throws SqlParseException, ValidationException, RelConversionException {
    resetPlanner();
    final ParsedNodes parsed = ParsedNodes.create(planner.parse(plannerContext.getSql()));
    try {
        if (parsed.getIngestionGranularity() != null) {
            plannerContext.getQueryContext().put(DruidSqlInsert.SQL_INSERT_SEGMENT_GRANULARITY, plannerContext.getJsonMapper().writeValueAsString(parsed.getIngestionGranularity()));
        }
    } catch (JsonProcessingException e) {
        throw new ValidationException("Unable to serialize partition granularity.");
    }
    // the planner's type factory is not available until after parsing
    this.rexBuilder = new RexBuilder(planner.getTypeFactory());
    final SqlNode parameterizedQueryNode = rewriteDynamicParameters(parsed.getQueryNode());
    final SqlNode validatedQueryNode = planner.validate(parameterizedQueryNode);
    final RelRoot rootQueryRel = planner.rel(validatedQueryNode);
    try {
        return planWithDruidConvention(rootQueryRel, parsed.getExplainNode(), parsed.getInsertNode());
    } catch (Exception e) {
        Throwable cannotPlanException = Throwables.getCauseOfType(e, RelOptPlanner.CannotPlanException.class);
        if (null == cannotPlanException) {
            // Not a CannotPlanningException, rethrow without trying with bindable
            throw e;
        }
        // any error, if it is plannable by the bindable convention
        if (parsed.getInsertNode() == null) {
            // Try again with BINDABLE convention. Used for querying Values and metadata tables.
            try {
                return planWithBindableConvention(rootQueryRel, parsed.getExplainNode());
            } catch (Exception e2) {
                e.addSuppressed(e2);
            }
        }
        Logger logger = log;
        if (!QueryContexts.isDebug(plannerContext.getQueryContext())) {
            logger = log.noStackTrace();
        }
        String errorMessage = buildSQLPlanningErrorMessage(cannotPlanException);
        logger.warn(e, errorMessage);
        throw new UnsupportedSQLQueryException(errorMessage);
    }
}
Also used : ValidationException(org.apache.calcite.tools.ValidationException) RexBuilder(org.apache.calcite.rex.RexBuilder) RelRoot(org.apache.calcite.rel.RelRoot) Logger(org.apache.druid.java.util.common.logger.Logger) EmittingLogger(org.apache.druid.java.util.emitter.EmittingLogger) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ValidationException(org.apache.calcite.tools.ValidationException) SqlParseException(org.apache.calcite.sql.parser.SqlParseException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) RelConversionException(org.apache.calcite.tools.RelConversionException) SqlNode(org.apache.calcite.sql.SqlNode)

Example 72 with RelRoot

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelRoot in project Mycat2 by MyCATApache.

the class DrdsSqlCompiler method getRelRoot.

public RelNodeContext getRelRoot(SchemaPlus plus, DrdsSql drdsSql) {
    CalciteCatalogReader catalogReader = DrdsRunnerHelper.newCalciteCatalogReader(plus);
    SqlValidator validator = DrdsRunnerHelper.getSqlValidator(drdsSql, catalogReader);
    RelOptCluster cluster = newCluster();
    SqlToRelConverter sqlToRelConverter = new SqlToRelConverter(new ViewExpander(), validator, catalogReader, cluster, MycatCalciteSupport.config.getConvertletTable(), MycatCalciteSupport.sqlToRelConverterConfig);
    MycatCalciteMySqlNodeVisitor mycatCalciteMySqlNodeVisitor = new MycatCalciteMySqlNodeVisitor();
    RelBuilder relBuilder = MycatCalciteSupport.relBuilderFactory.create(sqlToRelConverter.getCluster(), catalogReader);
    SQLStatement sqlStatement = null;
    SqlNode sqlNode = null;
    SqlNode validated = null;
    RelDataType parameterRowType = null;
    RelRoot root = null;
    RelNode decorRelNode = null;
    try {
        sqlStatement = drdsSql.getParameterizedStatement();
        mycatCalciteMySqlNodeVisitor = new MycatCalciteMySqlNodeVisitor();
        sqlStatement.accept(mycatCalciteMySqlNodeVisitor);
        sqlNode = mycatCalciteMySqlNodeVisitor.getSqlNode();
        validated = validator.validate(sqlNode);
        parameterRowType = validator.getParameterRowType(sqlNode);
        root = sqlToRelConverter.convertQuery(validated, false, true);
        decorRelNode = RelDecorrelator.decorrelateQuery(root.rel, relBuilder);
        return new RelNodeContext(root.withRel(decorRelNode), sqlToRelConverter, validator, relBuilder, catalogReader, parameterRowType);
    } catch (Throwable throwable) {
        log.error("sqlStatement:{} sqlNode:{} validated:{} root:{} newRelNode:{}", sqlStatement, sqlNode, validated, root, decorRelNode);
        throw throwable;
    }
}
Also used : SqlToRelConverter(org.apache.calcite.sql2rel.SqlToRelConverter) RelBuilder(org.apache.calcite.tools.RelBuilder) RelRoot(org.apache.calcite.rel.RelRoot) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) CalciteCatalogReader(org.apache.calcite.prepare.CalciteCatalogReader) RelNode(org.apache.calcite.rel.RelNode) SqlValidator(org.apache.calcite.sql.validate.SqlValidator) SqlNode(org.apache.calcite.sql.SqlNode)

Example 73 with RelRoot

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelRoot in project ignite-3 by apache.

the class IgnitePlanner method expandView.

/**
 * {@inheritDoc}
 */
@Override
public RelRoot expandView(RelDataType rowType, String qryStr, List<String> schemaPath, List<String> viewPath) {
    SqlParser parser = SqlParser.create(qryStr, parserCfg);
    SqlNode sqlNode;
    try {
        sqlNode = parser.parseQuery();
    } catch (SqlParseException e) {
        // throw new IgniteSQLException("parse failed", IgniteQueryErrorCode.PARSING, e);
        throw new IgniteException("parse failed", e);
    }
    CalciteCatalogReader catalogReader = this.catalogReader.withSchemaPath(schemaPath);
    SqlValidator validator = new IgniteSqlValidator(operatorTbl, catalogReader, typeFactory, validatorCfg, ctx.parameters());
    SqlToRelConverter sqlToRelConverter = sqlToRelConverter(validator, catalogReader, sqlToRelConverterCfg);
    RelRoot root = sqlToRelConverter.convertQuery(sqlNode, true, false);
    root = root.withRel(sqlToRelConverter.decorrelate(sqlNode, root.rel));
    return trimUnusedFields(root);
}
Also used : SqlToRelConverter(org.apache.calcite.sql2rel.SqlToRelConverter) SqlParseException(org.apache.calcite.sql.parser.SqlParseException) CalciteCatalogReader(org.apache.calcite.prepare.CalciteCatalogReader) IgniteException(org.apache.ignite.lang.IgniteException) SqlValidator(org.apache.calcite.sql.validate.SqlValidator) SqlParser(org.apache.calcite.sql.parser.SqlParser) RelRoot(org.apache.calcite.rel.RelRoot) SqlNode(org.apache.calcite.sql.SqlNode)

Example 74 with RelRoot

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelRoot in project coral by linkedin.

the class TestUtils method toRel.

static RelNode toRel(String sql, FrameworkConfig config) {
    Planner planner = Frameworks.getPlanner(config);
    try {
        SqlNode sn = planner.parse(sql);
        SqlNode validate = planner.validate(sn);
        RelRoot rel = planner.rel(validate);
        // RelNode relNode = rel.project();
        return rel.project();
    // return Calcite2TrinoUDFConverter.convertRel(relNode);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : Planner(org.apache.calcite.tools.Planner) RelRoot(org.apache.calcite.rel.RelRoot) MetaException(org.apache.hadoop.hive.metastore.api.MetaException) CommandNeedRetryException(org.apache.hadoop.hive.ql.CommandNeedRetryException) IOException(java.io.IOException) HiveException(org.apache.hadoop.hive.ql.metadata.HiveException) SqlNode(org.apache.calcite.sql.SqlNode)

Example 75 with RelRoot

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelRoot in project dingo by dingodb.

the class TestLogicalPlan method testSortLimit.

@Test
public void testSortLimit() throws SqlParseException {
    String sql = "select * from test order by name limit 3";
    RelRoot relRoot = parse(sql);
    Assert.relNode(relRoot.rel).isA(LogicalSort.class).convention(Convention.NONE).singleInput().isA(LogicalProject.class).convention(Convention.NONE).singleInput().isA(DingoTableScan.class).convention(DingoConventions.DINGO);
}
Also used : DingoTableScan(io.dingodb.calcite.rel.DingoTableScan) RelRoot(org.apache.calcite.rel.RelRoot) NlsString(org.apache.calcite.util.NlsString) LogicalSort(org.apache.calcite.rel.logical.LogicalSort) Test(org.junit.jupiter.api.Test)

Aggregations

RelRoot (org.apache.calcite.rel.RelRoot)77 SqlNode (org.apache.calcite.sql.SqlNode)30 RelNode (org.apache.calcite.rel.RelNode)22 Test (org.junit.jupiter.api.Test)18 NlsString (org.apache.calcite.util.NlsString)17 DingoTableScan (io.dingodb.calcite.rel.DingoTableScan)13 SqlParseException (org.apache.calcite.sql.parser.SqlParseException)11 SqlToRelConverter (org.apache.calcite.sql2rel.SqlToRelConverter)11 LogicalProject (org.apache.calcite.rel.logical.LogicalProject)9 SamzaSqlDslConverterFactory (org.apache.samza.sql.dsl.SamzaSqlDslConverterFactory)8 RelOptPlanner (org.apache.calcite.plan.RelOptPlanner)7 RelDataType (org.apache.calcite.rel.type.RelDataType)7 RexBuilder (org.apache.calcite.rex.RexBuilder)7 SqlValidator (org.apache.calcite.sql.validate.SqlValidator)7 DslConverter (org.apache.samza.sql.interfaces.DslConverter)7 Test (org.junit.Test)7 RelOptCluster (org.apache.calcite.plan.RelOptCluster)6 SqlParser (org.apache.calcite.sql.parser.SqlParser)6 Planner (org.apache.calcite.tools.Planner)6 LogicalFilter (org.apache.calcite.rel.logical.LogicalFilter)5