Search in sources :

Example 11 with RelOptRule

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.plan.RelOptRule in project druid by druid-io.

the class Rules method baseRuleSet.

private static List<RelOptRule> baseRuleSet(final QueryMaker queryMaker, final DruidOperatorTable operatorTable) {
    final PlannerConfig plannerConfig = queryMaker.getPlannerContext().getPlannerConfig();
    final ImmutableList.Builder<RelOptRule> rules = ImmutableList.builder();
    // Calcite rules.
    rules.addAll(DEFAULT_RULES);
    rules.addAll(MISCELLANEOUS_RULES);
    rules.addAll(CONSTANT_REDUCTION_RULES);
    rules.addAll(VOLCANO_ABSTRACT_RULES);
    rules.addAll(RELOPTUTIL_ABSTRACT_RULES);
    if (!plannerConfig.isUseApproximateCountDistinct()) {
        // We'll need this to expand COUNT DISTINCTs.
        // Avoid AggregateExpandDistinctAggregatesRule.INSTANCE; it uses grouping sets and we don't support those.
        rules.add(AggregateExpandDistinctAggregatesRule.JOIN);
    }
    if (plannerConfig.isUseFallback()) {
        rules.add(DruidRelToBindableRule.instance());
    }
    // Druid-specific rules.
    rules.add(new DruidTableScanRule(queryMaker));
    rules.add(new DruidFilterRule(operatorTable));
    if (plannerConfig.getMaxSemiJoinRowsInMemory() > 0) {
        rules.add(DruidSemiJoinRule.instance());
    }
    rules.addAll(SelectRules.rules(operatorTable));
    rules.addAll(GroupByRules.rules(operatorTable));
    return rules.build();
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) DruidTableScanRule(io.druid.sql.calcite.rule.DruidTableScanRule) DruidFilterRule(io.druid.sql.calcite.rule.DruidFilterRule) RelOptRule(org.apache.calcite.plan.RelOptRule)

Example 12 with RelOptRule

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.plan.RelOptRule in project samza by apache.

the class QueryPlanner method getPlanner.

private Planner getPlanner() {
    Planner planner = null;
    try {
        Connection connection = DriverManager.getConnection("jdbc:calcite:");
        CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
        SchemaPlus rootSchema = calciteConnection.getRootSchema();
        registerSourceSchemas(rootSchema);
        List<SamzaSqlScalarFunctionImpl> samzaSqlFunctions = udfMetadata.stream().map(SamzaSqlScalarFunctionImpl::new).collect(Collectors.toList());
        final List<RelTraitDef> traitDefs = new ArrayList<>();
        traitDefs.add(ConventionTraitDef.INSTANCE);
        traitDefs.add(RelCollationTraitDef.INSTANCE);
        List<SqlOperatorTable> sqlOperatorTables = new ArrayList<>();
        sqlOperatorTables.add(new SamzaSqlOperatorTable());
        sqlOperatorTables.add(new SamzaSqlUdfOperatorTable(samzaSqlFunctions));
        // TODO: Introduce a pluggable rule factory.
        List<RelOptRule> rules = ImmutableList.of(FilterProjectTransposeRule.INSTANCE, ProjectMergeRule.INSTANCE, new SamzaSqlFilterRemoteJoinRule.SamzaSqlFilterIntoRemoteJoinRule(true, RelFactories.LOGICAL_BUILDER, systemStreamConfigBySource));
        // Using lenient so that !=,%,- are allowed.
        FrameworkConfig frameworkConfig = Frameworks.newConfigBuilder().parserConfig(SqlParser.configBuilder().setLex(Lex.JAVA).setConformance(SqlConformanceEnum.LENIENT).setCaseSensitive(// Make Udfs case insensitive
        false).build()).defaultSchema(rootSchema).operatorTable(new ChainedSqlOperatorTable(sqlOperatorTables)).sqlToRelConverterConfig(SqlToRelConverter.Config.DEFAULT).traitDefs(traitDefs).programs(Programs.hep(rules, true, DefaultRelMetadataProvider.INSTANCE)).build();
        planner = Frameworks.getPlanner(frameworkConfig);
        return planner;
    } catch (Exception e) {
        String errorMsg = "Failed to create planner.";
        LOG.error(errorMsg, e);
        if (planner != null) {
            planner.close();
        }
        throw new SamzaException(errorMsg, e);
    }
}
Also used : Connection(java.sql.Connection) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) SchemaPlus(org.apache.calcite.schema.SchemaPlus) ArrayList(java.util.ArrayList) SamzaException(org.apache.samza.SamzaException) SamzaException(org.apache.samza.SamzaException) RelOptRule(org.apache.calcite.plan.RelOptRule) ChainedSqlOperatorTable(org.apache.calcite.sql.util.ChainedSqlOperatorTable) RelTraitDef(org.apache.calcite.plan.RelTraitDef) ChainedSqlOperatorTable(org.apache.calcite.sql.util.ChainedSqlOperatorTable) SqlOperatorTable(org.apache.calcite.sql.SqlOperatorTable) Planner(org.apache.calcite.tools.Planner) FrameworkConfig(org.apache.calcite.tools.FrameworkConfig) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection)

Example 13 with RelOptRule

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

the class PlannerTest method testPlanTransformWithRuleNameConflicts.

/**
 * Unit test that calls {@link Planner#transform} twice with
 *  rule name conflicts
 */
@Test
public void testPlanTransformWithRuleNameConflicts() throws Exception {
    // Create two dummy rules with identical rules.
    RelOptRule rule1 = new RelOptRule(operand(LogicalProject.class, operand(LogicalFilter.class, RelOptRule.any())), "MYRULE") {

        @Override
        public boolean matches(RelOptRuleCall call) {
            return false;
        }

        public void onMatch(RelOptRuleCall call) {
        }
    };
    RelOptRule rule2 = new RelOptRule(operand(LogicalFilter.class, operand(LogicalProject.class, RelOptRule.any())), "MYRULE") {

        @Override
        public boolean matches(RelOptRuleCall call) {
            return false;
        }

        public void onMatch(RelOptRuleCall call) {
        }
    };
    RuleSet ruleSet1 = RuleSets.ofList(rule1, EnumerableRules.ENUMERABLE_FILTER_RULE, EnumerableRules.ENUMERABLE_PROJECT_RULE);
    RuleSet ruleSet2 = RuleSets.ofList(rule2);
    Planner planner = getPlanner(null, Programs.of(ruleSet1), Programs.of(ruleSet2));
    SqlNode parse = planner.parse("select * from \"emps\"");
    SqlNode validate = planner.validate(parse);
    RelNode convert = planner.rel(validate).rel;
    RelTraitSet traitSet = planner.getEmptyTraitSet().replace(EnumerableConvention.INSTANCE);
    RelNode transform = planner.transform(0, traitSet, convert);
    RelNode transform2 = planner.transform(1, traitSet, transform);
    assertThat(toString(transform2), equalTo("EnumerableProject(empid=[$0], deptno=[$1], name=[$2], salary=[$3], commission=[$4])\n" + "  EnumerableTableScan(table=[[hr, emps]])\n"));
}
Also used : RelNode(org.apache.calcite.rel.RelNode) LogicalFilter(org.apache.calcite.rel.logical.LogicalFilter) RelOptPlanner(org.apache.calcite.plan.RelOptPlanner) LogicalProject(org.apache.calcite.rel.logical.LogicalProject) RelTraitSet(org.apache.calcite.plan.RelTraitSet) RelOptRuleCall(org.apache.calcite.plan.RelOptRuleCall) RelOptRule(org.apache.calcite.plan.RelOptRule) SqlNode(org.apache.calcite.sql.SqlNode) Test(org.junit.Test)

Example 14 with RelOptRule

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

the class PigRelBuilderStyleTest method getVolcanoPlanner.

private RelOptPlanner getVolcanoPlanner(RelNode root) {
    final RelBuilderFactory builderFactory = RelBuilder.proto(PigRelFactories.ALL_PIG_REL_FACTORIES);
    // VolcanoPlanner
    final RelOptPlanner planner = root.getCluster().getPlanner();
    for (RelOptRule r : PigRules.ALL_PIG_OPT_RULES) {
        planner.addRule(r);
    }
    planner.removeRule(FilterAggregateTransposeRule.INSTANCE);
    planner.removeRule(FilterJoinRule.FILTER_ON_JOIN);
    planner.addRule(new FilterAggregateTransposeRule(PigFilter.class, builderFactory, PigAggregate.class));
    planner.addRule(new FilterIntoJoinRule(true, builderFactory, TRUE_PREDICATE));
    planner.setRoot(root);
    return planner;
}
Also used : RelBuilderFactory(org.apache.calcite.tools.RelBuilderFactory) PigAggregate(org.apache.calcite.adapter.pig.PigAggregate) FilterIntoJoinRule(org.apache.calcite.rel.rules.FilterJoinRule.FilterIntoJoinRule) FilterAggregateTransposeRule(org.apache.calcite.rel.rules.FilterAggregateTransposeRule) PigFilter(org.apache.calcite.adapter.pig.PigFilter) RelOptPlanner(org.apache.calcite.plan.RelOptPlanner) RelOptRule(org.apache.calcite.plan.RelOptRule)

Example 15 with RelOptRule

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

the class VolcanoPlanner method onNewClass.

@Override
protected void onNewClass(RelNode node) {
    super.onNewClass(node);
    // Create mappings so that instances of this class will match existing
    // operands.
    final Class<? extends RelNode> clazz = node.getClass();
    for (RelOptRule rule : ruleSet) {
        for (RelOptRuleOperand operand : rule.getOperands()) {
            if (operand.getMatchedClass().isAssignableFrom(clazz)) {
                classOperands.put(clazz, operand);
            }
        }
    }
}
Also used : RelOptRuleOperand(org.apache.calcite.plan.RelOptRuleOperand) RelOptRule(org.apache.calcite.plan.RelOptRule)

Aggregations

RelOptRule (org.apache.calcite.plan.RelOptRule)19 Test (org.junit.Test)7 RuleSet (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.tools.RuleSet)6 RelOptPlanner (org.apache.calcite.plan.RelOptPlanner)6 BeamRelNode (org.apache.beam.sdk.extensions.sql.impl.rel.BeamRelNode)5 RelNode (org.apache.calcite.rel.RelNode)5 RelNode (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelNode)4 HepProgramBuilder (org.apache.calcite.plan.hep.HepProgramBuilder)4 ArrayList (java.util.ArrayList)3 RelOptRule (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.plan.RelOptRule)3 RelTraitSet (org.apache.calcite.plan.RelTraitSet)3 VolcanoPlanner (org.apache.calcite.plan.volcano.VolcanoPlanner)3 ImmutableList (com.google.common.collect.ImmutableList)2 Connection (java.sql.Connection)2 List (java.util.List)2 JoinCommuteRule (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.rules.JoinCommuteRule)2 HepPlanner (org.apache.calcite.plan.hep.HepPlanner)2 JaninoRelMetadataProvider (org.apache.calcite.rel.metadata.JaninoRelMetadataProvider)2 Program (org.apache.calcite.tools.Program)2 RuleSet (org.apache.calcite.tools.RuleSet)2