private ObjectPair<Integer, int[]> findMergePos(QBJoinTree node, QBJoinTree target) {
int res = -1;
String leftAlias = node.getLeftAlias();
if (leftAlias == null) {
return new ObjectPair(-1, null);
}
ArrayList<ASTNode> nodeCondn = node.getExpressions().get(0);
ArrayList<ASTNode> targetCondn = null;
if (leftAlias.equals(target.getLeftAlias())) {
targetCondn = target.getExpressions().get(0);
res = 0;
} else {
for (int i = 0; i < target.getRightAliases().length; i++) {
if (leftAlias.equals(target.getRightAliases()[i])) {
targetCondn = target.getExpressions().get(i + 1);
res = i + 1;
break;
}
}
}
if ( targetCondn == null ) {
return new ObjectPair(-1, null);
}
/*
* The order of the join condition expressions don't matter.
* A merge can happen:
* - if every target condition is present in some position of the node condition list.
* - there is no node condition, which is not equal to any target condition.
*/
int[] tgtToNodeExprMap = new int[targetCondn.size()];
boolean[] nodeFiltersMapped = new boolean[nodeCondn.size()];
int i, j;
for(i=0; i<targetCondn.size(); i++) {
String tgtExprTree = targetCondn.get(i).toStringTree();
tgtToNodeExprMap[i] = -1;
for(j=0; j < nodeCondn.size(); j++) {
if ( nodeCondn.get(j).toStringTree().equals(tgtExprTree)) {
tgtToNodeExprMap[i] = j;
nodeFiltersMapped[j] = true;
}
}
if ( tgtToNodeExprMap[i] == -1) {
return new ObjectPair(-1, null);
}
}
for(j=0; j < nodeCondn.size(); j++) {
if ( !nodeFiltersMapped[j]) {
return new ObjectPair(-1, null);
}
}
return new ObjectPair(res, tgtToNodeExprMap);
}