Search in sources :

Example 11 with RelMetadataProvider

use of org.apache.calcite.rel.metadata.RelMetadataProvider in project calcite by apache.

the class RelMetadataTest method testCustomProviderWithRelMetadataQuery.

@Test
void testCustomProviderWithRelMetadataQuery() {
    final List<String> buf = new ArrayList<>();
    ColTypeImpl.THREAD_LIST.set(buf);
    final String sql = "select deptno, count(*) from emp where deptno > 10 " + "group by deptno having count(*) = 0";
    final RelMetadataFixture.MetadataConfig metadataConfig = fixture().metadataConfig;
    final RelMetadataFixture fixture = sql(sql).withMetadataConfig(RelMetadataFixture.MetadataConfig.NOP).withCluster(cluster -> {
        // Create a custom provider that includes ColType.
        // Include the same provider twice just to be devious.
        final ImmutableList<RelMetadataProvider> list = ImmutableList.of(ColTypeImpl.SOURCE, ColTypeImpl.SOURCE, requireNonNull(cluster.getMetadataProvider(), "cluster.metadataProvider"));
        metadataConfig.applyMetadata(cluster, ChainedRelMetadataProvider.of(list), MyRelMetadataQuery::new);
        return cluster;
    });
    final RelNode rel = fixture.toRel();
    // Top node is a filter. Its metadata uses getColType(RelNode, int).
    assertThat(rel, instanceOf(LogicalFilter.class));
    assertThat(rel.getCluster().getMetadataQuery(), instanceOf(MyRelMetadataQuery.class));
    final MyRelMetadataQuery mq = (MyRelMetadataQuery) rel.getCluster().getMetadataQuery();
    assertThat(colType(mq, rel, 0), equalTo("DEPTNO-rel"));
    assertThat(colType(mq, rel, 1), equalTo("EXPR$1-rel"));
    // Next node is an aggregate. Its metadata uses
    // getColType(LogicalAggregate, int).
    final RelNode input = rel.getInput(0);
    assertThat(input, instanceOf(LogicalAggregate.class));
    assertThat(colType(mq, input, 0), equalTo("DEPTNO-agg"));
    if (metadataConfig.isCaching()) {
        // The metadata query is caching, only the first request for each piece of metadata
        // generates a new call to the provider.
        assertThat(buf.toString(), equalTo("[DEPTNO-rel, EXPR$1-rel, DEPTNO-agg]"));
        assertThat(buf.size(), equalTo(3));
        assertThat(colType(mq, input, 0), equalTo("DEPTNO-agg"));
        assertThat(buf.size(), equalTo(3));
        assertThat(colType(mq, input, 0), equalTo("DEPTNO-agg"));
        assertThat(buf.size(), equalTo(3));
        assertThat(colType(mq, input, 1), equalTo("EXPR$1-agg"));
        assertThat(buf.size(), equalTo(4));
        assertThat(colType(mq, input, 1), equalTo("EXPR$1-agg"));
        assertThat(buf.size(), equalTo(4));
        assertThat(colType(mq, input, 0), equalTo("DEPTNO-agg"));
        assertThat(buf.size(), equalTo(4));
    }
    // Invalidate the metadata query triggers clearing of all the metadata.
    rel.getCluster().invalidateMetadataQuery();
    assertThat(rel.getCluster().getMetadataQuery(), instanceOf(MyRelMetadataQuery.class));
    final MyRelMetadataQuery mq1 = (MyRelMetadataQuery) rel.getCluster().getMetadataQuery();
    assertThat(colType(mq1, input, 0), equalTo("DEPTNO-agg"));
    if (metadataConfig.isCaching()) {
        assertThat(buf.size(), equalTo(5));
    }
    assertThat(colType(mq1, input, 0), equalTo("DEPTNO-agg"));
    if (metadataConfig.isCaching()) {
        assertThat(buf.size(), equalTo(5));
    }
    // Resets the RelMetadataQuery to default.
    metadataConfig.applyMetadata(rel.getCluster());
}
Also used : LogicalAggregate(org.apache.calcite.rel.logical.LogicalAggregate) RelNode(org.apache.calcite.rel.RelNode) LogicalFilter(org.apache.calcite.rel.logical.LogicalFilter) ArrayList(java.util.ArrayList) 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) RelMetadataProvider(org.apache.calcite.rel.metadata.RelMetadataProvider) Test(org.junit.jupiter.api.Test)

Aggregations

RelMetadataProvider (org.apache.calcite.rel.metadata.RelMetadataProvider)11 RelNode (org.apache.calcite.rel.RelNode)10 ChainedRelMetadataProvider (org.apache.calcite.rel.metadata.ChainedRelMetadataProvider)9 DefaultRelMetadataProvider (org.apache.calcite.rel.metadata.DefaultRelMetadataProvider)9 HepPlanner (org.apache.calcite.plan.hep.HepPlanner)6 RelOptCluster (org.apache.calcite.plan.RelOptCluster)5 JaninoRelMetadataProvider (org.apache.calcite.rel.metadata.JaninoRelMetadataProvider)4 ReflectiveRelMetadataProvider (org.apache.calcite.rel.metadata.ReflectiveRelMetadataProvider)4 RelMetadataQuery (org.apache.calcite.rel.metadata.RelMetadataQuery)4 ArrayList (java.util.ArrayList)3 RelOptPlanner (org.apache.calcite.plan.RelOptPlanner)3 HepProgram (org.apache.calcite.plan.hep.HepProgram)3 HepProgramBuilder (org.apache.calcite.plan.hep.HepProgramBuilder)3 RelRoot (org.apache.calcite.rel.RelRoot)3 LogicalAggregate (org.apache.calcite.rel.logical.LogicalAggregate)3 LogicalFilter (org.apache.calcite.rel.logical.LogicalFilter)3 CachingRelMetadataProvider (org.apache.calcite.rel.metadata.CachingRelMetadataProvider)3 Test (org.junit.jupiter.api.Test)3 UnboundMetadata (org.apache.calcite.rel.metadata.UnboundMetadata)2 RelBuilder (org.apache.calcite.tools.RelBuilder)2