Search in sources :

Example 6 with RexTableInputRef

use of org.apache.calcite.rex.RexTableInputRef in project calcite by apache.

the class RelMetadataTest method testAllPredicatesAggregate2.

@Test
public 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 = 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 7 with RexTableInputRef

use of org.apache.calcite.rex.RexTableInputRef in project calcite by apache.

the class RelMetadataTest method testExpressionLineageTwoColumns.

@Test
public void testExpressionLineageTwoColumns() {
    // mgr is column 3 in catalog.sales.emp
    // deptno is column 7 in catalog.sales.emp
    final RelNode rel = convertSql("select mgr, deptno from emp");
    final RelMetadataQuery mq = RelMetadataQuery.instance();
    final RexNode ref1 = RexInputRef.of(0, rel.getRowType().getFieldList());
    final Set<RexNode> r1 = mq.getExpressionLineage(rel, ref1);
    assertThat(r1.size(), is(1));
    final RexTableInputRef result1 = (RexTableInputRef) r1.iterator().next();
    assertTrue(result1.getQualifiedName().equals(EMP_QNAME));
    assertThat(result1.getIndex(), is(3));
    final RexNode ref2 = RexInputRef.of(1, rel.getRowType().getFieldList());
    final Set<RexNode> r2 = mq.getExpressionLineage(rel, ref2);
    assertThat(r2.size(), is(1));
    final RexTableInputRef result2 = (RexTableInputRef) r2.iterator().next();
    assertTrue(result2.getQualifiedName().equals(EMP_QNAME));
    assertThat(result2.getIndex(), is(7));
    assertThat(result1.getIdentifier(), is(result2.getIdentifier()));
}
Also used : 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 8 with RexTableInputRef

use of org.apache.calcite.rex.RexTableInputRef in project calcite by apache.

the class RelMetadataTest method testExpressionLineageUnion.

@Test
public void testExpressionLineageUnion() {
    // sal is column 5 in catalog.sales.emp
    final RelNode rel = convertSql("select sal from (\n" + "  select * from emp union all select * from emp) " + "where deptno = 10");
    final RelNode tableRel = convertSql("select * from emp");
    final RelMetadataQuery mq = RelMetadataQuery.instance();
    final RexNode ref = RexInputRef.of(0, rel.getRowType().getFieldList());
    final Set<RexNode> r = mq.getExpressionLineage(rel, ref);
    final String inputRef = RexInputRef.of(5, tableRel.getRowType().getFieldList()).toString();
    assertThat(r.size(), is(2));
    for (RexNode result : r) {
        final String resultString = result.toString();
        assertThat(resultString, startsWith(EMP_QNAME.toString()));
        assertThat(resultString, endsWith(inputRef));
    }
    Iterator<RexNode> it = r.iterator();
    assertThat(((RexTableInputRef) it.next()).getIdentifier(), not(((RexTableInputRef) it.next()).getIdentifier()));
}
Also used : 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 9 with RexTableInputRef

use of org.apache.calcite.rex.RexTableInputRef in project calcite by apache.

the class RelMetadataTest method testExpressionLineageSelfJoin.

@Test
public void testExpressionLineageSelfJoin() {
    // deptno is column 7 in catalog.sales.emp
    // sal is column 5 in catalog.sales.emp
    final RelNode rel = convertSql("select a.deptno, b.sal from (select * from emp limit 7) as a\n" + "inner join (select * from emp limit 2) as b\n" + "on a.deptno = b.deptno");
    final RelNode tableRel = convertSql("select * from emp");
    final RelMetadataQuery mq = RelMetadataQuery.instance();
    final RexNode ref1 = RexInputRef.of(0, rel.getRowType().getFieldList());
    final Set<RexNode> r1 = mq.getExpressionLineage(rel, ref1);
    final String inputRef1 = RexInputRef.of(7, tableRel.getRowType().getFieldList()).toString();
    assertThat(r1.size(), is(1));
    final String resultString1 = r1.iterator().next().toString();
    assertThat(resultString1, startsWith(EMP_QNAME.toString()));
    assertThat(resultString1, endsWith(inputRef1));
    final RexNode ref2 = RexInputRef.of(1, rel.getRowType().getFieldList());
    final Set<RexNode> r2 = mq.getExpressionLineage(rel, ref2);
    final String inputRef2 = RexInputRef.of(5, tableRel.getRowType().getFieldList()).toString();
    assertThat(r2.size(), is(1));
    final String resultString2 = r2.iterator().next().toString();
    assertThat(resultString2, startsWith(EMP_QNAME.toString()));
    assertThat(resultString2, endsWith(inputRef2));
    assertThat(((RexTableInputRef) r1.iterator().next()).getIdentifier(), not(((RexTableInputRef) r2.iterator().next()).getIdentifier()));
}
Also used : 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 10 with RexTableInputRef

use of org.apache.calcite.rex.RexTableInputRef in project calcite by apache.

the class RelMetadataTest method testExpressionLineageInnerJoinLeft.

@Test
public void testExpressionLineageInnerJoinLeft() {
    // ename is column 1 in catalog.sales.emp
    final RelNode rel = convertSql("select ename from emp,dept");
    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 RexTableInputRef result = (RexTableInputRef) r.iterator().next();
    assertTrue(result.getQualifiedName().equals(EMP_QNAME));
    assertThat(result.getIndex(), is(1));
}
Also used : 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)

Aggregations

RexTableInputRef (org.apache.calcite.rex.RexTableInputRef)24 RexNode (org.apache.calcite.rex.RexNode)20 RelNode (org.apache.calcite.rel.RelNode)16 RelMetadataQuery (org.apache.calcite.rel.metadata.RelMetadataQuery)16 RexCall (org.apache.calcite.rex.RexCall)10 Test (org.junit.Test)10 ArrayList (java.util.ArrayList)9 RelReferentialConstraint (org.apache.calcite.rel.RelReferentialConstraint)7 List (java.util.List)6 RelOptPredicateList (org.apache.calcite.plan.RelOptPredicateList)6 RexBuilder (org.apache.calcite.rex.RexBuilder)6 ImmutableList (com.google.common.collect.ImmutableList)5 HashMap (java.util.HashMap)5 HashSet (java.util.HashSet)5 RexInputRef (org.apache.calcite.rex.RexInputRef)5 RelTableRef (org.apache.calcite.rex.RexTableInputRef.RelTableRef)5 ImmutableBitSet (org.apache.calcite.util.ImmutableBitSet)5 LinkedHashSet (java.util.LinkedHashSet)4 RelBuilder (org.apache.calcite.tools.RelBuilder)4 RelOptHiveTable (org.apache.hadoop.hive.ql.optimizer.calcite.RelOptHiveTable)4