Search in sources :

Example 71 with HepProgramBuilder

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

the class RelOptRulesTest method testEmptySort.

@Test
public void testEmptySort() {
    HepProgram program = new HepProgramBuilder().addRuleInstance(ReduceExpressionsRule.FILTER_INSTANCE).addRuleInstance(PruneEmptyRules.SORT_INSTANCE).build();
    checkPlanning(program, "select * from emp where false order by deptno");
}
Also used : HepProgram(org.apache.calcite.plan.hep.HepProgram) HepProgramBuilder(org.apache.calcite.plan.hep.HepProgramBuilder) Test(org.junit.Test)

Example 72 with HepProgramBuilder

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

the class RelOptRulesTest method testIntersectToDistinct.

/**
 * Tests {@link org.apache.calcite.rel.rules.IntersectToDistinctRule},
 * which rewrites an {@link Intersect} operator with 3 inputs.
 */
@Test
public void testIntersectToDistinct() throws Exception {
    HepProgram program = new HepProgramBuilder().addRuleInstance(UnionMergeRule.INTERSECT_INSTANCE).addRuleInstance(IntersectToDistinctRule.INSTANCE).build();
    final String sql = "select * from emp where deptno = 10\n" + "intersect\n" + "select * from emp where deptno = 20\n" + "intersect\n" + "select * from emp where deptno = 30\n";
    sql(sql).with(program).check();
}
Also used : HepProgram(org.apache.calcite.plan.hep.HepProgram) HepProgramBuilder(org.apache.calcite.plan.hep.HepProgramBuilder) Test(org.junit.Test)

Example 73 with HepProgramBuilder

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

the class RelOptRulesTest method testCustomColumnResolvingInNonCorrelatedSubQuery.

@Test
public void testCustomColumnResolvingInNonCorrelatedSubQuery() {
    final String sql = "select *\n" + "from struct.t t1\n" + "where c0 in (\n" + "  select f1.c0 from struct.t t2)";
    final HepProgram program = new HepProgramBuilder().addRuleInstance(SubQueryRemoveRule.PROJECT).addRuleInstance(SubQueryRemoveRule.FILTER).addRuleInstance(SubQueryRemoveRule.JOIN).build();
    sql(sql).withTrim(true).expand(false).with(program).check();
}
Also used : HepProgram(org.apache.calcite.plan.hep.HepProgram) HepProgramBuilder(org.apache.calcite.plan.hep.HepProgramBuilder) Test(org.junit.Test)

Example 74 with HepProgramBuilder

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

the class RelOptRulesTest method testHeterogeneousConversion.

@Ignore("cycles")
@Test
public void testHeterogeneousConversion() throws Exception {
    // This one tests the planner's ability to correctly
    // apply different converters on top of a common
    // sub-expression.  The common sub-expression is the
    // reference to the table sales.emps.  On top of that
    // are two projections, unioned at the top.  For one
    // of the projections, we force a Fennel implementation.
    // For the other, we force a Java implementation.
    // Then, we request conversion from Fennel to Java,
    // and verify that it only applies to one usage of the
    // table, not both (which would be incorrect).
    HepProgram program = new HepProgramBuilder().addRuleInstance(TableScanRule.INSTANCE).addRuleInstance(ProjectToCalcRule.INSTANCE).addMatchLimit(1).addMatchLimit(HepProgram.MATCH_UNTIL_FIXPOINT).build();
    checkPlanning(program, "select upper(ename) from emp union all" + " select lower(ename) from emp");
}
Also used : HepProgram(org.apache.calcite.plan.hep.HepProgram) HepProgramBuilder(org.apache.calcite.plan.hep.HepProgramBuilder) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 75 with HepProgramBuilder

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

the class DefaultSqlHandler method transform.

/**
 * Transform RelNode to a new RelNode, targeting the provided set of traits. Also will log the outcome if asked.
 *
 * @param plannerType
 *          The type of Planner to use.
 * @param phase
 *          The transformation phase we're running.
 * @param input
 *          The origianl RelNode
 * @param targetTraits
 *          The traits we are targeting for output.
 * @param log
 *          Whether to log the planning phase.
 * @return The transformed relnode.
 */
protected RelNode transform(PlannerType plannerType, PlannerPhase phase, RelNode input, RelTraitSet targetTraits, boolean log) {
    final Stopwatch watch = Stopwatch.createStarted();
    final RuleSet rules = config.getRules(phase);
    final RelTraitSet toTraits = targetTraits.simplify();
    final RelNode output;
    switch(plannerType) {
        case HEP_BOTTOM_UP:
        case HEP:
            {
                final HepProgramBuilder hepPgmBldr = new HepProgramBuilder();
                if (plannerType == PlannerType.HEP_BOTTOM_UP) {
                    hepPgmBldr.addMatchOrder(HepMatchOrder.BOTTOM_UP);
                }
                for (RelOptRule rule : rules) {
                    hepPgmBldr.addRuleInstance(rule);
                }
                // Set noDAG = true to avoid caching problems which lead to incorrect Drill work.
                final HepPlanner planner = new HepPlanner(hepPgmBldr.build(), context.getPlannerSettings(), true, null, RelOptCostImpl.FACTORY);
                JaninoRelMetadataProvider relMetadataProvider = JaninoRelMetadataProvider.of(DrillDefaultRelMetadataProvider.INSTANCE);
                RelMetadataQuery.THREAD_PROVIDERS.set(relMetadataProvider);
                // Modify RelMetaProvider for every RelNode in the SQL operator Rel tree.
                input.accept(new MetaDataProviderModifier(relMetadataProvider));
                planner.setRoot(input);
                if (!input.getTraitSet().equals(targetTraits)) {
                    planner.changeTraits(input, toTraits);
                }
                output = planner.findBestExp();
                break;
            }
        case VOLCANO:
        default:
            {
                // as weird as it seems, the cluster's only planner is the volcano planner.
                final RelOptPlanner planner = input.getCluster().getPlanner();
                final Program program = Programs.of(rules);
                Preconditions.checkArgument(planner instanceof VolcanoPlanner, "Cluster is expected to be constructed using VolcanoPlanner. Was actually of type %s.", planner.getClass().getName());
                output = program.run(planner, input, toTraits, ImmutableList.<RelOptMaterialization>of(), ImmutableList.<RelOptLattice>of());
                break;
            }
    }
    if (log) {
        log(plannerType, phase, output, logger, watch);
    }
    return output;
}
Also used : RuleSet(org.apache.calcite.tools.RuleSet) Program(org.apache.calcite.tools.Program) RelNode(org.apache.calcite.rel.RelNode) Stopwatch(com.google.common.base.Stopwatch) HepProgramBuilder(org.apache.calcite.plan.hep.HepProgramBuilder) JaninoRelMetadataProvider(org.apache.calcite.rel.metadata.JaninoRelMetadataProvider) VolcanoPlanner(org.apache.calcite.plan.volcano.VolcanoPlanner) RelTraitSet(org.apache.calcite.plan.RelTraitSet) HepPlanner(org.apache.calcite.plan.hep.HepPlanner) RelOptPlanner(org.apache.calcite.plan.RelOptPlanner) RelOptRule(org.apache.calcite.plan.RelOptRule)

Aggregations

HepProgramBuilder (org.apache.calcite.plan.hep.HepProgramBuilder)168 Test (org.junit.Test)147 HepProgram (org.apache.calcite.plan.hep.HepProgram)143 HepPlanner (org.apache.calcite.plan.hep.HepPlanner)56 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