@Override
    public void iterate(AggregationBuffer agg, Object[] parameters) throws HiveException {
      Object p = parameters[0];
      GenericUDAFComputeBitVectorBase.StringStatsAgg myagg = (GenericUDAFComputeBitVectorBase.StringStatsAgg) agg;

      if (myagg.firstItem) {
        myagg.numDV = HyperLogLog.builder().setSizeOptimized().build();
        myagg.numDV.reset();
        myagg.firstItem = false;
      }

      String v = PrimitiveObjectInspectorUtils.getString(p, inputOI);
      if (v != null) {
        // Add string value to NumDistinctValue Estimator
        myagg.numDV.addToEstimator(v);
      }
    }
GenericUDAFComputeBitVector. This UDAF will compute a bit vector using
HyperLogLog algorithm. The ndv_compute_bit_vector function can
be used on top of it to extract an estimate of the ndv from it.
ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDAFComputeBitVectorHLL.java