Search in sources :

Example 26 with RelOptPlanner

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.plan.RelOptPlanner in project drill by apache.

the class IndexSelector method getBestIndexNoFilter.

/**
 * we assume all the indexes added in indexPropList are all applicable (and covering).
 * For now this function is used and tested only in IndexScanWithSortOnlyPrule
 */
public IndexProperties getBestIndexNoFilter() {
    if (indexPropList.size() == 0) {
        return null;
    }
    RelOptPlanner planner = indexContext.getCall().getPlanner();
    List<IndexGroup> candidateIndexes = Lists.newArrayList();
    for (IndexProperties p : indexPropList) {
        p.setSatisfiesCollation(buildAndCheckCollation(p));
        IndexGroup index = new IndexGroup();
        index.addIndexProp(p);
        candidateIndexes.add(index);
    }
    Collections.sort(candidateIndexes, new IndexComparator(planner, builder));
    return candidateIndexes.get(0).getIndexProps().get(0);
}
Also used : RelOptPlanner(org.apache.calcite.plan.RelOptPlanner)

Example 27 with RelOptPlanner

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.plan.RelOptPlanner in project herddb by diennea.

the class CalcitePlanner method runPlanner.

private PlannerResult runPlanner(String defaultTableSpace, String query) throws RelConversionException, SqlParseException, ValidationException, MetadataStorageManagerException, StatementExecutionException {
    try {
        SchemaPlus subSchema = getSchemaForTableSpace(defaultTableSpace);
        if (subSchema == null) {
            clearCache();
            throw new StatementExecutionException("tablespace " + defaultTableSpace + " is not available");
        }
        Properties props = new Properties();
        props.put(CalciteConnectionProperty.TIME_ZONE.camelName(), TimeZone.getDefault().getID());
        props.put(CalciteConnectionProperty.LOCALE.camelName(), Locale.ROOT.toString());
        props.put(CalciteConnectionProperty.DEFAULT_NULL_COLLATION.camelName(), NullCollation.LAST.toString());
        final CalciteConnectionConfigImpl calciteRuntimeContextConfig = new CalciteConnectionConfigImpl(props);
        final FrameworkConfig config = Frameworks.newConfigBuilder().parserConfig(SQL_PARSER_CONFIG).defaultSchema(subSchema).traitDefs(TRAITS).context(new Context() {

            @Override
            public <C> C unwrap(Class<C> aClass) {
                if (aClass == CalciteConnectionConfigImpl.class || aClass == CalciteConnectionConfig.class) {
                    return (C) calciteRuntimeContextConfig;
                }
                return null;
            }
        }).programs(Programs.ofRules(Programs.RULE_SET)).build();
        Planner planner = new PlannerImpl(config);
        if (LOG.isLoggable(Level.FINER)) {
            LOG.log(Level.FINER, "Query: {0}", query);
        }
        try {
            SqlNode n = planner.parse(query);
            n = planner.validate(n);
            RelNode logicalPlan = planner.rel(n).project();
            if (LOG.isLoggable(DUMP_QUERY_LEVEL)) {
                LOG.log(DUMP_QUERY_LEVEL, "Query: {0} {1}", new Object[] { query, RelOptUtil.dumpPlan("-- Logical Plan", logicalPlan, SqlExplainFormat.TEXT, SqlExplainLevel.ALL_ATTRIBUTES) });
            }
            RelDataType originalRowType = logicalPlan.getRowType();
            RelOptCluster cluster = logicalPlan.getCluster();
            final RelOptPlanner optPlanner = cluster.getPlanner();
            optPlanner.addRule(CoreRules.FILTER_REDUCE_EXPRESSIONS);
            RelTraitSet desiredTraits = cluster.traitSet().replace(EnumerableConvention.INSTANCE);
            final RelCollation collation = logicalPlan instanceof Sort ? ((Sort) logicalPlan).collation : null;
            if (collation != null) {
                desiredTraits = desiredTraits.replace(collation);
            }
            final RelNode newRoot = optPlanner.changeTraits(logicalPlan, desiredTraits);
            optPlanner.setRoot(newRoot);
            RelNode bestExp = optPlanner.findBestExp();
            if (LOG.isLoggable(DUMP_QUERY_LEVEL)) {
                LOG.log(DUMP_QUERY_LEVEL, "Query: {0} {1}", new Object[] { query, RelOptUtil.dumpPlan("-- Best  Plan", bestExp, SqlExplainFormat.TEXT, SqlExplainLevel.ALL_ATTRIBUTES) });
            }
            return new PlannerResult(bestExp, originalRowType, logicalPlan, n);
        } catch (AssertionError err) {
            throw new StatementExecutionException("Internal Calcite error " + err, err);
        }
    } catch (java.lang.LinkageError err) {
        LOG.log(Level.SEVERE, "Error on Java Classpath", err);
        throw new StatementExecutionException("Internal Calcite error " + err, err);
    }
}
Also used : InitializerContext(org.apache.calcite.sql2rel.InitializerContext) Context(org.apache.calcite.plan.Context) DataContext(org.apache.calcite.DataContext) RelOptCluster(org.apache.calcite.plan.RelOptCluster) PlannerImpl(org.apache.calcite.prepare.PlannerImpl) CalciteConnectionConfigImpl(org.apache.calcite.config.CalciteConnectionConfigImpl) SchemaPlus(org.apache.calcite.schema.SchemaPlus) RelDataType(org.apache.calcite.rel.type.RelDataType) RelTraitSet(org.apache.calcite.plan.RelTraitSet) Properties(java.util.Properties) RelOptPlanner(org.apache.calcite.plan.RelOptPlanner) StatementExecutionException(herddb.model.StatementExecutionException) RelCollation(org.apache.calcite.rel.RelCollation) RelNode(org.apache.calcite.rel.RelNode) EnumerableSort(org.apache.calcite.adapter.enumerable.EnumerableSort) Sort(org.apache.calcite.rel.core.Sort) Planner(org.apache.calcite.tools.Planner) RelOptPlanner(org.apache.calcite.plan.RelOptPlanner) FrameworkConfig(org.apache.calcite.tools.FrameworkConfig) SqlNode(org.apache.calcite.sql.SqlNode)

Aggregations

RelOptPlanner (org.apache.calcite.plan.RelOptPlanner)26 RelNode (org.apache.calcite.rel.RelNode)16 RelOptCluster (org.apache.calcite.plan.RelOptCluster)9 RelTraitSet (org.apache.calcite.plan.RelTraitSet)9 RexBuilder (org.apache.calcite.rex.RexBuilder)7 RelOptMaterialization (org.apache.calcite.plan.RelOptMaterialization)5 RelDataType (org.apache.calcite.rel.type.RelDataType)5 ImmutableList (com.google.common.collect.ImmutableList)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4 RelOptRule (org.apache.calcite.plan.RelOptRule)4 HepPlanner (org.apache.calcite.plan.hep.HepPlanner)4 VolcanoPlanner (org.apache.calcite.plan.volcano.VolcanoPlanner)4 JaninoRelMetadataProvider (org.apache.calcite.rel.metadata.JaninoRelMetadataProvider)4 Properties (java.util.Properties)3 DataContext (org.apache.calcite.DataContext)3 JavaTypeFactory (org.apache.calcite.adapter.java.JavaTypeFactory)3 JavaTypeFactoryImpl (org.apache.calcite.jdbc.JavaTypeFactoryImpl)3 HepProgram (org.apache.calcite.plan.hep.HepProgram)3 HepProgramBuilder (org.apache.calcite.plan.hep.HepProgramBuilder)3