Skip to content

Commit

Permalink
Add PostAggregators to generator cache keys for top-n queries (apache…
Browse files Browse the repository at this point in the history
…#3899)

* Add PostAggregators to generator cache keys for top-n queries

* Add tests for strings

* Remove debug comments

* Add type keys and list sizes to cache key

* Make post aggregators used for sort are considered for cache key generation

* Use assertArrayEquals()

* Improve findPostAggregatorsForSort()

* Address comments

* fix test failure

* address comments
  • Loading branch information
jihoonson authored and gianm committed Feb 13, 2017
1 parent 8e31a46 commit 991e285
Show file tree
Hide file tree
Showing 49 changed files with 1,256 additions and 315 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*/
public class StringUtils extends io.druid.java.util.common.StringUtils
{
private static final byte[] EMPTY_BYTES = new byte[0];
public static final byte[] EMPTY_BYTES = new byte[0];

// should be used only for estimation
// returns the same result with StringUtils.fromUtf8(value).length for valid string values
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import com.google.common.collect.Sets;
import com.google.common.primitives.Doubles;
import io.druid.query.aggregation.PostAggregator;
import io.druid.query.aggregation.post.PostAggregatorIds;
import io.druid.query.cache.CacheKeyBuilder;

import java.util.Comparator;
import java.util.Map;
Expand Down Expand Up @@ -147,4 +149,12 @@ public int hashCode()
result = 31 * result + (errorBoundsStdDev != null ? errorBoundsStdDev.hashCode() : 0);
return result;
}

@Override
public byte[] getCacheKey()
{
final CacheKeyBuilder builder = new CacheKeyBuilder(PostAggregatorIds.DATA_SKETCHES_SKETCH_ESTIMATE)
.appendCacheable(field);
return errorBoundsStdDev == null ? builder.build() : builder.appendInt(errorBoundsStdDev).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@
import com.google.common.collect.Sets;
import com.yahoo.sketches.Util;
import io.druid.java.util.common.IAE;
import io.druid.java.util.common.logger.Logger;
import io.druid.query.aggregation.PostAggregator;
import io.druid.query.aggregation.post.PostAggregatorIds;
import io.druid.query.cache.CacheKeyBuilder;

import java.util.Comparator;
import java.util.List;
Expand All @@ -34,9 +35,6 @@

public class SketchSetPostAggregator implements PostAggregator
{

private static final Logger LOG = new Logger(SketchSetPostAggregator.class);

private final String name;
private final List<PostAggregator> fields;
private final SketchHolder.Func func;
Expand Down Expand Up @@ -163,4 +161,33 @@ public int hashCode()
result = 31 * result + maxSketchSize;
return result;
}

@Override
public byte[] getCacheKey()
{
final CacheKeyBuilder builder = new CacheKeyBuilder(PostAggregatorIds.DATA_SKETCHES_SKETCH_SET)
.appendString(getFunc())
.appendInt(maxSketchSize);

if (preserveFieldOrderInCacheKey(func)) {
builder.appendCacheables(fields);
} else {
builder.appendCacheablesIgnoringOrder(fields);
}

return builder.build();
}

private static boolean preserveFieldOrderInCacheKey(SketchHolder.Func func)
{
switch (func) {
case NOT:
return true;
case UNION:
case INTERSECT:
return false;
default:
throw new IAE(func.name());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public abstract class ApproximateHistogramPostAggregator implements PostAggregat
{
private static final Comparator COMPARATOR = ApproximateHistogramAggregator.COMPARATOR;

private final String name;
private final String fieldName;
protected final String name;
protected final String fieldName;

public ApproximateHistogramPostAggregator(
String name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeName;
import com.google.common.collect.Sets;

import io.druid.java.util.common.IAE;
import io.druid.query.aggregation.post.PostAggregatorIds;
import io.druid.query.cache.CacheKeyBuilder;

import java.util.Map;
import java.util.Set;
Expand All @@ -35,8 +36,6 @@ public class BucketsPostAggregator extends ApproximateHistogramPostAggregator
private final float bucketSize;
private final float offset;

private String fieldName;

@JsonCreator
public BucketsPostAggregator(
@JsonProperty("name") String name,
Expand All @@ -51,7 +50,6 @@ public BucketsPostAggregator(
throw new IAE("Illegal bucketSize [%s], must be > 0", this.bucketSize);
}
this.offset = offset;
this.fieldName = fieldName;
}

@Override
Expand All @@ -63,7 +61,7 @@ public Set<String> getDependentFields()
@Override
public Object compute(Map<String, Object> values)
{
ApproximateHistogram ah = (ApproximateHistogram) values.get(this.getFieldName());
ApproximateHistogram ah = (ApproximateHistogram) values.get(fieldName);
return ah.toHistogram(bucketSize, offset);
}

Expand All @@ -89,4 +87,14 @@ public String toString()
", offset=" + this.getOffset() +
'}';
}

@Override
public byte[] getCacheKey()
{
return new CacheKeyBuilder(PostAggregatorIds.HISTOGRAM_BUCKETS)
.appendString(fieldName)
.appendFloat(bucketSize)
.appendFloat(offset)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeName;
import com.google.common.collect.Sets;
import io.druid.query.aggregation.post.PostAggregatorIds;
import io.druid.query.cache.CacheKeyBuilder;

import java.util.Arrays;
import java.util.Map;
Expand Down Expand Up @@ -55,7 +57,7 @@ public Set<String> getDependentFields()
@Override
public Object compute(Map<String, Object> values)
{
ApproximateHistogram ah = (ApproximateHistogram) values.get(this.getFieldName());
ApproximateHistogram ah = (ApproximateHistogram) values.get(fieldName);
return ah.toHistogram(breaks);
}

Expand All @@ -74,4 +76,13 @@ public String toString()
", breaks=" + Arrays.toString(this.getBreaks()) +
'}';
}

@Override
public byte[] getCacheKey()
{
return new CacheKeyBuilder(PostAggregatorIds.HISTOGRAM_CUSTOM_BUCKETS)
.appendString(fieldName)
.appendFloatArray(breaks)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeName;
import com.google.common.collect.Sets;

import io.druid.java.util.common.IAE;
import io.druid.query.aggregation.post.PostAggregatorIds;
import io.druid.query.cache.CacheKeyBuilder;

import java.util.Map;
import java.util.Set;
Expand All @@ -33,7 +34,6 @@
public class EqualBucketsPostAggregator extends ApproximateHistogramPostAggregator
{
private final int numBuckets;
private String fieldName;

@JsonCreator
public EqualBucketsPostAggregator(
Expand All @@ -47,7 +47,6 @@ public EqualBucketsPostAggregator(
if (this.numBuckets <= 1) {
throw new IAE("Illegal number of buckets[%s], must be > 1", this.numBuckets);
}
this.fieldName = fieldName;
}

@Override
Expand All @@ -59,7 +58,7 @@ public Set<String> getDependentFields()
@Override
public Object compute(Map<String, Object> values)
{
ApproximateHistogram ah = (ApproximateHistogram) values.get(this.getFieldName());
ApproximateHistogram ah = (ApproximateHistogram) values.get(fieldName);
return ah.toHistogram(numBuckets);
}

Expand All @@ -73,9 +72,18 @@ public int getNumBuckets()
public String toString()
{
return "EqualBucketsPostAggregator{" +
"name='" + this.getName() + '\'' +
", fieldName='" + this.getFieldName() + '\'' +
", numBuckets=" + this.getNumBuckets() +
"name='" + name + '\'' +
", fieldName='" + fieldName + '\'' +
", numBuckets=" + numBuckets +
'}';
}

@Override
public byte[] getCacheKey()
{
return new CacheKeyBuilder(PostAggregatorIds.HISTOGRAM_EQUAL_BUCKETS)
.appendString(fieldName)
.appendInt(numBuckets)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeName;
import com.google.common.collect.Sets;
import io.druid.query.aggregation.post.PostAggregatorIds;
import io.druid.query.cache.CacheKeyBuilder;

import java.util.Comparator;
import java.util.Map;
Expand All @@ -40,16 +42,13 @@ public int compare(Object o, Object o1)
}
};

private String fieldName;

@JsonCreator
public MaxPostAggregator(
@JsonProperty("name") String name,
@JsonProperty("fieldName") String fieldName
)
{
super(name, fieldName);
this.fieldName = fieldName;
}

@Override
Expand All @@ -67,7 +66,7 @@ public Set<String> getDependentFields()
@Override
public Object compute(Map<String, Object> values)
{
final ApproximateHistogram ah = (ApproximateHistogram) values.get(this.getFieldName());
final ApproximateHistogram ah = (ApproximateHistogram) values.get(fieldName);
return ah.getMax();
}

Expand All @@ -78,4 +77,12 @@ public String toString()
"fieldName='" + fieldName + '\'' +
'}';
}

@Override
public byte[] getCacheKey()
{
return new CacheKeyBuilder(PostAggregatorIds.HISTOGRAM_MAX)
.appendString(fieldName)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeName;
import com.google.common.collect.Sets;
import io.druid.query.aggregation.post.PostAggregatorIds;
import io.druid.query.cache.CacheKeyBuilder;

import java.util.Comparator;
import java.util.Map;
Expand All @@ -40,16 +42,13 @@ public int compare(Object o, Object o1)
}
};

private String fieldName;

@JsonCreator
public MinPostAggregator(
@JsonProperty("name") String name,
@JsonProperty("fieldName") String fieldName
)
{
super(name, fieldName);
this.fieldName = fieldName;
}

@Override
Expand All @@ -67,7 +66,7 @@ public Set<String> getDependentFields()
@Override
public Object compute(Map<String, Object> values)
{
final ApproximateHistogram ah = (ApproximateHistogram) values.get(this.getFieldName());
final ApproximateHistogram ah = (ApproximateHistogram) values.get(fieldName);
return ah.getMin();
}

Expand All @@ -78,4 +77,12 @@ public String toString()
"fieldName='" + fieldName + '\'' +
'}';
}

@Override
public byte[] getCacheKey()
{
return new CacheKeyBuilder(PostAggregatorIds.HISTOGRAM_MIN)
.appendString(fieldName)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeName;
import com.google.common.collect.Sets;

import io.druid.java.util.common.IAE;
import io.druid.query.aggregation.post.PostAggregatorIds;
import io.druid.query.cache.CacheKeyBuilder;

import java.util.Comparator;
import java.util.Map;
Expand All @@ -43,7 +44,7 @@ public int compare(Object o, Object o1)
};

private final float probability;
private String fieldName;
private final String fieldName;

@JsonCreator
public QuantilePostAggregator(
Expand Down Expand Up @@ -76,8 +77,8 @@ public Set<String> getDependentFields()
@Override
public Object compute(Map<String, Object> values)
{
final ApproximateHistogram ah = (ApproximateHistogram) values.get(this.getFieldName());
return ah.getQuantiles(new float[]{this.getProbability()})[0];
final ApproximateHistogram ah = (ApproximateHistogram) values.get(fieldName);
return ah.getQuantiles(new float[]{probability})[0];
}

@JsonProperty
Expand Down Expand Up @@ -120,4 +121,13 @@ public String toString()
", fieldName='" + fieldName + '\'' +
'}';
}

@Override
public byte[] getCacheKey()
{
return new CacheKeyBuilder(PostAggregatorIds.HISTOGRAM_QUANTILE)
.appendString(fieldName)
.appendFloat(probability)
.build();
}
}
Loading

0 comments on commit 991e285

Please sign in to comment.