@UdafFactory(description = "Compute sample standard deviation of column with type Long.",
      aggregateSchema = "STRUCT<SUM bigint, COUNT bigint, M2 double>")
  public static TableUdaf<Long, Struct, Double> stdDevLong() {
    return getStdDevImplementation(
        0L,
        STRUCT_LONG,
        (agg, newValue) -> newValue + agg.getInt64(SUM),
        (agg, newValue) ->
            Double.valueOf(newValue * (agg.getInt64(COUNT) + 1) - (agg.getInt64(SUM) + newValue)),
        (agg1, agg2) ->
            Double.valueOf(
                agg1.getInt64(SUM) / agg1.getInt64(COUNT)
                    - agg2.getInt64(SUM) / agg2.getInt64(COUNT)),
        (agg1, agg2) -> agg1.getInt64(SUM) + agg2.getInt64(SUM),
        (agg, valueToRemove) -> agg.getInt64(SUM) - valueToRemove);
  }
@Test
  public void shouldCalculateStdDevLongs() {
    final TableUdaf<Long, Struct, Double> udaf = stdDevLong();
    Struct agg = udaf.initialize();
    final Long[] values = new Long[] {1L, 2L, 3L, 4L, 5L};
    for (final Long thisValue : values) {
      agg = udaf.aggregate(thisValue, agg);
    }
    assertThat(agg.getInt64(COUNT), equalTo(5L));
    assertThat(agg.getInt64(SUM), equalTo(15L));
    assertThat(agg.getFloat64(M2), equalTo(10.0));

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