use of org.apache.calcite.rel.metadata.RelMetadataQuery in project calcite by apache.
the class RelMetadataTest method testAllPredicatesCrossJoinMultiTable.
@Test
public void testAllPredicatesCrossJoinMultiTable() {
final String sql = "select x.sal from\n" + "(select a.deptno, c.sal from (select * from emp limit 7) as a\n" + "cross join (select * from dept limit 1) as b\n" + "cross join (select * from emp where empno = 5 limit 2) as c) as x";
final RelNode rel = convertSql(sql);
final RelMetadataQuery mq = RelMetadataQuery.instance();
final Set<RelTableRef> tableReferences = Sets.newTreeSet(mq.getTableReferences(rel));
assertThat(tableReferences.toString(), equalTo("[[CATALOG, SALES, DEPT].#0, " + "[CATALOG, SALES, EMP].#0, " + "[CATALOG, SALES, EMP].#1]"));
final RelOptPredicateList inputSet = mq.getAllPredicates(rel);
// Note that we reference [CATALOG, SALES, EMP].#1 rather than [CATALOG, SALES, EMP].#0
assertThat(inputSet.pulledUpPredicates.toString(), equalTo("[true, =([CATALOG, SALES, EMP].#1.$0, 5), true]"));
}
use of org.apache.calcite.rel.metadata.RelMetadataQuery in project calcite by apache.
the class RelMetadataTest method testAllPredicatesAndTablesJoin.
@Test
public void testAllPredicatesAndTablesJoin() {
final String sql = "select x.sal, y.deptno from\n" + "(select a.deptno, c.sal from (select * from emp limit 7) as a\n" + "cross join (select * from dept limit 1) as b\n" + "inner join (select * from emp limit 2) as c\n" + "on a.deptno = c.deptno) as x\n" + "inner join\n" + "(select a.deptno, c.sal from (select * from emp limit 7) as a\n" + "cross join (select * from dept limit 1) as b\n" + "inner join (select * from emp limit 2) as c\n" + "on a.deptno = c.deptno) as y\n" + "on x.deptno = y.deptno";
final RelNode rel = convertSql(sql);
final RelMetadataQuery mq = RelMetadataQuery.instance();
final RelOptPredicateList inputSet = mq.getAllPredicates(rel);
assertThat(inputSet.pulledUpPredicates.toString(), equalTo("[true, " + "=([CATALOG, SALES, EMP].#0.$7, [CATALOG, SALES, EMP].#1.$7), " + "true, " + "=([CATALOG, SALES, EMP].#2.$7, [CATALOG, SALES, EMP].#3.$7), " + "=([CATALOG, SALES, EMP].#0.$7, [CATALOG, SALES, EMP].#2.$7)]"));
final Set<RelTableRef> tableReferences = Sets.newTreeSet(mq.getTableReferences(rel));
assertThat(tableReferences.toString(), equalTo("[[CATALOG, SALES, DEPT].#0, [CATALOG, SALES, DEPT].#1, " + "[CATALOG, SALES, EMP].#0, [CATALOG, SALES, EMP].#1, " + "[CATALOG, SALES, EMP].#2, [CATALOG, SALES, EMP].#3]"));
}
use of org.apache.calcite.rel.metadata.RelMetadataQuery in project calcite by apache.
the class RelMetadataTest method testAllPredicatesAggregate1.
@Test
public void testAllPredicatesAggregate1() {
final String sql = "select a, max(b) from (\n" + " select empno as a, sal as b from emp where empno = 5)subq\n" + "group by a";
final RelNode rel = convertSql(sql);
final RelMetadataQuery mq = RelMetadataQuery.instance();
RelOptPredicateList inputSet = mq.getAllPredicates(rel);
ImmutableList<RexNode> pulledUpPredicates = inputSet.pulledUpPredicates;
assertThat(pulledUpPredicates.size(), is(1));
RexCall call = (RexCall) pulledUpPredicates.get(0);
assertThat(call.getOperands().size(), is(2));
final RexTableInputRef inputRef1 = (RexTableInputRef) call.getOperands().get(0);
assertTrue(inputRef1.getQualifiedName().equals(EMP_QNAME));
assertThat(inputRef1.getIndex(), is(0));
final RexLiteral constant = (RexLiteral) call.getOperands().get(1);
assertThat(constant.toString(), is("5"));
}
use of org.apache.calcite.rel.metadata.RelMetadataQuery in project calcite by apache.
the class RelMetadataTest method testExpressionLineageCombineTwoColumns.
@Test
public void testExpressionLineageCombineTwoColumns() {
// empno is column 0 in catalog.sales.emp
// deptno is column 7 in catalog.sales.emp
final RelNode rel = convertSql("select empno + deptno from emp");
final RelMetadataQuery mq = RelMetadataQuery.instance();
final RexNode ref = RexInputRef.of(0, rel.getRowType().getFieldList());
final Set<RexNode> r = mq.getExpressionLineage(rel, ref);
assertThat(r.size(), is(1));
final RexNode result = r.iterator().next();
assertThat(result.getKind(), is(SqlKind.PLUS));
final RexCall call = (RexCall) result;
assertThat(call.getOperands().size(), is(2));
final RexTableInputRef inputRef1 = (RexTableInputRef) call.getOperands().get(0);
assertTrue(inputRef1.getQualifiedName().equals(EMP_QNAME));
assertThat(inputRef1.getIndex(), is(0));
final RexTableInputRef inputRef2 = (RexTableInputRef) call.getOperands().get(1);
assertTrue(inputRef2.getQualifiedName().equals(EMP_QNAME));
assertThat(inputRef2.getIndex(), is(7));
assertThat(inputRef1.getIdentifier(), is(inputRef2.getIdentifier()));
}
use of org.apache.calcite.rel.metadata.RelMetadataQuery in project calcite by apache.
the class RelMetadataTest method testAllPredicatesAndTableUnion.
@Test
public void testAllPredicatesAndTableUnion() {
final String sql = "select a.deptno, c.sal from (select * from emp limit 7) as a\n" + "cross join (select * from dept limit 1) as b\n" + "inner join (select * from emp limit 2) as c\n" + "on a.deptno = c.deptno\n" + "union all\n" + "select a.deptno, c.sal from (select * from emp limit 7) as a\n" + "cross join (select * from dept limit 1) as b\n" + "inner join (select * from emp limit 2) as c\n" + "on a.deptno = c.deptno";
final RelNode rel = convertSql(sql);
final RelMetadataQuery mq = RelMetadataQuery.instance();
final RelOptPredicateList inputSet = mq.getAllPredicates(rel);
assertThat(inputSet.pulledUpPredicates.toString(), equalTo("[true, " + "=([CATALOG, SALES, EMP].#0.$7, [CATALOG, SALES, EMP].#1.$7), " + "true, " + "=([CATALOG, SALES, EMP].#2.$7, [CATALOG, SALES, EMP].#3.$7)]"));
final Set<RelTableRef> tableReferences = Sets.newTreeSet(mq.getTableReferences(rel));
assertThat(tableReferences.toString(), equalTo("[[CATALOG, SALES, DEPT].#0, [CATALOG, SALES, DEPT].#1, " + "[CATALOG, SALES, EMP].#0, [CATALOG, SALES, EMP].#1, " + "[CATALOG, SALES, EMP].#2, [CATALOG, SALES, EMP].#3]"));
}
Aggregations