use of org.apache.calcite.rex.RexTableInputRef.RelTableRef in project calcite by apache.
the class RelMdAllPredicates method getAllPredicates.
/**
* Extract predicates for a Union.
*/
public RelOptPredicateList getAllPredicates(Union union, RelMetadataQuery mq) {
final RexBuilder rexBuilder = union.getCluster().getRexBuilder();
final Multimap<List<String>, RelTableRef> qualifiedNamesToRefs = HashMultimap.create();
RelOptPredicateList newPreds = RelOptPredicateList.EMPTY;
for (int i = 0; i < union.getInputs().size(); i++) {
final RelNode input = union.getInput(i);
final RelOptPredicateList inputPreds = mq.getAllPredicates(input);
if (inputPreds == null) {
// Bail out
return null;
}
// Gather table references
final Set<RelTableRef> tableRefs = mq.getTableReferences(input);
if (i == 0) {
// Left input references remain unchanged
for (RelTableRef leftRef : tableRefs) {
qualifiedNamesToRefs.put(leftRef.getQualifiedName(), leftRef);
}
newPreds = newPreds.union(rexBuilder, inputPreds);
} else {
// Right input references might need to be updated if there are table name
// clashes with left input
final Map<RelTableRef, RelTableRef> currentTablesMapping = new HashMap<>();
for (RelTableRef rightRef : tableRefs) {
int shift = 0;
Collection<RelTableRef> lRefs = qualifiedNamesToRefs.get(rightRef.getQualifiedName());
if (lRefs != null) {
shift = lRefs.size();
}
currentTablesMapping.put(rightRef, RelTableRef.of(rightRef.getTable(), shift + rightRef.getEntityNumber()));
}
// Add to existing qualified names
for (RelTableRef newRef : currentTablesMapping.values()) {
qualifiedNamesToRefs.put(newRef.getQualifiedName(), newRef);
}
// Update preds
final List<RexNode> updatedPreds = Lists.newArrayList(Iterables.transform(inputPreds.pulledUpPredicates, new Function<RexNode, RexNode>() {
@Override
public RexNode apply(RexNode e) {
return RexUtil.swapTableReferences(rexBuilder, e, currentTablesMapping);
}
}));
newPreds = newPreds.union(rexBuilder, RelOptPredicateList.of(rexBuilder, updatedPreds));
}
}
return newPreds;
}
use of org.apache.calcite.rex.RexTableInputRef.RelTableRef in project calcite by apache.
the class RelMetadataTest method testAllPredicatesUnionMultiTable.
@Test
public 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 = RelMetadataQuery.instance();
final Set<RelTableRef> tableReferences = Sets.newTreeSet(mq.getTableReferences(rel));
assertThat(tableReferences.toString(), equalTo("[[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.toString(), equalTo("[=([CATALOG, SALES, EMP].#2.$0, 5)]"));
}
use of org.apache.calcite.rex.RexTableInputRef.RelTableRef 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]"));
}
use of org.apache.calcite.rex.RexTableInputRef.RelTableRef 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]"));
}
use of org.apache.calcite.rex.RexTableInputRef.RelTableRef 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]"));
}
Aggregations