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