Search in sources :

Example 86 with RelMetadataQuery

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]"));
}
Also used : RelMetadataQuery(org.apache.calcite.rel.metadata.RelMetadataQuery) RelNode(org.apache.calcite.rel.RelNode) RelOptPredicateList(org.apache.calcite.plan.RelOptPredicateList) RelTableRef(org.apache.calcite.rex.RexTableInputRef.RelTableRef) Test(org.junit.Test)

Example 87 with RelMetadataQuery

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]"));
}
Also used : RelMetadataQuery(org.apache.calcite.rel.metadata.RelMetadataQuery) RelNode(org.apache.calcite.rel.RelNode) RelOptPredicateList(org.apache.calcite.plan.RelOptPredicateList) RelTableRef(org.apache.calcite.rex.RexTableInputRef.RelTableRef) Test(org.junit.Test)

Example 88 with RelMetadataQuery

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"));
}
Also used : RexCall(org.apache.calcite.rex.RexCall) RelMetadataQuery(org.apache.calcite.rel.metadata.RelMetadataQuery) RexLiteral(org.apache.calcite.rex.RexLiteral) RelNode(org.apache.calcite.rel.RelNode) RelOptPredicateList(org.apache.calcite.plan.RelOptPredicateList) RexTableInputRef(org.apache.calcite.rex.RexTableInputRef) RexNode(org.apache.calcite.rex.RexNode) Test(org.junit.Test)

Example 89 with RelMetadataQuery

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()));
}
Also used : RexCall(org.apache.calcite.rex.RexCall) RelMetadataQuery(org.apache.calcite.rel.metadata.RelMetadataQuery) RelNode(org.apache.calcite.rel.RelNode) RexTableInputRef(org.apache.calcite.rex.RexTableInputRef) RexNode(org.apache.calcite.rex.RexNode) Test(org.junit.Test)

Example 90 with RelMetadataQuery

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]"));
}
Also used : RelMetadataQuery(org.apache.calcite.rel.metadata.RelMetadataQuery) RelNode(org.apache.calcite.rel.RelNode) RelOptPredicateList(org.apache.calcite.plan.RelOptPredicateList) RelTableRef(org.apache.calcite.rex.RexTableInputRef.RelTableRef) Test(org.junit.Test)

Aggregations

RelMetadataQuery (org.apache.calcite.rel.metadata.RelMetadataQuery)122 RelNode (org.apache.calcite.rel.RelNode)88 Test (org.junit.Test)43 RexNode (org.apache.calcite.rex.RexNode)38 RelOptPredicateList (org.apache.calcite.plan.RelOptPredicateList)24 ImmutableBitSet (org.apache.calcite.util.ImmutableBitSet)21 ArrayList (java.util.ArrayList)19 RexBuilder (org.apache.calcite.rex.RexBuilder)19 RelBuilder (org.apache.calcite.tools.RelBuilder)19 ImmutableList (com.google.common.collect.ImmutableList)12 Aggregate (org.apache.calcite.rel.core.Aggregate)12 RexTableInputRef (org.apache.calcite.rex.RexTableInputRef)12 RelOptCluster (org.apache.calcite.plan.RelOptCluster)11 RelTraitSet (org.apache.calcite.plan.RelTraitSet)11 AggregateCall (org.apache.calcite.rel.core.AggregateCall)11 RelCollation (org.apache.calcite.rel.RelCollation)10 RelDistribution (org.apache.calcite.rel.RelDistribution)10 LogicalAggregate (org.apache.calcite.rel.logical.LogicalAggregate)10 HashMap (java.util.HashMap)9 List (java.util.List)9