@Override
    public void merge(AggregationBuffer agg, Object partial) throws HiveException {
      if(partial == null) {
        return;
      }
      NGramAggBuf myagg = (NGramAggBuf) agg;
      List partialNGrams = (List) loi.getList(partial);
      int n = Integer.parseInt(partialNGrams.get(partialNGrams.size()-1).toString());

      // A value of 0 for n indicates that the mapper processed data that does not meet
      // filter criteria, so merge() should be NO-OP.
      if (n == 0) {
        return;
      }

      if(myagg.n > 0 && myagg.n != n) {
        throw new HiveException(getClass().getSimpleName() + ": mismatch in value for 'n'"
            + ", which usually is caused by a non-constant expression. Found '"+n+"' and '"
            + myagg.n + "'.");
      }
      myagg.n = n;
      partialNGrams.remove(partialNGrams.size()-1);
      myagg.nge.merge(partialNGrams);
    }
A constant-space heuristic to estimate the top-k n-grams.
ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDAFnGrams.java