@Override public void deserialize(Block block, int index, DoubleHistogramAggregation.State state) { state.set(new NumericHistogram(VARBINARY.getSlice(block, index), DoubleHistogramAggregation.ENTRY_BUFFER_SIZE)); } }
@Test public void testSameValues() { NumericHistogram histogram = new NumericHistogram(4); for (int i = 0; i < 100; ++i) { histogram.add(1.0, 3); histogram.add(2.0, 5); } Map<Double, Double> expected = ImmutableMap.<Double, Double>builder() .put(1.0, 300.0) .put(2.0, 500.0) .build(); assertEquals(histogram.getBuckets(), expected); }
@Test public void testRoundtrip() { NumericHistogram histogram = new NumericHistogram(100, 20); for (int i = 0; i < 1000; i++) { histogram.add(i); } Slice serialized = histogram.serialize(); NumericHistogram deserialized = new NumericHistogram(serialized, 20); assertEquals(deserialized.getBuckets(), histogram.getBuckets()); }
@Test public void testMergeSame() { NumericHistogram histogram = new NumericHistogram(10, 3); for (int i = 0; i < 1000; i++) { histogram.add(i); } Map<Double, Double> expected = Maps.transformValues(histogram.getBuckets(), value -> value * 2); histogram.mergeWith(histogram); assertEquals(histogram.getBuckets(), expected); }
2.6, 3.8, 3.2, 3.4, 2.5, 4.1, 2.2, 3.4}; NumericHistogram histogram = new NumericHistogram(4, data.length);
@Test public void testMergeDifferent() { NumericHistogram histogram1 = new NumericHistogram(10, 3); NumericHistogram histogram2 = new NumericHistogram(10, 3); for (int i = 0; i < 1000; i++) { histogram1.add(i); histogram2.add(i + 1000); } NumericHistogram expected = new NumericHistogram(10, 1000); for (Map.Entry<Double, Double> entry : histogram1.getBuckets().entrySet()) { expected.add(entry.getKey(), entry.getValue()); } for (Map.Entry<Double, Double> entry : histogram2.getBuckets().entrySet()) { expected.add(entry.getKey(), entry.getValue()); } expected.compact(); histogram1.mergeWith(histogram2); assertEquals(histogram1.getBuckets(), expected.getBuckets()); } }
@InputFunction public static void add(@AggregationState State state, @SqlType(BIGINT) long buckets, @SqlType(DOUBLE) double value, @SqlType(DOUBLE) double weight) { NumericHistogram histogram = state.get(); if (histogram == null) { checkCondition(buckets >= 2, INVALID_FUNCTION_ARGUMENT, "numeric_histogram bucket count must be greater than one"); histogram = new NumericHistogram(toIntExact(buckets), ENTRY_BUFFER_SIZE); state.set(histogram); } histogram.add(value, weight); }
@Override public void deserialize(Block block, int index, NumericHistogramAggregation.State state) { if (!block.isNull(index)) { state.set(new NumericHistogram(VARBINARY.getSlice(block, index), NumericHistogramAggregation.ENTRY_BUFFER_SIZE)); } } }
@Test public void testRoundtrip() throws Exception { NumericHistogram histogram = new NumericHistogram(100, 20); for (int i = 0; i < 1000; i++) { histogram.add(i); } Slice serialized = histogram.serialize(); NumericHistogram deserialized = new NumericHistogram(serialized, 20); assertEquals(deserialized.getBuckets(), histogram.getBuckets()); }
@Test public void testSameValues() throws Exception { NumericHistogram histogram = new NumericHistogram(4); for (int i = 0; i < 100; ++i) { histogram.add(1.0, 3); histogram.add(2.0, 5); } Map<Double, Double> expected = ImmutableMap.<Double, Double>builder() .put(1.0, 300.0) .put(2.0, 500.0) .build(); assertEquals(histogram.getBuckets(), expected); }
@Test public void testMergeSame() throws Exception { NumericHistogram histogram = new NumericHistogram(10, 3); for (int i = 0; i < 1000; i++) { histogram.add(i); } Map<Double, Double> expected = Maps.transformValues(histogram.getBuckets(), value -> value * 2); histogram.mergeWith(histogram); assertEquals(histogram.getBuckets(), expected); }
@InputFunction public static void add(State state, @SqlType(BIGINT) long buckets, @SqlType(DOUBLE) double value, @SqlType(DOUBLE) double weight) { NumericHistogram histogram = state.get(); if (histogram == null) { checkCondition(buckets >= 2, INVALID_FUNCTION_ARGUMENT, "numeric_histogram bucket count must be greater than one"); histogram = new NumericHistogram(Ints.checkedCast(buckets), ENTRY_BUFFER_SIZE); state.set(histogram); } histogram.add(value, weight); }
2.6, 3.8, 3.2, 3.4, 2.5, 4.1, 2.2, 3.4}; NumericHistogram histogram = new NumericHistogram(4, data.length);
@Test public void testMergeDifferent() throws Exception { NumericHistogram histogram1 = new NumericHistogram(10, 3); NumericHistogram histogram2 = new NumericHistogram(10, 3); for (int i = 0; i < 1000; i++) { histogram1.add(i); histogram2.add(i + 1000); } NumericHistogram expected = new NumericHistogram(10, 1000); for (Map.Entry<Double, Double> entry : histogram1.getBuckets().entrySet()) { expected.add(entry.getKey(), entry.getValue()); } for (Map.Entry<Double, Double> entry : histogram2.getBuckets().entrySet()) { expected.add(entry.getKey(), entry.getValue()); } expected.compact(); histogram1.mergeWith(histogram2); assertEquals(histogram1.getBuckets(), expected.getBuckets()); } }