@Override
    public Object terminate(AggregationBuffer agg) throws HiveException {
      PercentileAgg percAgg = (PercentileAgg) agg;

      // No input data.
      if (percAgg.counts == null || percAgg.counts.size() == 0) {
        return null;
      }

      // Get all items into an array and sort them.
      Set<Map.Entry<U, LongWritable>> entries = percAgg.counts.entrySet();
      List<Map.Entry<U, LongWritable>> entriesList =
          new ArrayList<Map.Entry<U, LongWritable>>(entries);
      sortEntries(entriesList, percAgg.isAscending);

      // Accumulate the counts.
      long total = getTotal(entriesList);
      if (results == null) {
        results = new ArrayList<>(percAgg.percentiles.size());
        for (int i = 0; i < percAgg.percentiles.size(); ++i) {
          results.add(new DoubleWritable(0));
        }
      }
      calculatePercentile(percAgg.percentiles, entriesList, total, results);
      return converter.convertResults(results);
    }
A state class to store intermediate aggregation results.
ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDAFPercentileCont.java