Merge pull request #1534 from metamx/fix-histogram-cachekey

fix histogram aggregator cache key
This commit is contained in:
Charles Allen 2015-07-15 18:32:52 -07:00
commit 4b1bd29424
2 changed files with 26 additions and 1 deletions

View File

@ -153,7 +153,14 @@ public class HistogramAggregatorFactory implements AggregatorFactory
public byte[] getCacheKey() public byte[] getCacheKey()
{ {
byte[] fieldNameBytes = StringUtils.toUtf8(fieldName); 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 @Override

View File

@ -17,12 +17,15 @@
package io.druid.query.aggregation; package io.druid.query.aggregation;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import io.druid.jackson.DefaultObjectMapper;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
public class HistogramAggregatorTest public class HistogramAggregatorTest
{ {
@ -32,6 +35,21 @@ public class HistogramAggregatorTest
selector.increment(); 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 @Test
public void testAggregate() throws Exception { 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}; final float[] values = {0.55f, 0.27f, -0.3f, -.1f, -0.8f, -.7f, -.5f, 0.25f, 0.1f, 2f, -3f};