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

      if (myagg.firstItem) {
        int numVectors = 0;
        if (parameters.length > 1) {
          numVectors = 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);
        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
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