in tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/physical/RightOuterMergeJoinExec.java [124:323]
public Tuple next() throws IOException {
Tuple previous;
for (;;) {
boolean newRound = false;
if((posRightTupleSlots == -1) && (posLeftTupleSlots == -1)) {
newRound = true;
}
if ((posRightTupleSlots == innerTupleSlots.size()) && (posLeftTupleSlots == leftTupleSlots.size())) {
newRound = true;
}
if (newRound) {
//////////////////////////////////////////////////////////////////////
// BEGIN FINALIZING STAGE
//////////////////////////////////////////////////////////////////////
// The finalizing stage, where remaining tuples on the only right are transformed into left-padded results
if (end) {
if (initRightDone == false) {
// maybe the left operand was empty => the right one didn't have the chance to initialize
rightTuple = rightChild.next();
initRightDone = true;
}
if(rightTuple == null) {
return null;
} else {
// output a tuple with the nulls padded leftTuple
Tuple nullPaddedTuple = createNullPaddedTuple(leftNumCols);
frameTuple.set(nullPaddedTuple, rightTuple);
projector.eval(frameTuple, outTuple);
// we simulate we found a match, which is exactly the null padded one
rightTuple = rightChild.next();
return outTuple;
}
}
//////////////////////////////////////////////////////////////////////
// END FINALIZING STAGE
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
// BEGIN INITIALIZATION STAGE
//////////////////////////////////////////////////////////////////////
// This stage reads the first tuple on each side
if (leftTuple == null) {
leftTuple = leftChild.next();
if (leftTuple == null) {
end = true;
continue;
}
}
if(rightTuple == null){
rightTuple = rightChild.next();
if(rightTuple != null){
initRightDone = true;
}
else {
initRightDone = true;
end = true;
continue;
}
}
//////////////////////////////////////////////////////////////////////
// END INITIALIZATION STAGE
//////////////////////////////////////////////////////////////////////
// reset tuple slots for a new round
leftTupleSlots.clear();
innerTupleSlots.clear();
posRightTupleSlots = -1;
posLeftTupleSlots = -1;
//////////////////////////////////////////////////////////////////////
// BEGIN MOVE FORWARDING STAGE
//////////////////////////////////////////////////////////////////////
// This stage moves forward a tuple cursor on each side relation until a match
// is found
int cmp;
while ((end != true) && ((cmp = joinComparator.compare(leftTuple, rightTuple)) != 0)) {
// if right is lower than the left tuple, it means that all right tuples s.t. cmp <= 0 are
// matched tuples.
if (cmp > 0) {
// before getting a new tuple from the right, a left null padded tuple should be built
// output a tuple with the nulls padded left tuple
Tuple nullPaddedTuple = createNullPaddedTuple(leftNumCols);
frameTuple.set(nullPaddedTuple, rightTuple);
projector.eval(frameTuple, outTuple);
// we simulate we found a match, which is exactly the null padded one
// BEFORE RETURN, MOVE FORWARD
rightTuple = rightChild.next();
if(rightTuple == null) {
end = true;
}
return outTuple;
} else if (cmp < 0) {
// If the left tuple is lower than the right tuple, just move forward the left tuple cursor.
leftTuple = leftChild.next();
if(leftTuple == null) {
end = true;
// in original algorithm we had return null ,
// but now we need to continue the end processing phase for remaining unprocessed right tuples
}
} // if (cmp<0)
} // while
//////////////////////////////////////////////////////////////////////
// END MOVE FORWARDING STAGE
//////////////////////////////////////////////////////////////////////
// once a match is found, retain all tuples with this key in tuple slots on each side
if(!end) {
endInPopulationStage = false;
boolean endOuter = false;
boolean endInner = false;
previous = new VTuple(leftTuple);
do {
leftTupleSlots.add(new VTuple(leftTuple));
leftTuple = leftChild.next();
if( leftTuple == null) {
endOuter = true;
}
} while ((endOuter != true) && (tupleComparator[0].compare(previous, leftTuple) == 0));
posLeftTupleSlots = 0;
previous = new VTuple(rightTuple);
do {
innerTupleSlots.add(new VTuple(rightTuple));
rightTuple = rightChild.next();
if(rightTuple == null) {
endInner = true;
}
} while ((endInner != true) && (tupleComparator[1].compare(previous, rightTuple) == 0) );
posRightTupleSlots = 0;
if ((endOuter == true) || (endInner == true)) {
end = true;
endInPopulationStage = true;
}
} // if end false
} // if newRound
// Now output result matching tuples from the slots
// if either we haven't reached end on neither side, or we did reach end on one(or both) sides
// but that happened in the slots population step (i.e. refers to next round)
if ((end == false) || ((end == true) && (endInPopulationStage == true))){
if(posLeftTupleSlots == 0){
nextLeft = new VTuple (leftTupleSlots.get(posLeftTupleSlots));
posLeftTupleSlots = posLeftTupleSlots + 1;
}
if(posRightTupleSlots <= (innerTupleSlots.size() -1)) {
Tuple aTuple = new VTuple(innerTupleSlots.get(posRightTupleSlots));
posRightTupleSlots = posRightTupleSlots + 1;
frameTuple.set(nextLeft, aTuple);
joinQual.eval(inSchema, frameTuple);
projector.eval(frameTuple, outTuple);
return outTuple;
} else {
// right (inner) slots reached end and should be rewind if there are still tuples in the outer slots
if(posLeftTupleSlots <= (leftTupleSlots.size() - 1)) {
//rewind the right slots position
posRightTupleSlots = 0;
Tuple aTuple = new VTuple(innerTupleSlots.get(posRightTupleSlots));
posRightTupleSlots = posRightTupleSlots + 1;
nextLeft = new VTuple (leftTupleSlots.get(posLeftTupleSlots));
posLeftTupleSlots = posLeftTupleSlots + 1;
frameTuple.set(nextLeft, aTuple);
joinQual.eval(inSchema, frameTuple);
projector.eval(frameTuple, outTuple);
return outTuple;
}
}
} // the second if end false
} // for
}