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