@SuppressWarnings("unchecked")
  public static <T> T findLastOperator(Operator<?> op, Class<T> clazz) {
    Operator<?> currentOp = op;
    T lastOp = null;
    while (currentOp != null) {
      if (clazz.isInstance(currentOp)) {
        lastOp = (T) currentOp;
      }
      if (currentOp.getChildOperators().size() == 1) {
        currentOp = currentOp.getChildOperators().get(0);
      }
      else {
        currentOp = null;
      }
    }
    return lastOp;
  }
Starting at the input operator, finds the last operator in the stream that
is an instance of the input class.

@param op the starting operator
@param clazz the class that the operator that we are looking for instantiates
@return null if no such operator exists or multiple branches are found in
the stream, the last operator otherwise
ql/src/java/org/apache/hadoop/hive/ql/exec/OperatorUtils.java