use of org.apache.calcite.rel.metadata.RelMetadataQuery in project calcite by apache.
the class RelMetadataTest method checkAllPredicates.
private void checkAllPredicates(RelOptCluster cluster, RelOptTable empTable, RelOptTable deptTable) {
final RelBuilder relBuilder = RelBuilder.proto().create(cluster, null);
final RelMetadataQuery mq = RelMetadataQuery.instance();
final LogicalTableScan empScan = LogicalTableScan.create(cluster, empTable);
relBuilder.push(empScan);
RelOptPredicateList predicates = mq.getAllPredicates(empScan);
assertThat(predicates.pulledUpPredicates.isEmpty(), is(true));
relBuilder.filter(relBuilder.equals(relBuilder.field("EMPNO"), relBuilder.literal(BigDecimal.ONE)));
final RelNode filter = relBuilder.peek();
predicates = mq.getAllPredicates(filter);
assertThat(predicates.pulledUpPredicates.size(), is(1));
RexCall call = (RexCall) predicates.pulledUpPredicates.get(0);
assertThat(call.getOperands().size(), is(2));
RexTableInputRef inputRef1 = (RexTableInputRef) call.getOperands().get(0);
assertTrue(inputRef1.getQualifiedName().equals(EMP_QNAME));
assertThat(inputRef1.getIndex(), is(0));
final LogicalTableScan deptScan = LogicalTableScan.create(cluster, deptTable);
relBuilder.push(deptScan);
relBuilder.join(JoinRelType.INNER, relBuilder.equals(relBuilder.field(2, 0, "DEPTNO"), relBuilder.field(2, 1, "DEPTNO")));
relBuilder.project(relBuilder.field("DEPTNO"));
final RelNode project = relBuilder.peek();
predicates = mq.getAllPredicates(project);
assertThat(predicates.pulledUpPredicates.size(), is(2));
// From Filter
call = (RexCall) predicates.pulledUpPredicates.get(0);
assertThat(call.getOperands().size(), is(2));
inputRef1 = (RexTableInputRef) call.getOperands().get(0);
assertTrue(inputRef1.getQualifiedName().equals(EMP_QNAME));
assertThat(inputRef1.getIndex(), is(0));
// From Join
call = (RexCall) predicates.pulledUpPredicates.get(1);
assertThat(call.getOperands().size(), is(2));
inputRef1 = (RexTableInputRef) call.getOperands().get(0);
assertTrue(inputRef1.getQualifiedName().equals(EMP_QNAME));
assertThat(inputRef1.getIndex(), is(7));
RexTableInputRef inputRef2 = (RexTableInputRef) call.getOperands().get(1);
assertTrue(inputRef2.getQualifiedName().equals(ImmutableList.of("CATALOG", "SALES", "DEPT")));
assertThat(inputRef2.getIndex(), is(0));
}
use of org.apache.calcite.rel.metadata.RelMetadataQuery in project calcite by apache.
the class RelMetadataTest method checkPredicates.
private void checkPredicates(RelOptCluster cluster, RelOptTable empTable, RelOptTable deptTable) {
final RelBuilder relBuilder = RelBuilder.proto().create(cluster, null);
final RelMetadataQuery mq = RelMetadataQuery.instance();
final LogicalTableScan empScan = LogicalTableScan.create(cluster, empTable);
relBuilder.push(empScan);
RelOptPredicateList predicates = mq.getPulledUpPredicates(empScan);
assertThat(predicates.pulledUpPredicates.isEmpty(), is(true));
relBuilder.filter(relBuilder.equals(relBuilder.field("EMPNO"), relBuilder.literal(BigDecimal.ONE)));
final RelNode filter = relBuilder.peek();
predicates = mq.getPulledUpPredicates(filter);
assertThat(predicates.pulledUpPredicates.toString(), is("[=($0, 1)]"));
final LogicalTableScan deptScan = LogicalTableScan.create(cluster, deptTable);
relBuilder.push(deptScan);
relBuilder.semiJoin(relBuilder.equals(relBuilder.field(2, 0, "DEPTNO"), relBuilder.field(2, 1, "DEPTNO")));
final SemiJoin semiJoin = (SemiJoin) relBuilder.build();
predicates = mq.getPulledUpPredicates(semiJoin);
assertThat(predicates.pulledUpPredicates, sortsAs("[=($0, 1)]"));
assertThat(predicates.leftInferredPredicates, sortsAs("[]"));
assertThat(predicates.rightInferredPredicates.isEmpty(), is(true));
// Create a Join similar to the previous SemiJoin
relBuilder.push(filter);
relBuilder.push(deptScan);
relBuilder.join(JoinRelType.INNER, relBuilder.equals(relBuilder.field(2, 0, "DEPTNO"), relBuilder.field(2, 1, "DEPTNO")));
relBuilder.project(relBuilder.field("DEPTNO"));
final RelNode project = relBuilder.peek();
predicates = mq.getPulledUpPredicates(project);
// No inferred predicates, because we already know DEPTNO is NOT NULL
assertThat(predicates.pulledUpPredicates, sortsAs("[]"));
assertThat(project.getRowType().getFullTypeString(), is("RecordType(INTEGER NOT NULL DEPTNO) NOT NULL"));
assertThat(predicates.leftInferredPredicates.isEmpty(), is(true));
assertThat(predicates.rightInferredPredicates.isEmpty(), is(true));
// Create a Join similar to the previous Join, but joining on MGR, which
// is nullable. From the join condition "e.MGR = d.DEPTNO" we can deduce
// the projected predicate "IS NOT NULL($0)".
relBuilder.push(filter);
relBuilder.push(deptScan);
relBuilder.join(JoinRelType.INNER, relBuilder.equals(relBuilder.field(2, 0, "MGR"), relBuilder.field(2, 1, "DEPTNO")));
relBuilder.project(relBuilder.field("MGR"));
final RelNode project2 = relBuilder.peek();
predicates = mq.getPulledUpPredicates(project2);
assertThat(predicates.pulledUpPredicates, sortsAs("[IS NOT NULL($0)]"));
assertThat(predicates.leftInferredPredicates.isEmpty(), is(true));
assertThat(predicates.rightInferredPredicates.isEmpty(), is(true));
// Create another similar Join. From the join condition
// e.MGR - e.EMPNO = d.DEPTNO + e.MGR_COMM
// we can deduce the projected predicate
// MGR IS NOT NULL OR MGR_COMM IS NOT NULL
//
// EMPNO is omitted because it is NOT NULL.
// MGR_COMM is a made-up nullable field.
relBuilder.push(filter);
relBuilder.project(Iterables.concat(relBuilder.fields(), ImmutableList.of(relBuilder.alias(relBuilder.call(SqlStdOperatorTable.PLUS, relBuilder.field("MGR"), relBuilder.field("COMM")), "MGR_COMM"))));
relBuilder.push(deptScan);
relBuilder.join(JoinRelType.INNER, relBuilder.equals(relBuilder.call(SqlStdOperatorTable.MINUS, relBuilder.field(2, 0, "MGR"), relBuilder.field(2, 0, "EMPNO")), relBuilder.call(SqlStdOperatorTable.PLUS, relBuilder.field(2, 1, "DEPTNO"), relBuilder.field(2, 0, "MGR_COMM"))));
relBuilder.project(relBuilder.field("MGR"), relBuilder.field("NAME"), relBuilder.field("MGR_COMM"), relBuilder.field("COMM"));
final RelNode project3 = relBuilder.peek();
predicates = mq.getPulledUpPredicates(project3);
assertThat(predicates.pulledUpPredicates, sortsAs("[OR(IS NOT NULL($0), IS NOT NULL($2))]"));
assertThat(predicates.leftInferredPredicates.isEmpty(), is(true));
assertThat(predicates.rightInferredPredicates.isEmpty(), is(true));
}
use of org.apache.calcite.rel.metadata.RelMetadataQuery in project calcite by apache.
the class RelMetadataTest method testCustomProvider.
@Test
public void testCustomProvider() {
final List<String> buf = Lists.newArrayList();
ColTypeImpl.THREAD_LIST.set(buf);
final String sql = "select deptno, count(*) from emp where deptno > 10 " + "group by deptno having count(*) = 0";
final RelRoot root = tester.withClusterFactory(new Function<RelOptCluster, RelOptCluster>() {
public RelOptCluster apply(RelOptCluster 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, cluster.getMetadataProvider());
cluster.setMetadataProvider(ChainedRelMetadataProvider.of(list));
return cluster;
}
}).convertSqlToRel(sql);
final RelNode rel = root.rel;
// Top node is a filter. Its metadata uses getColType(RelNode, int).
assertThat(rel, instanceOf(LogicalFilter.class));
final RelMetadataQuery mq = RelMetadataQuery.instance();
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"));
// There is no caching. Another request causes another 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(4));
// Now add a cache. Only the first request for each piece of metadata
// generates a new call to the provider.
final RelOptPlanner planner = rel.getCluster().getPlanner();
rel.getCluster().setMetadataProvider(new CachingRelMetadataProvider(rel.getCluster().getMetadataProvider(), planner));
assertThat(colType(mq, input, 0), equalTo("DEPTNO-agg"));
assertThat(buf.size(), equalTo(5));
assertThat(colType(mq, input, 0), equalTo("DEPTNO-agg"));
assertThat(buf.size(), equalTo(5));
assertThat(colType(mq, input, 1), equalTo("EXPR$1-agg"));
assertThat(buf.size(), equalTo(6));
assertThat(colType(mq, input, 1), equalTo("EXPR$1-agg"));
assertThat(buf.size(), equalTo(6));
assertThat(colType(mq, input, 0), equalTo("DEPTNO-agg"));
assertThat(buf.size(), equalTo(6));
// With a different timestamp, a metadata item is re-computed on first call.
long timestamp = planner.getRelMetadataTimestamp(rel);
assertThat(timestamp, equalTo(0L));
((MockRelOptPlanner) planner).setRelMetadataTimestamp(timestamp + 1);
assertThat(colType(mq, input, 0), equalTo("DEPTNO-agg"));
assertThat(buf.size(), equalTo(7));
assertThat(colType(mq, input, 0), equalTo("DEPTNO-agg"));
assertThat(buf.size(), equalTo(7));
}
use of org.apache.calcite.rel.metadata.RelMetadataQuery in project calcite by apache.
the class RelMetadataTest method checkColumnOrigin.
// ----------------------------------------------------------------------
// Tests for getColumnOrigins
// ----------------------------------------------------------------------
private Set<RelColumnOrigin> checkColumnOrigin(String sql) {
RelNode rel = convertSql(sql);
final RelMetadataQuery mq = RelMetadataQuery.instance();
return mq.getColumnOrigins(rel, 0);
}
use of org.apache.calcite.rel.metadata.RelMetadataQuery in project calcite by apache.
the class RelMetadataTest method testGetPredicatesForJoin.
@Test
public void testGetPredicatesForJoin() throws Exception {
final FrameworkConfig config = RelBuilderTest.config().build();
final RelBuilder builder = RelBuilder.create(config);
RelNode join = builder.scan("EMP").scan("DEPT").join(JoinRelType.INNER, builder.call(NONDETERMINISTIC_OP)).build();
RelMetadataQuery mq = RelMetadataQuery.instance();
assertTrue(mq.getPulledUpPredicates(join).pulledUpPredicates.isEmpty());
RelNode join1 = builder.scan("EMP").scan("DEPT").join(JoinRelType.INNER, builder.call(SqlStdOperatorTable.EQUALS, builder.field(2, 0, 0), builder.field(2, 1, 0))).build();
assertEquals("=($0, $8)", mq.getPulledUpPredicates(join1).pulledUpPredicates.get(0).toString());
}
Aggregations