Search in sources :

Example 6 with HepProgramBuilder

use of org.apache.calcite.plan.hep.HepProgramBuilder in project calcite by apache.

the class Programs method heuristicJoinOrder.

/**
 * Creates a program that invokes heuristic join-order optimization
 * (via {@link org.apache.calcite.rel.rules.JoinToMultiJoinRule},
 * {@link org.apache.calcite.rel.rules.MultiJoin} and
 * {@link org.apache.calcite.rel.rules.LoptOptimizeJoinRule})
 * if there are 6 or more joins (7 or more relations).
 */
public static Program heuristicJoinOrder(final Iterable<? extends RelOptRule> rules, final boolean bushy, final int minJoinCount) {
    return new Program() {

        public RelNode run(RelOptPlanner planner, RelNode rel, RelTraitSet requiredOutputTraits, List<RelOptMaterialization> materializations, List<RelOptLattice> lattices) {
            final int joinCount = RelOptUtil.countJoins(rel);
            final Program program;
            if (joinCount < minJoinCount) {
                program = ofRules(rules);
            } else {
                // Create a program that gathers together joins as a MultiJoin.
                final HepProgram hep = new HepProgramBuilder().addRuleInstance(FilterJoinRule.FILTER_ON_JOIN).addMatchOrder(HepMatchOrder.BOTTOM_UP).addRuleInstance(JoinToMultiJoinRule.INSTANCE).build();
                final Program program1 = of(hep, false, DefaultRelMetadataProvider.INSTANCE);
                // Create a program that contains a rule to expand a MultiJoin
                // into heuristically ordered joins.
                // We use the rule set passed in, but remove JoinCommuteRule and
                // JoinPushThroughJoinRule, because they cause exhaustive search.
                final List<RelOptRule> list = Lists.newArrayList(rules);
                list.removeAll(ImmutableList.of(JoinCommuteRule.INSTANCE, JoinAssociateRule.INSTANCE, JoinPushThroughJoinRule.LEFT, JoinPushThroughJoinRule.RIGHT));
                list.add(bushy ? MultiJoinOptimizeBushyRule.INSTANCE : LoptOptimizeJoinRule.INSTANCE);
                final Program program2 = ofRules(list);
                program = sequence(program1, program2);
            }
            return program.run(planner, rel, requiredOutputTraits, materializations, lattices);
        }
    };
}
Also used : HepProgram(org.apache.calcite.plan.hep.HepProgram) HepProgram(org.apache.calcite.plan.hep.HepProgram) RelNode(org.apache.calcite.rel.RelNode) HepProgramBuilder(org.apache.calcite.plan.hep.HepProgramBuilder) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) RelTraitSet(org.apache.calcite.plan.RelTraitSet) RelOptPlanner(org.apache.calcite.plan.RelOptPlanner) RelOptRule(org.apache.calcite.plan.RelOptRule)

Example 7 with HepProgramBuilder

use of org.apache.calcite.plan.hep.HepProgramBuilder in project calcite by apache.

the class RelOptRulesTest method testWindowInParenthesis.

/**
 * Test case for
 * <a href="https://issues.apache.org/jira/browse/CALCITE-888">[CALCITE-888]
 * Overlay window loses PARTITION BY list</a>.
 */
@Test
public void testWindowInParenthesis() {
    HepProgramBuilder builder = new HepProgramBuilder();
    builder.addRuleClass(ProjectToWindowRule.class);
    HepPlanner hepPlanner = new HepPlanner(builder.build());
    hepPlanner.addRule(ProjectToWindowRule.PROJECT);
    final String sql = "select count(*) over (w), count(*) over w\n" + "from emp\n" + "window w as (partition by empno order by empno)";
    sql(sql).with(hepPlanner).check();
}
Also used : HepProgramBuilder(org.apache.calcite.plan.hep.HepProgramBuilder) HepPlanner(org.apache.calcite.plan.hep.HepPlanner) Test(org.junit.Test)

Example 8 with HepProgramBuilder

use of org.apache.calcite.plan.hep.HepProgramBuilder in project calcite by apache.

the class RelOptRulesTest method testProjectToWindowRuleForMultipleWindows.

@Test
public void testProjectToWindowRuleForMultipleWindows() {
    HepProgram preProgram = new HepProgramBuilder().build();
    HepProgramBuilder builder = new HepProgramBuilder();
    builder.addRuleClass(ProjectToWindowRule.class);
    HepPlanner hepPlanner = new HepPlanner(builder.build());
    hepPlanner.addRule(ProjectToWindowRule.PROJECT);
    final String sql = "select\n" + " count(*) over(partition by empno order by sal) as count1,\n" + " count(*) over(partition by deptno order by sal) as count2,\n" + " sum(deptno) over(partition by empno order by sal) as sum1,\n" + " sum(deptno) over(partition by deptno order by sal) as sum2\n" + "from emp";
    checkPlanning(tester, preProgram, hepPlanner, sql);
}
Also used : HepProgram(org.apache.calcite.plan.hep.HepProgram) HepProgramBuilder(org.apache.calcite.plan.hep.HepProgramBuilder) HepPlanner(org.apache.calcite.plan.hep.HepPlanner) Test(org.junit.Test)

Example 9 with HepProgramBuilder

use of org.apache.calcite.plan.hep.HepProgramBuilder in project calcite by apache.

the class RelOptRulesTest method testMergeSetOpMixed.

/**
 * Tests that {@link UnionMergeRule} does nothing if its arguments have
 * are different set operators, {@link Union} and {@link Intersect}.
 */
@Test
public void testMergeSetOpMixed() throws Exception {
    HepProgram program = new HepProgramBuilder().addRuleInstance(UnionMergeRule.INSTANCE).addRuleInstance(UnionMergeRule.INTERSECT_INSTANCE).build();
    final String sql = "select * from emp where deptno = 10\n" + "union\n" + "select * from emp where deptno = 20\n" + "intersect\n" + "select * from emp where deptno = 30\n";
    sql(sql).with(program).checkUnchanged();
}
Also used : HepProgram(org.apache.calcite.plan.hep.HepProgram) HepProgramBuilder(org.apache.calcite.plan.hep.HepProgramBuilder) Test(org.junit.Test)

Example 10 with HepProgramBuilder

use of org.apache.calcite.plan.hep.HepProgramBuilder in project calcite by apache.

the class RelOptRulesTest method testProjectWindowTransposeRuleWithConstants.

@Test
public void testProjectWindowTransposeRuleWithConstants() {
    HepProgram program = new HepProgramBuilder().addRuleInstance(ProjectToWindowRule.PROJECT).addRuleInstance(ProjectMergeRule.INSTANCE).addRuleInstance(ProjectWindowTransposeRule.INSTANCE).build();
    final String sql = "select col1, col2\n" + "from (\n" + "  select empno,\n" + "    sum(100) over (partition by  deptno order by sal) as col1,\n" + "  sum(1000) over(partition by deptno order by sal) as col2\n" + "  from emp)";
    checkPlanning(program, sql);
}
Also used : HepProgram(org.apache.calcite.plan.hep.HepProgram) HepProgramBuilder(org.apache.calcite.plan.hep.HepProgramBuilder) Test(org.junit.Test)

Aggregations

HepProgramBuilder (org.apache.calcite.plan.hep.HepProgramBuilder)167 Test (org.junit.Test)147 HepProgram (org.apache.calcite.plan.hep.HepProgram)142 HepPlanner (org.apache.calcite.plan.hep.HepPlanner)55 RelNode (org.apache.calcite.rel.RelNode)9 RelOptRule (org.apache.calcite.plan.RelOptRule)5 RelDataType (org.apache.calcite.rel.type.RelDataType)5 RexBuilder (org.apache.calcite.rex.RexBuilder)5 JavaTypeFactoryImpl (org.apache.calcite.jdbc.JavaTypeFactoryImpl)4 RelOptCluster (org.apache.calcite.plan.RelOptCluster)4 AggregateExtractProjectRule (org.apache.calcite.rel.rules.AggregateExtractProjectRule)4 Ignore (org.junit.Ignore)4 RelOptPlanner (org.apache.calcite.plan.RelOptPlanner)3 RelTraitSet (org.apache.calcite.plan.RelTraitSet)3 LogicalTableScan (org.apache.calcite.rel.logical.LogicalTableScan)3 RuleSet (org.apache.calcite.tools.RuleSet)3 Before (org.junit.Before)3 Properties (java.util.Properties)2 CalciteConnectionConfigImpl (org.apache.calcite.config.CalciteConnectionConfigImpl)2 Context (org.apache.calcite.plan.Context)2