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);
}
};
}
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();
}
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);
}
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();
}
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);
}
Aggregations