From a15a2c4047ea6f7765d6ad48f8789ae5505b5c7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xavier=20L=C3=A9aut=C3=A9?= Date: Wed, 15 Jul 2015 13:02:42 -0700 Subject: [PATCH] fix histogram aggregator cache key --- .../HistogramAggregatorFactory.java | 9 ++++++++- .../aggregation/HistogramAggregatorTest.java | 18 ++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/processing/src/main/java/io/druid/query/aggregation/HistogramAggregatorFactory.java b/processing/src/main/java/io/druid/query/aggregation/HistogramAggregatorFactory.java index 3319b0987f7..9f3c4ccfecf 100644 --- a/processing/src/main/java/io/druid/query/aggregation/HistogramAggregatorFactory.java +++ b/processing/src/main/java/io/druid/query/aggregation/HistogramAggregatorFactory.java @@ -153,7 +153,14 @@ public class HistogramAggregatorFactory implements AggregatorFactory public byte[] getCacheKey() { byte[] fieldNameBytes = StringUtils.toUtf8(fieldName); - return ByteBuffer.allocate(1 + fieldNameBytes.length).put(CACHE_TYPE_ID).put(fieldNameBytes).array(); + ByteBuffer buf = ByteBuffer + .allocate(1 + fieldNameBytes.length + Floats.BYTES * breaks.length) + .put(CACHE_TYPE_ID) + .put(fieldNameBytes) + .put((byte)0xFF); + buf.asFloatBuffer().put(breaks); + + return buf.array(); } @Override diff --git a/processing/src/test/java/io/druid/query/aggregation/HistogramAggregatorTest.java b/processing/src/test/java/io/druid/query/aggregation/HistogramAggregatorTest.java index 159898538e8..82151c7a05e 100644 --- a/processing/src/test/java/io/druid/query/aggregation/HistogramAggregatorTest.java +++ b/processing/src/test/java/io/druid/query/aggregation/HistogramAggregatorTest.java @@ -17,12 +17,15 @@ package io.druid.query.aggregation; +import com.google.common.collect.ImmutableList; import com.google.common.collect.Lists; +import io.druid.jackson.DefaultObjectMapper; import org.junit.Assert; import org.junit.Test; import java.nio.ByteBuffer; import java.util.ArrayList; +import java.util.Arrays; public class HistogramAggregatorTest { @@ -32,6 +35,21 @@ public class HistogramAggregatorTest selector.increment(); } + @Test + public void testSerde() throws Exception + { + final DefaultObjectMapper objectMapper = new DefaultObjectMapper(); + String json0 = "{\"type\": \"histogram\", \"name\": \"billy\", \"fieldName\": \"nilly\"}"; + HistogramAggregatorFactory agg0 = objectMapper.readValue(json0, HistogramAggregatorFactory.class); + Assert.assertEquals(ImmutableList.of(), agg0.getBreaks()); + + String aggSpecJson = "{\"type\": \"histogram\", \"name\": \"billy\", \"fieldName\": \"nilly\", \"breaks\": [ -1, 2, 3.0 ]}"; + HistogramAggregatorFactory agg = objectMapper.readValue(aggSpecJson, HistogramAggregatorFactory.class); + + Assert.assertEquals(new HistogramAggregatorFactory("billy", "nilly", Arrays.asList(-1f, 2f, 3.0f)), agg); + Assert.assertEquals(agg, objectMapper.readValue(objectMapper.writeValueAsBytes(agg), HistogramAggregatorFactory.class)); + } + @Test public void testAggregate() throws Exception { final float[] values = {0.55f, 0.27f, -0.3f, -.1f, -0.8f, -.7f, -.5f, 0.25f, 0.1f, 2f, -3f};