/** * Returns a {@code PTransform} that takes an input {@code PCollection<KV<K, Double>>} and returns * a {@code PCollection<KV<K, Double>>} that contains an output element mapping each distinct key * in the input {@code PCollection} to the sum of the values associated with that key in the input * {@code PCollection}. */ public static <K> Combine.PerKey<K, Double, Double> doublesPerKey() { return Combine.perKey(Sum.ofDoubles()); }
/** * Returns a {@code PTransform} that takes an input {@code PCollection<Double>} and returns a * {@code PCollection<Double>} whose contents is the sum of the input {@code PCollection}'s * elements, or {@code 0} if there are no elements. */ public static Combine.Globally<Double, Double> doublesGlobally() { return Combine.globally(Sum.ofDoubles()); }
/** {@link CombineFn} for Sum based on {@link Sum} and {@link Combine.BinaryCombineFn}. */ static CombineFn createSum(Schema.TypeName fieldType) { switch (fieldType) { case INT32: return Sum.ofIntegers(); case INT16: return new ShortSum(); case BYTE: return new ByteSum(); case INT64: return Sum.ofLongs(); case FLOAT: return new FloatSum(); case DOUBLE: return Sum.ofDoubles(); case DECIMAL: return new BigDecimalSum(); default: throw new UnsupportedOperationException( String.format("[%s] is not support in SUM", fieldType)); } }
@Test public void testSumDoubleFn() { testCombineFn(Sum.ofDoubles(), Lists.newArrayList(1.0, 2.0, 3.0, 4.0), 10.0); }
@Test public void testDoubleStats() { for (TestCase<Double> t : DOUBLE_CASES) { assertEquals(t.sum, Sum.ofDoubles().apply(t.data), DOUBLE_COMPARISON_ACCURACY); assertEquals(t.min, Min.ofDoubles().apply(t.data), DOUBLE_COMPARISON_ACCURACY); assertEquals(t.max, Max.ofDoubles().apply(t.data), DOUBLE_COMPARISON_ACCURACY); assertEquals(t.mean, Mean.<Double>of().apply(t.data), DOUBLE_COMPARISON_ACCURACY); } }
@Test public void testGetAccumulatorCoderEquals() { Combine.BinaryCombineIntegerFn sumIntegerFn = Sum.ofIntegers(); assertEquals( sumIntegerFn.getAccumulatorCoder(STANDARD_REGISTRY, VarIntCoder.of()), sumIntegerFn.getAccumulatorCoder(STANDARD_REGISTRY, VarIntCoder.of())); assertNotEquals( sumIntegerFn.getAccumulatorCoder(STANDARD_REGISTRY, VarIntCoder.of()), sumIntegerFn.getAccumulatorCoder(STANDARD_REGISTRY, BigEndianIntegerCoder.of())); Combine.BinaryCombineLongFn sumLongFn = Sum.ofLongs(); assertEquals( sumLongFn.getAccumulatorCoder(STANDARD_REGISTRY, VarLongCoder.of()), sumLongFn.getAccumulatorCoder(STANDARD_REGISTRY, VarLongCoder.of())); assertNotEquals( sumLongFn.getAccumulatorCoder(STANDARD_REGISTRY, VarLongCoder.of()), sumLongFn.getAccumulatorCoder(STANDARD_REGISTRY, BigEndianLongCoder.of())); Combine.BinaryCombineDoubleFn sumDoubleFn = Sum.ofDoubles(); assertEquals( sumDoubleFn.getAccumulatorCoder(STANDARD_REGISTRY, DoubleCoder.of()), sumDoubleFn.getAccumulatorCoder(STANDARD_REGISTRY, DoubleCoder.of())); } }