@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) ->
            Double.valueOf(
                agg1.getInt32(SUM) / agg1.getInt64(COUNT)
                    - agg2.getInt32(SUM) / agg2.getInt64(COUNT)),
        (agg1, agg2) -> agg1.getInt32(SUM) + agg2.getInt32(SUM),
        (agg, valueToRemove) -> agg.getInt32(SUM) - valueToRemove);
  }
@Test
  public void shouldMergeInts() {
    final TableUdaf<Integer, Struct, Double> udaf = stdDevInt();

    Struct left = udaf.initialize();
    final Integer[] leftValues = new Integer[] {5, 8, 10};
    for (final Integer thisValue : leftValues) {
      left = udaf.aggregate(thisValue, left);
    }

    Struct right = udaf.initialize();
    final Integer[] rightValues = new Integer[] {6, 7, 9};
    for (final Integer thisValue : rightValues) {
      right = udaf.aggregate(thisValue, right);
    }

    final Struct merged = udaf.merge(left, right);

    assertThat(merged.getInt64(COUNT), equalTo(6L));
    assertThat(merged.getInt32(SUM), equalTo(45));
    assertThat(merged.getFloat64(M2), equalTo(17.333333333333332));

    final double standardDev = udaf.map(merged);
    assertThat(standardDev, equalTo(3.4666666666666663));
  }