Search in sources :

Example 91 with RelOptCluster

use of org.apache.calcite.plan.RelOptCluster in project calcite by apache.

the class RelOptTestBase method checkPlanning.

/**
 * Checks the plan for a SQL statement before/after executing a given rule,
 * with a pre-program to prepare the tree.
 *
 * @param tester     Tester
 * @param preProgram Program to execute before comparing before state
 * @param planner    Planner
 * @param sql        SQL query
 * @param unchanged  Whether the rule is to have no effect
 */
protected void checkPlanning(Tester tester, HepProgram preProgram, RelOptPlanner planner, String sql, boolean unchanged) {
    final DiffRepository diffRepos = getDiffRepos();
    String sql2 = diffRepos.expand("sql", sql);
    final RelRoot root = tester.convertSqlToRel(sql2);
    final RelNode relInitial = root.rel;
    assertTrue(relInitial != null);
    List<RelMetadataProvider> list = Lists.newArrayList();
    list.add(DefaultRelMetadataProvider.INSTANCE);
    planner.registerMetadataProviders(list);
    RelMetadataProvider plannerChain = ChainedRelMetadataProvider.of(list);
    final RelOptCluster cluster = relInitial.getCluster();
    cluster.setMetadataProvider(plannerChain);
    RelNode relBefore;
    if (preProgram == null) {
        relBefore = relInitial;
    } else {
        HepPlanner prePlanner = new HepPlanner(preProgram);
        prePlanner.setRoot(relInitial);
        relBefore = prePlanner.findBestExp();
    }
    assertThat(relBefore, notNullValue());
    final String planBefore = NL + RelOptUtil.toString(relBefore);
    diffRepos.assertEquals("planBefore", "${planBefore}", planBefore);
    SqlToRelTestBase.assertValid(relBefore);
    planner.setRoot(relBefore);
    RelNode r = planner.findBestExp();
    if (tester.isLateDecorrelate()) {
        final String planMid = NL + RelOptUtil.toString(r);
        diffRepos.assertEquals("planMid", "${planMid}", planMid);
        SqlToRelTestBase.assertValid(r);
        final RelBuilder relBuilder = RelFactories.LOGICAL_BUILDER.create(cluster, null);
        r = RelDecorrelator.decorrelateQuery(r, relBuilder);
    }
    final String planAfter = NL + RelOptUtil.toString(r);
    if (unchanged) {
        assertThat(planAfter, is(planBefore));
    } else {
        diffRepos.assertEquals("planAfter", "${planAfter}", planAfter);
        if (planBefore.equals(planAfter)) {
            throw new AssertionError("Expected plan before and after is the same.\n" + "You must use unchanged=true or call checkPlanUnchanged");
        }
    }
    SqlToRelTestBase.assertValid(r);
}
Also used : RelOptCluster(org.apache.calcite.plan.RelOptCluster) RelBuilder(org.apache.calcite.tools.RelBuilder) RelNode(org.apache.calcite.rel.RelNode) DefaultRelMetadataProvider(org.apache.calcite.rel.metadata.DefaultRelMetadataProvider) ChainedRelMetadataProvider(org.apache.calcite.rel.metadata.ChainedRelMetadataProvider) RelMetadataProvider(org.apache.calcite.rel.metadata.RelMetadataProvider) RelRoot(org.apache.calcite.rel.RelRoot) HepPlanner(org.apache.calcite.plan.hep.HepPlanner)

Example 92 with RelOptCluster

use of org.apache.calcite.plan.RelOptCluster in project calcite by apache.

the class SqlToRelConverterExtendedTest method foo.

public static void foo(RelNode rel) {
    // Convert rel tree to JSON.
    final RelJsonWriter writer = new RelJsonWriter();
    rel.explain(writer);
    final String json = writer.asString();
    // Find the schema. If there are no tables in the plan, we won't need one.
    final RelOptSchema[] schemas = { null };
    rel.accept(new RelShuttleImpl() {

        @Override
        public RelNode visit(TableScan scan) {
            schemas[0] = scan.getTable().getRelOptSchema();
            return super.visit(scan);
        }
    });
    // Convert JSON back to rel tree.
    Frameworks.withPlanner(new Frameworks.PlannerAction<Object>() {

        public Object apply(RelOptCluster cluster, RelOptSchema relOptSchema, SchemaPlus rootSchema) {
            final RelJsonReader reader = new RelJsonReader(cluster, schemas[0], rootSchema);
            try {
                RelNode x = reader.read(json);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            return null;
        }
    });
}
Also used : RelOptCluster(org.apache.calcite.plan.RelOptCluster) TableScan(org.apache.calcite.rel.core.TableScan) RelOptSchema(org.apache.calcite.plan.RelOptSchema) SchemaPlus(org.apache.calcite.schema.SchemaPlus) RelShuttleImpl(org.apache.calcite.rel.RelShuttleImpl) RelJsonWriter(org.apache.calcite.rel.externalize.RelJsonWriter) IOException(java.io.IOException) RelNode(org.apache.calcite.rel.RelNode) Frameworks(org.apache.calcite.tools.Frameworks) RelJsonReader(org.apache.calcite.rel.externalize.RelJsonReader)

Example 93 with RelOptCluster

use of org.apache.calcite.plan.RelOptCluster in project calcite by apache.

the class CollationConversionTest method testCollationConversion.

@Test
public void testCollationConversion() {
    final VolcanoPlanner planner = new VolcanoPlanner();
    planner.addRelTraitDef(ConventionTraitDef.INSTANCE);
    planner.addRelTraitDef(COLLATION_TRAIT_DEF);
    planner.addRule(new SingleNodeRule());
    planner.addRule(new LeafTraitRule());
    planner.addRule(ExpandConversionRule.INSTANCE);
    final RelOptCluster cluster = newCluster(planner);
    final NoneLeafRel leafRel = new NoneLeafRel(cluster, "a");
    final NoneSingleRel singleRel = new NoneSingleRel(cluster, leafRel);
    final RelNode convertedRel = planner.changeTraits(singleRel, cluster.traitSetOf(PHYS_CALLING_CONVENTION).plus(ROOT_COLLATION));
    planner.setRoot(convertedRel);
    RelNode result = planner.chooseDelegate().findBestExp();
    assertTrue(result instanceof RootSingleRel);
    assertTrue(result.getTraitSet().contains(ROOT_COLLATION));
    assertTrue(result.getTraitSet().contains(PHYS_CALLING_CONVENTION));
    final RelNode input = result.getInput(0);
    assertTrue(input instanceof PhysicalSort);
    assertTrue(result.getTraitSet().contains(ROOT_COLLATION));
    assertTrue(input.getTraitSet().contains(PHYS_CALLING_CONVENTION));
    final RelNode input2 = input.getInput(0);
    assertTrue(input2 instanceof LeafRel);
    assertTrue(input2.getTraitSet().contains(LEAF_COLLATION));
    assertTrue(input.getTraitSet().contains(PHYS_CALLING_CONVENTION));
}
Also used : RelOptCluster(org.apache.calcite.plan.RelOptCluster) TestLeafRel(org.apache.calcite.plan.volcano.PlannerTests.TestLeafRel) RelNode(org.apache.calcite.rel.RelNode) Test(org.junit.Test)

Example 94 with RelOptCluster

use of org.apache.calcite.plan.RelOptCluster in project calcite by apache.

the class ComboRuleTest method testCombo.

@Test
public void testCombo() {
    VolcanoPlanner planner = new VolcanoPlanner();
    planner.addRelTraitDef(ConventionTraitDef.INSTANCE);
    planner.addRule(new ComboRule());
    planner.addRule(new AddIntermediateNodeRule());
    planner.addRule(new GoodSingleRule());
    RelOptCluster cluster = newCluster(planner);
    NoneLeafRel leafRel = new NoneLeafRel(cluster, "a");
    NoneSingleRel singleRel = new NoneSingleRel(cluster, leafRel);
    NoneSingleRel singleRel2 = new NoneSingleRel(cluster, singleRel);
    RelNode convertedRel = planner.changeTraits(singleRel2, cluster.traitSetOf(PHYS_CALLING_CONVENTION));
    planner.setRoot(convertedRel);
    RelNode result = planner.chooseDelegate().findBestExp();
    assertTrue(result instanceof IntermediateNode);
}
Also used : RelOptCluster(org.apache.calcite.plan.RelOptCluster) GoodSingleRule(org.apache.calcite.plan.volcano.PlannerTests.GoodSingleRule) RelNode(org.apache.calcite.rel.RelNode) NoneSingleRel(org.apache.calcite.plan.volcano.PlannerTests.NoneSingleRel) NoneLeafRel(org.apache.calcite.plan.volcano.PlannerTests.NoneLeafRel) Test(org.junit.Test)

Example 95 with RelOptCluster

use of org.apache.calcite.plan.RelOptCluster in project calcite by apache.

the class TraitConversionTest method testTraitConversion.

@Test
public void testTraitConversion() {
    final VolcanoPlanner planner = new VolcanoPlanner();
    planner.addRelTraitDef(ConventionTraitDef.INSTANCE);
    planner.addRelTraitDef(NEW_TRAIT_DEF_INSTANCE);
    planner.addRule(new RandomSingleTraitRule());
    planner.addRule(new SingleLeafTraitRule());
    planner.addRule(ExpandConversionRule.INSTANCE);
    final RelOptCluster cluster = newCluster(planner);
    final NoneLeafRel leafRel = new NoneLeafRel(cluster, "a");
    final NoneSingleRel singleRel = new NoneSingleRel(cluster, leafRel);
    final RelNode convertedRel = planner.changeTraits(singleRel, cluster.traitSetOf(PHYS_CALLING_CONVENTION));
    planner.setRoot(convertedRel);
    final RelNode result = planner.chooseDelegate().findBestExp();
    assertTrue(result instanceof RandomSingleRel);
    assertTrue(result.getTraitSet().contains(PHYS_CALLING_CONVENTION));
    assertTrue(result.getTraitSet().contains(SIMPLE_DISTRIBUTION_RANDOM));
    final RelNode input = result.getInput(0);
    assertTrue(input instanceof BridgeRel);
    assertTrue(input.getTraitSet().contains(PHYS_CALLING_CONVENTION));
    assertTrue(input.getTraitSet().contains(SIMPLE_DISTRIBUTION_RANDOM));
    final RelNode input2 = input.getInput(0);
    assertTrue(input2 instanceof SingletonLeafRel);
    assertTrue(input2.getTraitSet().contains(PHYS_CALLING_CONVENTION));
    assertTrue(input2.getTraitSet().contains(SIMPLE_DISTRIBUTION_SINGLETON));
}
Also used : RelOptCluster(org.apache.calcite.plan.RelOptCluster) RelNode(org.apache.calcite.rel.RelNode) Test(org.junit.Test)

Aggregations

RelOptCluster (org.apache.calcite.plan.RelOptCluster)117 RelNode (org.apache.calcite.rel.RelNode)63 RelTraitSet (org.apache.calcite.plan.RelTraitSet)36 RexBuilder (org.apache.calcite.rex.RexBuilder)35 RexNode (org.apache.calcite.rex.RexNode)31 ArrayList (java.util.ArrayList)25 RelDataType (org.apache.calcite.rel.type.RelDataType)23 Test (org.junit.Test)21 ImmutableBitSet (org.apache.calcite.util.ImmutableBitSet)15 List (java.util.List)13 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)13 RelBuilder (org.apache.calcite.tools.RelBuilder)13 RelCollation (org.apache.calcite.rel.RelCollation)12 RelMetadataQuery (org.apache.calcite.rel.metadata.RelMetadataQuery)11 RelOptTable (org.apache.calcite.plan.RelOptTable)10 ImmutableList (com.google.common.collect.ImmutableList)9 HashMap (java.util.HashMap)9 RelOptPlanner (org.apache.calcite.plan.RelOptPlanner)9 Join (org.apache.calcite.rel.core.Join)9 LogicalJoin (org.apache.calcite.rel.logical.LogicalJoin)9