use of org.apache.calcite.rel.metadata.CachingRelMetadataProvider in project calcite by apache.
the class RelOptRulesTest method transitiveInference.
private void transitiveInference(RelOptRule... extraRules) throws Exception {
final DiffRepository diffRepos = getDiffRepos();
final String sql = diffRepos.expand(null, "${sql}");
final HepProgram program = new HepProgramBuilder().addRuleInstance(FilterJoinRule.DUMB_FILTER_ON_JOIN).addRuleInstance(FilterJoinRule.JOIN).addRuleInstance(FilterProjectTransposeRule.INSTANCE).addRuleInstance(FilterSetOpTransposeRule.INSTANCE).build();
final HepPlanner planner = new HepPlanner(program);
final RelRoot root = tester.convertSqlToRel(sql);
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);
relInitial.getCluster().setMetadataProvider(new CachingRelMetadataProvider(plannerChain, planner));
planner.setRoot(relInitial);
RelNode relBefore = planner.findBestExp();
String planBefore = NL + RelOptUtil.toString(relBefore);
diffRepos.assertEquals("planBefore", "${planBefore}", planBefore);
HepProgram program2 = new HepProgramBuilder().addMatchOrder(HepMatchOrder.BOTTOM_UP).addRuleInstance(FilterJoinRule.DUMB_FILTER_ON_JOIN).addRuleInstance(FilterJoinRule.JOIN).addRuleInstance(FilterProjectTransposeRule.INSTANCE).addRuleInstance(FilterSetOpTransposeRule.INSTANCE).addRuleInstance(JoinPushTransitivePredicatesRule.INSTANCE).addRuleCollection(Arrays.asList(extraRules)).build();
final HepPlanner planner2 = new HepPlanner(program2);
planner.registerMetadataProviders(list);
planner2.setRoot(relBefore);
RelNode relAfter = planner2.findBestExp();
String planAfter = NL + RelOptUtil.toString(relAfter);
diffRepos.assertEquals("planAfter", "${planAfter}", planAfter);
}
use of org.apache.calcite.rel.metadata.CachingRelMetadataProvider in project calcite by apache.
the class RelMetadataTest method testSelectivityAggCached.
/**
* Checks that we can cache a metadata request that includes a null
* argument.
*/
@Test
public void testSelectivityAggCached() {
RelNode rel = convertSql("select deptno, count(*) from emp where deptno > 10 " + "group by deptno having count(*) = 0");
rel.getCluster().setMetadataProvider(new CachingRelMetadataProvider(rel.getCluster().getMetadataProvider(), rel.getCluster().getPlanner()));
final RelMetadataQuery mq = RelMetadataQuery.instance();
Double result = mq.getSelectivity(rel, null);
assertThat(result, within(DEFAULT_COMP_SELECTIVITY * DEFAULT_EQUAL_SELECTIVITY, EPSILON));
}
use of org.apache.calcite.rel.metadata.CachingRelMetadataProvider in project hive by apache.
the class TestCBORuleFiredOnlyOnce method testRuleFiredOnlyOnce.
@Test
public void testRuleFiredOnlyOnce() {
HiveConf conf = new HiveConf();
// Create HepPlanner
HepProgramBuilder programBuilder = new HepProgramBuilder();
programBuilder.addMatchOrder(HepMatchOrder.TOP_DOWN);
programBuilder = programBuilder.addRuleCollection(ImmutableList.<RelOptRule>of(DummyRule.INSTANCE));
// Create rules registry to not trigger a rule more than once
HiveRulesRegistry registry = new HiveRulesRegistry();
HivePlannerContext context = new HivePlannerContext(null, registry, null, null, null, null);
HepPlanner planner = new HepPlanner(programBuilder.build(), context);
// Cluster
RexBuilder rexBuilder = new RexBuilder(new JavaTypeFactoryImpl());
RelOptCluster cluster = RelOptCluster.create(planner, rexBuilder);
// Create MD provider
HiveDefaultRelMetadataProvider mdProvider = new HiveDefaultRelMetadataProvider(conf, null);
List<RelMetadataProvider> list = Lists.newArrayList();
list.add(mdProvider.getMetadataProvider());
planner.registerMetadataProviders(list);
RelMetadataProvider chainedProvider = ChainedRelMetadataProvider.of(list);
final RelNode node = new DummyNode(cluster, cluster.traitSet());
node.getCluster().setMetadataProvider(new CachingRelMetadataProvider(chainedProvider, planner));
planner.setRoot(node);
planner.findBestExp();
// Matches 2 times: one time the original node, one time the new node created by the rule
assertEquals(2, DummyRule.INSTANCE.numberMatches);
// It is fired only once: on the original node
assertEquals(1, DummyRule.INSTANCE.numberOnMatch);
}
use of org.apache.calcite.rel.metadata.CachingRelMetadataProvider in project calcite by apache.
the class PlannerImpl method transform.
public RelNode transform(int ruleSetIndex, RelTraitSet requiredOutputTraits, RelNode rel) throws RelConversionException {
ensure(State.STATE_5_CONVERTED);
rel.getCluster().setMetadataProvider(new CachingRelMetadataProvider(rel.getCluster().getMetadataProvider(), rel.getCluster().getPlanner()));
Program program = programs.get(ruleSetIndex);
return program.run(planner, rel, requiredOutputTraits, ImmutableList.<RelOptMaterialization>of(), ImmutableList.<RelOptLattice>of());
}
use of org.apache.calcite.rel.metadata.CachingRelMetadataProvider in project calcite by apache.
the class RelMetadataTest method testMetadataHandlerCacheLimit.
/**
* Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-1808">[CALCITE-1808]
* JaninoRelMetadataProvider loading cache might cause
* OutOfMemoryError</a>.
*/
@Test
public void testMetadataHandlerCacheLimit() {
Assume.assumeTrue("If cache size is too large, this test may fail and the " + "test won't be to blame", SaffronProperties.INSTANCE.metadataHandlerCacheMaximumSize().get() < 10_000);
final int iterationCount = 2_000;
final RelNode rel = convertSql("select * from emp");
final RelMetadataProvider metadataProvider = rel.getCluster().getMetadataProvider();
final RelOptPlanner planner = rel.getCluster().getPlanner();
for (int i = 0; i < iterationCount; i++) {
RelMetadataQuery.THREAD_PROVIDERS.set(JaninoRelMetadataProvider.of(new CachingRelMetadataProvider(metadataProvider, planner)));
final RelMetadataQuery mq = RelMetadataQuery.instance();
final Double result = mq.getRowCount(rel);
assertThat(result, within(14d, 0.1d));
}
}
Aggregations