use of org.apache.calcite.rex.RexVisitorImpl in project drill by apache.
the class DrillRelOptUtil method analyzeSimpleEquiJoin.
public static List<Pair<Integer, Integer>> analyzeSimpleEquiJoin(Join join) {
List<Pair<Integer, Integer>> joinConditions = new ArrayList<>();
try {
RexVisitor<Void> visitor = new RexVisitorImpl<Void>(true) {
@Override
public Void visitCall(RexCall call) {
if (call.getKind() == SqlKind.AND || call.getKind() == SqlKind.OR) {
super.visitCall(call);
} else {
if (call.getKind() == SqlKind.EQUALS) {
RexNode leftComparand = call.operands.get(0);
RexNode rightComparand = call.operands.get(1);
// we bail out!
if (!(leftComparand instanceof RexInputRef && rightComparand instanceof RexInputRef)) {
joinConditions.clear();
throw new Util.FoundOne(call);
}
int leftFieldCount = join.getLeft().getRowType().getFieldCount();
int rightFieldCount = join.getRight().getRowType().getFieldCount();
RexInputRef leftFieldAccess = (RexInputRef) leftComparand;
RexInputRef rightFieldAccess = (RexInputRef) rightComparand;
if (leftFieldAccess.getIndex() >= leftFieldCount + rightFieldCount || rightFieldAccess.getIndex() >= leftFieldCount + rightFieldCount) {
joinConditions.clear();
throw new Util.FoundOne(call);
}
/* Both columns reference same table */
if ((leftFieldAccess.getIndex() >= leftFieldCount && rightFieldAccess.getIndex() >= leftFieldCount) || (leftFieldAccess.getIndex() < leftFieldCount && rightFieldAccess.getIndex() < leftFieldCount)) {
joinConditions.clear();
throw new Util.FoundOne(call);
} else {
if (leftFieldAccess.getIndex() < leftFieldCount) {
joinConditions.add(Pair.of(leftFieldAccess.getIndex(), rightFieldAccess.getIndex() - leftFieldCount));
} else {
joinConditions.add(Pair.of(rightFieldAccess.getIndex(), leftFieldAccess.getIndex() - leftFieldCount));
}
}
}
}
return null;
}
};
join.getCondition().accept(visitor);
} catch (Util.FoundOne ex) {
Util.swallow(ex, null);
}
return joinConditions;
}
Aggregations