Search in sources :

Example 1 with CachingRelMetadataProvider

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);
}
Also used : HepProgram(org.apache.calcite.plan.hep.HepProgram) RelNode(org.apache.calcite.rel.RelNode) CachingRelMetadataProvider(org.apache.calcite.rel.metadata.CachingRelMetadataProvider) HepProgramBuilder(org.apache.calcite.plan.hep.HepProgramBuilder) DefaultRelMetadataProvider(org.apache.calcite.rel.metadata.DefaultRelMetadataProvider) ChainedRelMetadataProvider(org.apache.calcite.rel.metadata.ChainedRelMetadataProvider) CachingRelMetadataProvider(org.apache.calcite.rel.metadata.CachingRelMetadataProvider) RelMetadataProvider(org.apache.calcite.rel.metadata.RelMetadataProvider) RelRoot(org.apache.calcite.rel.RelRoot) HepPlanner(org.apache.calcite.plan.hep.HepPlanner)

Example 2 with CachingRelMetadataProvider

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));
}
Also used : RelMetadataQuery(org.apache.calcite.rel.metadata.RelMetadataQuery) RelNode(org.apache.calcite.rel.RelNode) CachingRelMetadataProvider(org.apache.calcite.rel.metadata.CachingRelMetadataProvider) Test(org.junit.Test)

Example 3 with CachingRelMetadataProvider

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);
}
Also used : RelOptCluster(org.apache.calcite.plan.RelOptCluster) CachingRelMetadataProvider(org.apache.calcite.rel.metadata.CachingRelMetadataProvider) HepProgramBuilder(org.apache.calcite.plan.hep.HepProgramBuilder) HepPlanner(org.apache.calcite.plan.hep.HepPlanner) RelOptRule(org.apache.calcite.plan.RelOptRule) HiveRulesRegistry(org.apache.hadoop.hive.ql.optimizer.calcite.rules.HiveRulesRegistry) AbstractRelNode(org.apache.calcite.rel.AbstractRelNode) RelNode(org.apache.calcite.rel.RelNode) JavaTypeFactoryImpl(org.apache.calcite.jdbc.JavaTypeFactoryImpl) RexBuilder(org.apache.calcite.rex.RexBuilder) ChainedRelMetadataProvider(org.apache.calcite.rel.metadata.ChainedRelMetadataProvider) CachingRelMetadataProvider(org.apache.calcite.rel.metadata.CachingRelMetadataProvider) RelMetadataProvider(org.apache.calcite.rel.metadata.RelMetadataProvider) HiveConf(org.apache.hadoop.hive.conf.HiveConf) Test(org.junit.Test)

Example 4 with CachingRelMetadataProvider

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());
}
Also used : Program(org.apache.calcite.tools.Program) CachingRelMetadataProvider(org.apache.calcite.rel.metadata.CachingRelMetadataProvider)

Example 5 with CachingRelMetadataProvider

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));
    }
}
Also used : RelMetadataQuery(org.apache.calcite.rel.metadata.RelMetadataQuery) RelNode(org.apache.calcite.rel.RelNode) CachingRelMetadataProvider(org.apache.calcite.rel.metadata.CachingRelMetadataProvider) JaninoRelMetadataProvider(org.apache.calcite.rel.metadata.JaninoRelMetadataProvider) DefaultRelMetadataProvider(org.apache.calcite.rel.metadata.DefaultRelMetadataProvider) ChainedRelMetadataProvider(org.apache.calcite.rel.metadata.ChainedRelMetadataProvider) ReflectiveRelMetadataProvider(org.apache.calcite.rel.metadata.ReflectiveRelMetadataProvider) CachingRelMetadataProvider(org.apache.calcite.rel.metadata.CachingRelMetadataProvider) RelMetadataProvider(org.apache.calcite.rel.metadata.RelMetadataProvider) RelOptPlanner(org.apache.calcite.plan.RelOptPlanner) Test(org.junit.Test)

Aggregations

CachingRelMetadataProvider (org.apache.calcite.rel.metadata.CachingRelMetadataProvider)6 RelNode (org.apache.calcite.rel.RelNode)5 ChainedRelMetadataProvider (org.apache.calcite.rel.metadata.ChainedRelMetadataProvider)4 RelMetadataProvider (org.apache.calcite.rel.metadata.RelMetadataProvider)4 Test (org.junit.Test)4 DefaultRelMetadataProvider (org.apache.calcite.rel.metadata.DefaultRelMetadataProvider)3 RelMetadataQuery (org.apache.calcite.rel.metadata.RelMetadataQuery)3 RelOptCluster (org.apache.calcite.plan.RelOptCluster)2 RelOptPlanner (org.apache.calcite.plan.RelOptPlanner)2 HepPlanner (org.apache.calcite.plan.hep.HepPlanner)2 HepProgramBuilder (org.apache.calcite.plan.hep.HepProgramBuilder)2 RelRoot (org.apache.calcite.rel.RelRoot)2 JaninoRelMetadataProvider (org.apache.calcite.rel.metadata.JaninoRelMetadataProvider)2 ReflectiveRelMetadataProvider (org.apache.calcite.rel.metadata.ReflectiveRelMetadataProvider)2 Function (com.google.common.base.Function)1 JavaTypeFactoryImpl (org.apache.calcite.jdbc.JavaTypeFactoryImpl)1 RelOptRule (org.apache.calcite.plan.RelOptRule)1 HepProgram (org.apache.calcite.plan.hep.HepProgram)1 AbstractRelNode (org.apache.calcite.rel.AbstractRelNode)1 LogicalAggregate (org.apache.calcite.rel.logical.LogicalAggregate)1