Java Code Examples for org.apache.calcite.rel.metadata.RelMetadataQuery#getAllPredicates()

The following examples show how to use org.apache.calcite.rel.metadata.RelMetadataQuery#getAllPredicates() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RelMetadataTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Test 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 = rel.getCluster().getMetadataQuery();
  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);
  assertThat(inputRef1.getQualifiedName(), is(EMP_QNAME));
  assertThat(inputRef1.getIndex(), is(0));
  final RexLiteral constant = (RexLiteral) call.getOperands().get(1);
  assertThat(constant.toString(), is("5"));
}
 
Example 2
Source File: RelMetadataTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Test void testAllPredicatesAggregate2() {
  final String sql = "select * from (select a, max(b) from (\n"
      + "  select empno as a, sal as b from emp)subq\n"
      + "group by a)\n"
      + "where a = 5";
  final RelNode rel = convertSql(sql);
  final RelMetadataQuery mq = rel.getCluster().getMetadataQuery();
  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"));
}
 
Example 3
Source File: RelMetadataTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Test 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 = rel.getCluster().getMetadataQuery();
  final RelOptPredicateList inputSet = mq.getAllPredicates(rel);
  assertThat(inputSet.pulledUpPredicates,
      sortsAs("[=([CATALOG, SALES, EMP].#0.$7, [CATALOG, SALES, EMP].#1.$7),"
          + " =([CATALOG, SALES, EMP].#2.$7, [CATALOG, SALES, EMP].#3.$7), "
          + "true, "
          + "true]"));
  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]"));
}
 
Example 4
Source File: RelMetadataTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Test 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 = rel.getCluster().getMetadataQuery();
  final Set<RelTableRef> tableReferences = Sets.newTreeSet(mq.getTableReferences(rel));
  assertThat(tableReferences,
      sortsAs("[[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,
      sortsAs("[=([CATALOG, SALES, EMP].#1.$0, 5), true, true]"));
}
 
Example 5
Source File: RelMetadataTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Test void testAllPredicatesUnionMultiTable() {
  final String sql = "select x.sal from\n"
      + "(select a.deptno, a.sal from (select * from emp) as a\n"
      + "union all select emp.deptno, emp.sal from emp\n"
      + "union all select emp.deptno, emp.sal from emp where empno = 5) as x";
  final RelNode rel = convertSql(sql);
  final RelMetadataQuery mq = rel.getCluster().getMetadataQuery();
  final Set<RelTableRef> tableReferences = Sets.newTreeSet(mq.getTableReferences(rel));
  assertThat(tableReferences,
      sortsAs("[[CATALOG, SALES, EMP].#0, "
          + "[CATALOG, SALES, EMP].#1, "
          + "[CATALOG, SALES, EMP].#2]"));
  // Note that we reference [CATALOG, SALES, EMP].#2 rather than
  // [CATALOG, SALES, EMP].#0 or [CATALOG, SALES, EMP].#1
  final RelOptPredicateList inputSet = mq.getAllPredicates(rel);
  assertThat(inputSet.pulledUpPredicates,
      sortsAs("[=([CATALOG, SALES, EMP].#2.$0, 5)]"));
}
 
Example 6
Source File: RelMetadataTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testAllPredicatesAggregate3() {
  final String sql = "select * from (select a, max(b) as b from (\n"
      + "  select empno as a, sal as b from emp)subq\n"
      + "group by a)\n"
      + "where b = 5";
  final RelNode rel = convertSql(sql);
  final RelMetadataQuery mq = rel.getCluster().getMetadataQuery();
  RelOptPredicateList inputSet = mq.getAllPredicates(rel);
  // Filter on aggregate, we cannot infer lineage
  assertNull(inputSet);
}
 
Example 7
Source File: RelMetadataTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test 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 = rel.getCluster().getMetadataQuery();
  final RelOptPredicateList inputSet = mq.getAllPredicates(rel);
  assertThat(inputSet.pulledUpPredicates,
      sortsAs("[=([CATALOG, SALES, EMP].#0.$7, [CATALOG, SALES, EMP].#1.$7), "
          + "=([CATALOG, SALES, EMP].#0.$7, [CATALOG, SALES, EMP].#2.$7), "
          + "=([CATALOG, SALES, EMP].#2.$7, [CATALOG, SALES, EMP].#3.$7), "
          + "true, "
          + "true]"));
  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]"));
}