Skip to content

Commit

Permalink
Sketch cache key should include size, isInputThetaSketch. (apache#2893)
Browse files Browse the repository at this point in the history
  • Loading branch information
gianm authored and fjy committed Apr 28, 2016
1 parent 90ce03c commit 909abd1
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Preconditions;
import com.google.common.primitives.Doubles;
import com.google.common.primitives.Ints;
import com.metamx.common.IAE;
import com.yahoo.sketches.Family;
import com.yahoo.sketches.Util;
Expand Down Expand Up @@ -169,7 +170,11 @@ public List<String> requiredFields()
public byte[] getCacheKey()
{
byte[] fieldNameBytes = fieldName.getBytes();
return ByteBuffer.allocate(1 + fieldNameBytes.length).put(cacheId).put(fieldNameBytes).array();
return ByteBuffer.allocate(1 + Ints.BYTES + fieldNameBytes.length)
.put(cacheId)
.putInt(size)
.put(fieldNameBytes)
.array();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import io.druid.query.aggregation.AggregatorFactory;
import io.druid.query.aggregation.AggregatorFactoryNotMergeableException;

import java.nio.ByteBuffer;
import java.util.Collections;
import java.util.List;

Expand Down Expand Up @@ -104,7 +105,7 @@ public boolean getIsInputThetaSketch()
{
return isInputThetaSketch;
}

@JsonProperty
public Integer getErrorBoundsStdDev()
{
Expand All @@ -126,9 +127,9 @@ public Object finalizeComputation(Object object)
Sketch sketch = (Sketch) object;
if (errorBoundsStdDev != null) {
SketchEstimateWithErrorBounds result = new SketchEstimateWithErrorBounds(
sketch.getEstimate(),
sketch.getUpperBound(errorBoundsStdDev),
sketch.getLowerBound(errorBoundsStdDev),
sketch.getEstimate(),
sketch.getUpperBound(errorBoundsStdDev),
sketch.getLowerBound(errorBoundsStdDev),
errorBoundsStdDev);
return result;
} else {
Expand All @@ -149,6 +150,17 @@ public String getTypeName()
}
}

@Override
public byte[] getCacheKey()
{
final byte[] superCacheKey = super.getCacheKey();

return ByteBuffer.allocate(superCacheKey.length + 1)
.put(superCacheKey)
.put(isInputThetaSketch ? (byte) 1 : (byte) 0)
.array();
}

@Override
public boolean equals(Object o)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.List;

/**
Expand Down Expand Up @@ -384,6 +385,38 @@ public void testSketchSetPostAggregatorSerde() throws Exception
);
}

@Test
public void testCacheKey()
{
final SketchMergeAggregatorFactory factory1 = new SketchMergeAggregatorFactory(
"name",
"fieldName",
16,
null,
null,
null
);
final SketchMergeAggregatorFactory factory2 = new SketchMergeAggregatorFactory(
"name",
"fieldName",
16,
null,
null,
null
);
final SketchMergeAggregatorFactory factory3 = new SketchMergeAggregatorFactory(
"name",
"fieldName",
32,
null,
null,
null
);

Assert.assertTrue(Arrays.equals(factory1.getCacheKey(), factory2.getCacheKey()));
Assert.assertFalse(Arrays.equals(factory1.getCacheKey(), factory3.getCacheKey()));
}

private void assertPostAggregatorSerde(PostAggregator agg) throws Exception
{
Assert.assertEquals(
Expand Down

0 comments on commit 909abd1

Please sign in to comment.