public Tuple next()

in tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/physical/MergeFullOuterJoinExec.java [105:312]


  public Tuple next() throws IOException {
    Tuple previous;

    for (;;) {
      boolean newRound = false;
      if((posRightTupleSlots == -1) && (posLeftTupleSlots == -1)) {
        newRound = true;
      }
      if ((posRightTupleSlots == rightTupleSlots.size()) && (posLeftTupleSlots == leftTupleSlots.size())) {
        newRound = true;
      }

      if(newRound == true){

        if (end) {

          ////////////////////////////////////////////////////////////////////////
          // FINALIZING STAGE
          ////////////////////////////////////////////////////////////////////////
          // the finalizing stage, where remaining tuples on the right are
          // transformed into left-padded results while tuples on the left
          // are transformed into right-padded results

          // before exit, a left-padded tuple should be built for all remaining
          // right side and a right-padded tuple should be built for all remaining
          // left side

          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((leftTuple == null) && (rightTuple == null)) {
            return null;
          }

          if((leftTuple == null) && (rightTuple != null)){
            // output a tuple with the nulls padded leftTuple
            Tuple nullPaddedTuple = TupleUtil.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;
          }

          if((leftTuple != null) && (rightTuple == null)){
            // output a tuple with the nulls padded leftTuple
            Tuple nullPaddedTuple = TupleUtil.createNullPaddedTuple(rightNumCols);
            frameTuple.set(leftTuple, nullPaddedTuple);
            projector.eval(frameTuple, outTuple);
            // we simulate we found a match, which is exactly the null padded one
            leftTuple = leftChild.next();
            return outTuple;
          }
        } // if end

        ////////////////////////////////////////////////////////////////////////
        // INITIALIZING STAGE
        ////////////////////////////////////////////////////////////////////////
        // initializing stage, reading the first tuple on each side
        if (leftTuple == null) {
          leftTuple = leftChild.next();
          if( leftTuple == null){
            end = true;
            continue;
          }
        }
        if (rightTuple == null) {
          rightTuple = rightChild.next();
          initRightDone = true;
          if (rightTuple == null) {
            end = true;
            continue;
          }
        }

        // reset tuple slots for a new round
        leftTupleSlots.clear();
        rightTupleSlots.clear();
        posRightTupleSlots = -1;
        posLeftTupleSlots = -1;

        ////////////////////////////////////////////////////////////////////////
        // Comparison and Move Forward Stage
        ////////////////////////////////////////////////////////////////////////
        // advance alternatively on each side until a match is found
        int cmp;
        while (!end && ((cmp = joincomparator.compare(leftTuple, rightTuple)) != 0)) {

          if (cmp > 0) {

            //before getting a new tuple from the right,  a leftnullpadded tuple should be built
            //output a tuple with the nulls padded leftTuple
            Tuple nullPaddedTuple = TupleUtil.createNullPaddedTuple(leftNumCols);
            frameTuple.set(nullPaddedTuple, rightTuple);
            projector.eval(frameTuple, outTuple);
            // BEFORE RETURN, MOVE FORWARD
            rightTuple = rightChild.next();
            if(rightTuple == null) {
              end = true;
            }

            return outTuple;

          } else if (cmp < 0) {
            // before getting a new tuple from the left,  a rightnullpadded tuple should be built
            // output a tuple with the nulls padded rightTuple
            Tuple nullPaddedTuple = TupleUtil.createNullPaddedTuple(rightNumCols);
            frameTuple.set(leftTuple, nullPaddedTuple);
            projector.eval(frameTuple, outTuple);
            // we simulate we found a match, which is exactly the null padded one
            // BEFORE RETURN, MOVE FORWARD
            leftTuple = leftChild.next();
            if(leftTuple == null) {
              end = true;
            }

            return outTuple;

          } // if (cmp < 0)
        } //while


        ////////////////////////////////////////////////////////////////////////
        // SLOTS POPULATION STAGE
        ////////////////////////////////////////////////////////////////////////
        // once a match is found, retain all tuples with this key in tuple slots
        // on each side
        if(!end) {
          endInPopulationStage = false;

          boolean endLeft = false;
          boolean endRight = false;

          previous = new VTuple(leftTuple);
          do {
            leftTupleSlots.add(new VTuple(leftTuple));
            leftTuple = leftChild.next();
            if(leftTuple == null) {
              endLeft = true;
            }


          } while ((endLeft != true) && (tupleComparator[0].compare(previous, leftTuple) == 0));
          posLeftTupleSlots = 0;


          previous = new VTuple(rightTuple);
          do {
            rightTupleSlots.add(new VTuple(rightTuple));
            rightTuple = rightChild.next();
            if(rightTuple == null) {
              endRight = true;
            }

          } while ((endRight != true) && (tupleComparator[1].compare(previous, rightTuple) == 0) );
          posRightTupleSlots = 0;

          if ((endLeft == true) || (endRight == true)) {
            end = true;
            endInPopulationStage = true;
          }

        } // if end false
      } // if newRound


      ////////////////////////////////////////////////////////////////////////
      // RESULTS STAGE
      ////////////////////////////////////////////////////////////////////////
      // 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 || (end && endInPopulationStage)){
        if(posLeftTupleSlots == 0){
          leftNext = new VTuple (leftTupleSlots.get(posLeftTupleSlots));
          posLeftTupleSlots = posLeftTupleSlots + 1;
        }

        if(posRightTupleSlots <= (rightTupleSlots.size() -1)) {
          Tuple aTuple = new VTuple(rightTupleSlots.get(posRightTupleSlots));
          posRightTupleSlots = posRightTupleSlots + 1;
          frameTuple.set(leftNext, 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(rightTupleSlots.get(posRightTupleSlots));
            posRightTupleSlots = posRightTupleSlots + 1;
            leftNext = new VTuple (leftTupleSlots.get(posLeftTupleSlots));
            posLeftTupleSlots = posLeftTupleSlots + 1;

            frameTuple.set(leftNext, aTuple);
            joinQual.eval(inSchema, frameTuple);
            projector.eval(frameTuple, outTuple);
            return outTuple;
          }
        }
      } // the second if end false
    } // for
  }