@UdafFactory(description = "Compute sample standard deviation of column with type Integer.",
      aggregateSchema = "STRUCT<SUM integer, COUNT bigint, M2 double>")
  public static TableUdaf<Integer, Struct, Double> stdDevInt() {
    return getStdDevImplementation(
        0,
        STRUCT_INT,
        (agg, newValue) -> newValue + agg.getInt32(SUM),
        (agg, newValue) ->
            Double.valueOf(newValue * (agg.getInt64(COUNT) + 1) - (agg.getInt32(SUM) + newValue)),
        (agg1, agg2) ->
            agg1.getInt32(SUM).doubleValue() / agg1.getInt64(COUNT).doubleValue()
                - agg2.getInt32(SUM).doubleValue() / agg2.getInt64(COUNT).doubleValue(),
        (agg1, agg2) -> agg1.getInt32(SUM) + agg2.getInt32(SUM),
        (agg, valueToRemove) -> agg.getInt32(SUM) - valueToRemove);
  }
@Test
  public void shouldCalculateStdDevInts() {
    final TableUdaf<Integer, Struct, Double> udaf = stdDevInt();
    Struct agg = udaf.initialize();
    final Integer[] values = new Integer[] {3, 5, 6, 7};
    for (final Integer thisValue : values) {
      agg = udaf.aggregate(thisValue, agg);
    }
    assertThat(agg.getInt64(COUNT), equalTo(4L));
    assertThat(agg.getInt32(SUM), equalTo(21));
    assertThat(agg.getFloat64(M2), equalTo(8.75));

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