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"));
}
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()));
}
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));
}
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;
}
}
Aggregations