Search in sources :

Example 31 with RelOptCluster

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

the class TraitPropagationTest method run.

// Created so that we can control when the TraitDefs are defined (e.g.
// before the cluster is created).
private static RelNode run(PropAction action, RuleSet rules) throws Exception {
    FrameworkConfig config = Frameworks.newConfigBuilder().ruleSets(rules).build();
    final Properties info = new Properties();
    final Connection connection = DriverManager.getConnection("jdbc:calcite:", info);
    final CalciteServerStatement statement = connection.createStatement().unwrap(CalciteServerStatement.class);
    final CalcitePrepare.Context prepareContext = statement.createPrepareContext();
    final JavaTypeFactory typeFactory = prepareContext.getTypeFactory();
    CalciteCatalogReader catalogReader = new CalciteCatalogReader(prepareContext.getRootSchema(), prepareContext.getDefaultSchemaPath(), typeFactory, prepareContext.config());
    final RexBuilder rexBuilder = new RexBuilder(typeFactory);
    final RelOptPlanner planner = new VolcanoPlanner(config.getCostFactory(), config.getContext());
    // set up rules before we generate cluster
    planner.clearRelTraitDefs();
    planner.addRelTraitDef(RelCollationTraitDef.INSTANCE);
    planner.addRelTraitDef(ConventionTraitDef.INSTANCE);
    planner.clear();
    for (RelOptRule r : rules) {
        planner.addRule(r);
    }
    final RelOptCluster cluster = RelOptCluster.create(planner, rexBuilder);
    return action.apply(cluster, catalogReader, prepareContext.getRootSchema().plus());
}
Also used : RelOptCluster(org.apache.calcite.plan.RelOptCluster) Connection(java.sql.Connection) CalcitePrepare(org.apache.calcite.jdbc.CalcitePrepare) Properties(java.util.Properties) RelOptPlanner(org.apache.calcite.plan.RelOptPlanner) RelOptRule(org.apache.calcite.plan.RelOptRule) CalciteCatalogReader(org.apache.calcite.prepare.CalciteCatalogReader) CalciteServerStatement(org.apache.calcite.server.CalciteServerStatement) JavaTypeFactory(org.apache.calcite.adapter.java.JavaTypeFactory) RexBuilder(org.apache.calcite.rex.RexBuilder) FrameworkConfig(org.apache.calcite.tools.FrameworkConfig)

Example 32 with RelOptCluster

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

the class VolcanoPlannerTest method testRemoveSingleReformed.

/**
 * Previously, this didn't work because ReformedRemoveSingleRule uses a
 * pattern which spans calling conventions.
 */
// broken, because ReformedSingleRule matches child traits strictly
@Ignore
@Test
public void testRemoveSingleReformed() {
    VolcanoPlanner planner = new VolcanoPlanner();
    planner.ambitious = true;
    planner.addRelTraitDef(ConventionTraitDef.INSTANCE);
    planner.addRule(new PhysLeafRule());
    planner.addRule(new ReformedRemoveSingleRule());
    RelOptCluster cluster = newCluster(planner);
    NoneLeafRel leafRel = new NoneLeafRel(cluster, "a");
    NoneSingleRel singleRel = new NoneSingleRel(cluster, leafRel);
    RelNode convertedRel = planner.changeTraits(singleRel, cluster.traitSetOf(PHYS_CALLING_CONVENTION));
    planner.setRoot(convertedRel);
    RelNode result = planner.chooseDelegate().findBestExp();
    assertTrue(result instanceof PhysLeafRel);
    PhysLeafRel resultLeaf = (PhysLeafRel) result;
    assertEquals("c", resultLeaf.label);
}
Also used : RelOptCluster(org.apache.calcite.plan.RelOptCluster) RelNode(org.apache.calcite.rel.RelNode) PhysLeafRule(org.apache.calcite.plan.volcano.PlannerTests.PhysLeafRule) NoneSingleRel(org.apache.calcite.plan.volcano.PlannerTests.NoneSingleRel) NoneLeafRel(org.apache.calcite.plan.volcano.PlannerTests.NoneLeafRel) PhysLeafRel(org.apache.calcite.plan.volcano.PlannerTests.PhysLeafRel) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 33 with RelOptCluster

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

the class VolcanoPlannerTest method testListener.

/**
 * Tests whether planner correctly notifies listeners of events.
 */
@Ignore
@Test
public void testListener() {
    TestListener listener = new TestListener();
    VolcanoPlanner planner = new VolcanoPlanner();
    planner.addListener(listener);
    planner.addRelTraitDef(ConventionTraitDef.INSTANCE);
    planner.addRule(new PhysLeafRule());
    RelOptCluster cluster = newCluster(planner);
    NoneLeafRel leafRel = new NoneLeafRel(cluster, "a");
    RelNode convertedRel = planner.changeTraits(leafRel, cluster.traitSetOf(PHYS_CALLING_CONVENTION));
    planner.setRoot(convertedRel);
    RelNode result = planner.chooseDelegate().findBestExp();
    assertTrue(result instanceof PhysLeafRel);
    List<RelOptListener.RelEvent> eventList = listener.getEventList();
    // add node
    checkEvent(eventList, 0, RelOptListener.RelEquivalenceEvent.class, leafRel, null);
    // internal subset
    checkEvent(eventList, 1, RelOptListener.RelEquivalenceEvent.class, null, null);
    // before rule
    checkEvent(eventList, 2, RelOptListener.RuleAttemptedEvent.class, leafRel, PhysLeafRule.class);
    // before rule
    checkEvent(eventList, 3, RelOptListener.RuleProductionEvent.class, result, PhysLeafRule.class);
    // result of rule
    checkEvent(eventList, 4, RelOptListener.RelEquivalenceEvent.class, result, null);
    // after rule
    checkEvent(eventList, 5, RelOptListener.RuleProductionEvent.class, result, PhysLeafRule.class);
    // after rule
    checkEvent(eventList, 6, RelOptListener.RuleAttemptedEvent.class, leafRel, PhysLeafRule.class);
    // choose plan
    checkEvent(eventList, 7, RelOptListener.RelChosenEvent.class, result, null);
    // finish choosing plan
    checkEvent(eventList, 8, RelOptListener.RelChosenEvent.class, null, null);
}
Also used : RelOptCluster(org.apache.calcite.plan.RelOptCluster) RelOptListener(org.apache.calcite.plan.RelOptListener) RelNode(org.apache.calcite.rel.RelNode) PhysLeafRule(org.apache.calcite.plan.volcano.PlannerTests.PhysLeafRule) NoneLeafRel(org.apache.calcite.plan.volcano.PlannerTests.NoneLeafRel) PhysLeafRel(org.apache.calcite.plan.volcano.PlannerTests.PhysLeafRel) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 34 with RelOptCluster

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

the class VolcanoPlannerTest method testTransformSingleReformed.

/**
 * Tests transformation of a single+leaf from NONE to PHYS. In the past,
 * this one didn't work due to the definition of ReformedSingleRule.
 */
// broken, because ReformedSingleRule matches child traits strictly
@Ignore
@Test
public void testTransformSingleReformed() {
    VolcanoPlanner planner = new VolcanoPlanner();
    planner.addRelTraitDef(ConventionTraitDef.INSTANCE);
    planner.addRule(new PhysLeafRule());
    planner.addRule(new ReformedSingleRule());
    RelOptCluster cluster = newCluster(planner);
    NoneLeafRel leafRel = new NoneLeafRel(cluster, "a");
    NoneSingleRel singleRel = new NoneSingleRel(cluster, leafRel);
    RelNode convertedRel = planner.changeTraits(singleRel, cluster.traitSetOf(PHYS_CALLING_CONVENTION));
    planner.setRoot(convertedRel);
    RelNode result = planner.chooseDelegate().findBestExp();
    assertTrue(result instanceof PhysSingleRel);
}
Also used : RelOptCluster(org.apache.calcite.plan.RelOptCluster) RelNode(org.apache.calcite.rel.RelNode) PhysLeafRule(org.apache.calcite.plan.volcano.PlannerTests.PhysLeafRule) NoneSingleRel(org.apache.calcite.plan.volcano.PlannerTests.NoneSingleRel) NoneLeafRel(org.apache.calcite.plan.volcano.PlannerTests.NoneLeafRel) PhysSingleRel(org.apache.calcite.plan.volcano.PlannerTests.PhysSingleRel) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 35 with RelOptCluster

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

the class VolcanoPlannerTest method testSubsetRule.

/**
 * Tests a rule that is fired once per subset (whereas most rules are fired
 * once per rel in a set or rel in a subset)
 */
@Test
public void testSubsetRule() {
    VolcanoPlanner planner = new VolcanoPlanner();
    planner.addRelTraitDef(ConventionTraitDef.INSTANCE);
    planner.addRule(new PhysLeafRule());
    planner.addRule(new GoodSingleRule());
    final List<String> buf = new ArrayList<>();
    planner.addRule(new SubsetRule(buf));
    RelOptCluster cluster = newCluster(planner);
    NoneLeafRel leafRel = new NoneLeafRel(cluster, "a");
    NoneSingleRel singleRel = new NoneSingleRel(cluster, leafRel);
    RelNode convertedRel = planner.changeTraits(singleRel, cluster.traitSetOf(PHYS_CALLING_CONVENTION));
    planner.setRoot(convertedRel);
    RelNode result = planner.chooseDelegate().findBestExp();
    assertTrue(result instanceof PhysSingleRel);
    assertThat(sort(buf), equalTo(sort("NoneSingleRel:Subset#0.NONE", "PhysSingleRel:Subset#0.NONE", "PhysSingleRel:Subset#0.PHYS")));
}
Also used : RelOptCluster(org.apache.calcite.plan.RelOptCluster) GoodSingleRule(org.apache.calcite.plan.volcano.PlannerTests.GoodSingleRule) RelNode(org.apache.calcite.rel.RelNode) PhysLeafRule(org.apache.calcite.plan.volcano.PlannerTests.PhysLeafRule) NoneSingleRel(org.apache.calcite.plan.volcano.PlannerTests.NoneSingleRel) ArrayList(java.util.ArrayList) NoneLeafRel(org.apache.calcite.plan.volcano.PlannerTests.NoneLeafRel) PhysSingleRel(org.apache.calcite.plan.volcano.PlannerTests.PhysSingleRel) 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