@Override
    public void iterate(AggregationBuffer agg, Object[] parameters) throws HiveException {
      NumericStatsAgg myagg = (NumericStatsAgg) agg;

      if (myagg.numDV == null) {
        int numVectors = 0;
        if (parameters.length == 2) {
          numVectors = parameters[1] == null ? 0 : PrimitiveObjectInspectorUtils.getInt(
              parameters[1], numVectorsOI);
          if (numVectors > MAX_BIT_VECTORS) {
            throw new HiveException("The maximum allowed value for number of bit vectors " + " is "
                + MAX_BIT_VECTORS + ", but was passed " + numVectors + " bit vectors");
          }
        }
        myagg.numDV = new FMSketch(numVectors);
      }

      if (parameters[0] != null) {
        myagg.update(parameters[0], inputOI);
      }
    }
GenericUDAFComputeBitVector. This UDAF will compute a bit vector using
FMSketch 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/GenericUDAFComputeBitVectorFMSketch.java