Skip to content

Commit

Permalink
fix histogram aggregator cache key
Browse files Browse the repository at this point in the history
  • Loading branch information
xvrl committed Jul 16, 2015
1 parent 8504604 commit a15a2c4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,14 @@ public List<String> requiredFields()
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -32,6 +35,21 @@ private void aggregate(TestFloatColumnSelector selector, Aggregator agg)
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};
Expand Down

0 comments on commit a15a2c4

Please sign in to comment.