Search in sources :

Example 21 with RexTableInputRef

use of org.apache.calcite.rex.RexTableInputRef 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 22 with RexTableInputRef

use of org.apache.calcite.rex.RexTableInputRef 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 23 with RexTableInputRef

use of org.apache.calcite.rex.RexTableInputRef 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));
}
Also used : RexCall(org.apache.calcite.rex.RexCall) RelMetadataQuery(org.apache.calcite.rel.metadata.RelMetadataQuery) RelBuilder(org.apache.calcite.tools.RelBuilder) RelNode(org.apache.calcite.rel.RelNode) RelOptPredicateList(org.apache.calcite.plan.RelOptPredicateList) RexTableInputRef(org.apache.calcite.rex.RexTableInputRef) LogicalTableScan(org.apache.calcite.rel.logical.LogicalTableScan)

Example 24 with RexTableInputRef

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

the class AbstractMaterializedViewRule method shuttleReferences.

/**
 * Replaces all the possible subexpressions by input references
 * to the input node. If available, it uses the rewriting mapping
 * to change the position to reference. Takes the reference type
 * from the input node.
 */
private static RexNode shuttleReferences(final RexBuilder rexBuilder, final RexNode expr, final Multimap<String, Integer> exprsLineage, final RelNode node, final Mapping rewritingMapping) {
    try {
        RexShuttle visitor = new RexShuttle() {

            @Override
            public RexNode visitTableInputRef(RexTableInputRef ref) {
                Collection<Integer> c = exprsLineage.get(ref.toString());
                if (c.isEmpty()) {
                    // Cannot map expression
                    throw Util.FoundOne.NULL;
                }
                int pos = c.iterator().next();
                if (rewritingMapping != null) {
                    pos = rewritingMapping.getTargetOpt(pos);
                    if (pos == -1) {
                        // Cannot map expression
                        throw Util.FoundOne.NULL;
                    }
                }
                if (node != null) {
                    return rexBuilder.makeInputRef(node, pos);
                }
                return rexBuilder.makeInputRef(ref.getType(), pos);
            }

            @Override
            public RexNode visitInputRef(RexInputRef inputRef) {
                Collection<Integer> c = exprsLineage.get(inputRef.toString());
                if (c.isEmpty()) {
                    // Cannot map expression
                    throw Util.FoundOne.NULL;
                }
                int pos = c.iterator().next();
                if (rewritingMapping != null) {
                    pos = rewritingMapping.getTargetOpt(pos);
                    if (pos == -1) {
                        // Cannot map expression
                        throw Util.FoundOne.NULL;
                    }
                }
                if (node != null) {
                    return rexBuilder.makeInputRef(node, pos);
                }
                return rexBuilder.makeInputRef(inputRef.getType(), pos);
            }

            @Override
            public RexNode visitCall(final RexCall call) {
                Collection<Integer> c = exprsLineage.get(call.toString());
                if (c.isEmpty()) {
                    // Cannot map expression
                    return super.visitCall(call);
                }
                int pos = c.iterator().next();
                if (rewritingMapping != null) {
                    pos = rewritingMapping.getTargetOpt(pos);
                    if (pos == -1) {
                        // Cannot map expression
                        return super.visitCall(call);
                    }
                }
                if (node != null) {
                    return rexBuilder.makeInputRef(node, pos);
                }
                return rexBuilder.makeInputRef(call.getType(), pos);
            }
        };
        return visitor.apply(expr);
    } catch (Util.FoundOne ex) {
        Util.swallow(ex, null);
        return null;
    }
}
Also used : RexCall(org.apache.calcite.rex.RexCall) RexShuttle(org.apache.calcite.rex.RexShuttle) RexInputRef(org.apache.calcite.rex.RexInputRef) RexUtil(org.apache.calcite.rex.RexUtil) RelOptUtil(org.apache.calcite.plan.RelOptUtil) Util(org.apache.calcite.util.Util) RexTableInputRef(org.apache.calcite.rex.RexTableInputRef) RelReferentialConstraint(org.apache.calcite.rel.RelReferentialConstraint)

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