@Override
    public void iterate(AggregationBuffer agg, Object[] parameters) throws HiveException {
      HypotheticalSetRankBuffer rankBuffer = (HypotheticalSetRankBuffer) agg;
      rankBuffer.rowCount++;

      CompareResult compareResult = compare(parameters);

      if (compareResult.getCompareResult() == 0) {
        if (allowEquality) {
          rankBuffer.rank++;
        }
        return;
      }

      if (compareResult.getOrder() == ASCENDING_CODE && compareResult.getCompareResult() < 0 ||
              compareResult.getOrder() == DESCENDING_CODE && compareResult.getCompareResult() > 0) {
        rankBuffer.rank++;
      }
    }
Hypothetical rank calculation.
 Calculates the rank of a hypothetical row specified by the arguments of the
 function in a group of values specified by the order by clause.
 SELECT rank(expression1[, expressionn]*) WITHIN GROUP (ORDER BY col1[, coln]*)
 (the number of rows where col1 &lt; expression1 [and coln &lt; expressionn]*) + 1
ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDAFRank.java