@UdafFactory(description = "Compute sample standard deviation of column with type Double.",
      aggregateSchema = "STRUCT<SUM double, COUNT bigint, M2 double>")
  public static TableUdaf<Double, Struct, Double> stdDevDouble() {
    return getStdDevImplementation(
        0.0,
        STRUCT_DOUBLE,
        (agg, newValue) -> newValue + agg.getFloat64(SUM),
        (agg, newValue) -> newValue * (agg.getInt64(COUNT) + 1) - (agg.getFloat64(SUM) + newValue),
        (agg1, agg2) ->
            agg1.getFloat64(SUM) / agg1.getInt64(COUNT)
                - agg2.getFloat64(SUM) / agg2.getInt64(COUNT),
        (agg1, agg2) -> agg1.getFloat64(SUM) + agg2.getFloat64(SUM),
        (agg, valueToRemove) -> agg.getFloat64(SUM) - valueToRemove);
  }
@Test
  public void shouldCalculateStdDevDoubles() {
    final TableUdaf<Double, Struct, Double> udaf = stdDevDouble();
    Struct agg = udaf.initialize();
    final Double[] values = new Double[] {10.2, 13.4, 14.5, 17.8};
    for (final Double thisValue : values) {
      agg = udaf.aggregate(thisValue, agg);
    }
    assertThat(agg.getInt64(COUNT), equalTo(4L));
    assertThat(agg.getFloat64(SUM), equalTo(55.900000000000006));
    assertThat(agg.getFloat64(M2), equalTo(29.48749999999999));

    final double standardDev = udaf.map(agg);
    assertThat(standardDev, equalTo(3.1351501824739856));
  }