@Override
  public boolean next() throws IOException {
    //add the previous nextKVReader back to queue
    if (!nextKVReaders.isEmpty()) {
      for (KeyValuesReader kvReader : nextKVReaders) {
        addToQueue(kvReader);
      }
      nextKVReaders.clear();
    }

    KeyValuesReader nextKVReader = null;
    //get the new nextKVReader with lowest key
    nextKVReader = pQueue.poll();
    if (nextKVReader != null) {
      nextKVReaders.add(nextKVReader);
    }

    while (pQueue.peek() != null) {
      KeyValuesReader equalValueKVReader = pQueue.poll();
      if (pQueue.comparator().compare(nextKVReader, equalValueKVReader) == 0) {
        nextKVReaders.add(equalValueKVReader);
      } else {
        pQueue.add(equalValueKVReader);
        break;
      }
    }
    return !(nextKVReaders.isEmpty());
  }
@return true if there are more key-values and advances to next key-values
@throws IOException
ql/src/java/org/apache/hadoop/hive/ql/exec/tez/tools/KeyValuesInputMerger.java