Java Code Examples for org.apache.calcite.plan.RelOptPlanner#addRule()

The following examples show how to use org.apache.calcite.plan.RelOptPlanner#addRule() . 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: Programs.java    From calcite with Apache License 2.0 6 votes vote down vote up
public RelNode run(RelOptPlanner planner, RelNode rel,
    RelTraitSet requiredOutputTraits,
    List<RelOptMaterialization> materializations,
    List<RelOptLattice> lattices) {
  planner.clear();
  for (RelOptRule rule : ruleSet) {
    planner.addRule(rule);
  }
  for (RelOptMaterialization materialization : materializations) {
    planner.addMaterialization(materialization);
  }
  for (RelOptLattice lattice : lattices) {
    planner.addLattice(lattice);
  }
  if (!rel.getTraitSet().equals(requiredOutputTraits)) {
    rel = planner.changeTraits(rel, requiredOutputTraits);
  }
  planner.setRoot(rel);
  return planner.findBestExp();

}
 
Example 2
Source File: TraitPropagationTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
private static RelNode run(PropAction action, RuleSet rules)
    throws Exception {

  FrameworkConfig config = Frameworks.newConfigBuilder()
      .ruleSets(rules).build();

  final Properties info = new Properties();
  final Connection connection = DriverManager
      .getConnection("jdbc:calcite:", info);
  final CalciteServerStatement statement = connection
      .createStatement().unwrap(CalciteServerStatement.class);
  final CalcitePrepare.Context prepareContext =
        statement.createPrepareContext();
  final JavaTypeFactory typeFactory = prepareContext.getTypeFactory();
  CalciteCatalogReader catalogReader =
        new CalciteCatalogReader(prepareContext.getRootSchema(),
            prepareContext.getDefaultSchemaPath(),
            typeFactory,
            prepareContext.config());
  final RexBuilder rexBuilder = new RexBuilder(typeFactory);
  final RelOptPlanner planner = new VolcanoPlanner(config.getCostFactory(),
      config.getContext());

  // set up rules before we generate cluster
  planner.clearRelTraitDefs();
  planner.addRelTraitDef(RelCollationTraitDef.INSTANCE);
  planner.addRelTraitDef(ConventionTraitDef.INSTANCE);

  planner.clear();
  for (RelOptRule r : rules) {
    planner.addRule(r);
  }

  final RelOptCluster cluster = RelOptCluster.create(planner, rexBuilder);
  return action.apply(cluster, catalogReader,
      prepareContext.getRootSchema().plus());
}
 
Example 3
Source File: JdbcConvention.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public void register(RelOptPlanner planner) {
  for (RelOptRule rule : JdbcRules.rules(this)) {
    planner.addRule(rule);
  }
  planner.addRule(FilterSetOpTransposeRule.INSTANCE);
  planner.addRule(ProjectRemoveRule.INSTANCE);
}
 
Example 4
Source File: PigTableScan.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public void register(RelOptPlanner planner) {
  planner.addRule(PigToEnumerableConverterRule.INSTANCE);
  for (RelOptRule rule : PigRules.ALL_PIG_OPT_RULES) {
    planner.addRule(rule);
  }
  // Don't move Aggregates around, otherwise PigAggregate.implement() won't
  // know how to correctly procuce Pig Latin
  planner.removeRule(AggregateExpandDistinctAggregatesRule.INSTANCE);
  // Make sure planner picks PigJoin over EnumerableHashJoin. Should there be
  // a rule for this instead for removing ENUMERABLE_JOIN_RULE here?
  planner.removeRule(EnumerableRules.ENUMERABLE_JOIN_RULE);
}
 
Example 5
Source File: ToLogicalConverterTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
private static RelNode toPhysical(RelNode rel) {
  final RelOptPlanner planner = rel.getCluster().getPlanner();
  planner.clear();
  for (RelOptRule rule : RULE_SET) {
    planner.addRule(rule);
  }

  final Program program = Programs.of(RuleSets.ofList(planner.getRules()));
  return program.run(planner, rel, rel.getTraitSet().replace(EnumerableConvention.INSTANCE),
      ImmutableList.of(), ImmutableList.of());
}
 
Example 6
Source File: ElasticsearchTableScan.java    From dk-fitting with Apache License 2.0 5 votes vote down vote up
@Override
    public void register(RelOptPlanner planner) {
        planner.addRule(ElasticsearchToEnumerableConverterRule.INSTANCE);
        for (RelOptRule rule: ElasticsearchRules.RULES) {
            planner.addRule(rule);
        }

//        planner.removeRule(AggregateExpandDistinctAggregatesRule.INSTANCE);
//        planner.removeRule(EnumerableRules.ENUMERABLE_JOIN_RULE);
    }
 
Example 7
Source File: CalcitePlanner.java    From herddb with Apache License 2.0 4 votes vote down vote up
private PlannerResult runPlanner(String defaultTableSpace, String query) throws RelConversionException,
        SqlParseException, ValidationException, MetadataStorageManagerException, StatementExecutionException {
    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());
    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;
                }
            })
            // define the rules you want to apply
            .programs(Programs.ofRules(Programs.RULE_SET))
            .build();
    Planner planner = Frameworks.getPlanner(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(ReduceExpressionsRule.FILTER_INSTANCE);
        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);
    }
}
 
Example 8
Source File: QuarkTableScan.java    From quark with Apache License 2.0 4 votes vote down vote up
@Override
public void register(RelOptPlanner planner) {
  planner.addRule(ProjectRule.INSTANCE);
}
 
Example 9
Source File: QuarkTileScan.java    From quark with Apache License 2.0 4 votes vote down vote up
@Override
public void register(RelOptPlanner planner) {
  planner.addRule(ProjectRule.INSTANCE);
}
 
Example 10
Source File: ProvenanceTableScan.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Override
public void register(RelOptPlanner planner) {
    planner.addRule(ProvenanceProjectTableScanRule.INSTANCE);
}
 
Example 11
Source File: ConnectionStatusTableScan.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Override
public void register(RelOptPlanner planner) {
    planner.addRule(ConnectionStatusProjectTableScanRule.INSTANCE);
}
 
Example 12
Source File: CsvTableScan.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Override public void register(RelOptPlanner planner) {
  planner.addRule(CsvProjectTableScanRule.INSTANCE);
}
 
Example 13
Source File: SplunkTableScan.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Override public void register(RelOptPlanner planner) {
  planner.addRule(SplunkPushDownRule.FILTER);
  planner.addRule(SplunkPushDownRule.FILTER_ON_PROJECT);
  planner.addRule(SplunkPushDownRule.PROJECT);
  planner.addRule(SplunkPushDownRule.PROJECT_ON_FILTER);
}
 
Example 14
Source File: JvmMetricsTableScan.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Override
public void register(RelOptPlanner planner) {
    planner.addRule(JvmMetricsProjectTableScanRule.INSTANCE);
}
 
Example 15
Source File: GeodeTableScan.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Override public void register(RelOptPlanner planner) {
  planner.addRule(GeodeToEnumerableConverterRule.INSTANCE);
  for (RelOptRule rule : GeodeRules.RULES) {
    planner.addRule(rule);
  }
}
 
Example 16
Source File: CassandraTableScan.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Override public void register(RelOptPlanner planner) {
  planner.addRule(CassandraToEnumerableConverterRule.INSTANCE);
  for (RelOptRule rule : CassandraRules.RULES) {
    planner.addRule(rule);
  }
}
 
Example 17
Source File: ProcessorStatusTableScan.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Override
public void register(RelOptPlanner planner) {
    planner.addRule(ProcessorStatusProjectTableScanRule.INSTANCE);
}
 
Example 18
Source File: FlowFileTableScan.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Override
public void register(RelOptPlanner planner) {
    planner.addRule(FlowFileProjectTableScanRule.INSTANCE);
}
 
Example 19
Source File: MongoTableScan.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Override public void register(RelOptPlanner planner) {
  planner.addRule(MongoToEnumerableConverterRule.INSTANCE);
  for (RelOptRule rule : MongoRules.RULES) {
    planner.addRule(rule);
  }
}
 
Example 20
Source File: OLAPTableScan.java    From kylin-on-parquet-v2 with Apache License 2.0 4 votes vote down vote up
@Override
public void register(RelOptPlanner planner) {
    // force clear the query context before traversal relational operators
    OLAPContext.clearThreadLocalContexts();

    // register OLAP rules
    addRules(planner, kylinConfig.getCalciteAddRule());

    planner.addRule(OLAPToEnumerableConverterRule.INSTANCE);
    planner.addRule(OLAPFilterRule.INSTANCE);
    planner.addRule(OLAPProjectRule.INSTANCE);
    planner.addRule(OLAPAggregateRule.INSTANCE);
    planner.addRule(OLAPJoinRule.INSTANCE);
    planner.addRule(OLAPLimitRule.INSTANCE);
    planner.addRule(OLAPSortRule.INSTANCE);
    planner.addRule(OLAPUnionRule.INSTANCE);
    planner.addRule(OLAPWindowRule.INSTANCE);
    planner.addRule(OLAPValuesRule.INSTANCE);

    planner.addRule(AggregateProjectReduceRule.INSTANCE);

    // CalcitePrepareImpl.CONSTANT_REDUCTION_RULES
    if (kylinConfig.isReduceExpressionsRulesEnabled()) {
        planner.addRule(ReduceExpressionsRule.PROJECT_INSTANCE);
        planner.addRule(ReduceExpressionsRule.FILTER_INSTANCE);
        planner.addRule(ReduceExpressionsRule.CALC_INSTANCE);
        planner.addRule(ReduceExpressionsRule.JOIN_INSTANCE);
    }
    // the ValuesReduceRule breaks query test somehow...
    //        planner.addRule(ValuesReduceRule.FILTER_INSTANCE);
    //        planner.addRule(ValuesReduceRule.PROJECT_FILTER_INSTANCE);
    //        planner.addRule(ValuesReduceRule.PROJECT_INSTANCE);

    removeRules(planner, kylinConfig.getCalciteRemoveRule());
    if (!kylinConfig.isEnumerableRulesEnabled()) {
        for (RelOptRule rule : CalcitePrepareImpl.ENUMERABLE_RULES) {
            planner.removeRule(rule);
        }
    }
    // since join is the entry point, we can't push filter past join
    planner.removeRule(FilterJoinRule.FILTER_ON_JOIN);
    planner.removeRule(FilterJoinRule.JOIN);

    // since we don't have statistic of table, the optimization of join is too cost
    planner.removeRule(JoinCommuteRule.INSTANCE);
    planner.removeRule(JoinPushThroughJoinRule.LEFT);
    planner.removeRule(JoinPushThroughJoinRule.RIGHT);

    // keep tree structure like filter -> aggregation -> project -> join/table scan, implementOLAP() rely on this tree pattern
    planner.removeRule(AggregateJoinTransposeRule.INSTANCE);
    planner.removeRule(AggregateProjectMergeRule.INSTANCE);
    planner.removeRule(FilterProjectTransposeRule.INSTANCE);
    planner.removeRule(SortJoinTransposeRule.INSTANCE);
    planner.removeRule(JoinPushExpressionsRule.INSTANCE);
    planner.removeRule(SortUnionTransposeRule.INSTANCE);
    planner.removeRule(JoinUnionTransposeRule.LEFT_UNION);
    planner.removeRule(JoinUnionTransposeRule.RIGHT_UNION);
    planner.removeRule(AggregateUnionTransposeRule.INSTANCE);
    planner.removeRule(DateRangeRules.FILTER_INSTANCE);
    planner.removeRule(SemiJoinRule.JOIN);
    planner.removeRule(SemiJoinRule.PROJECT);
    // distinct count will be split into a separated query that is joined with the left query
    planner.removeRule(AggregateExpandDistinctAggregatesRule.INSTANCE);

    // see Dec 26th email @ http://mail-archives.apache.org/mod_mbox/calcite-dev/201412.mbox/browser
    planner.removeRule(ExpandConversionRule.INSTANCE);
}