Skip to content

Commit

Permalink
Fix cache bug in stats module (#5650)
Browse files Browse the repository at this point in the history
  • Loading branch information
drcrallen authored and jihoonson committed Apr 17, 2018
1 parent fbf3fc1 commit 8e441cd
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ public String toString()
return "StandardDeviationPostAggregator{" +
"name='" + name + '\'' +
", fieldName='" + fieldName + '\'' +
", isVariancePop='" + isVariancePop + '\'' +
", estimator='" + estimator + '\'' +
", isVariancePop=" + isVariancePop +
'}';
}

Expand All @@ -116,6 +117,7 @@ public byte[] getCacheKey()
{
return new CacheKeyBuilder(PostAggregatorIds.VARIANCE_STANDARD_DEVIATION)
.appendString(fieldName)
.appendString(estimator)
.appendBoolean(isVariancePop)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,15 @@ public static VarianceAggregatorCollector from(ByteBuffer buffer)
return new VarianceAggregatorCollector(buffer.getLong(), buffer.getDouble(), buffer.getDouble());
}

public static final Comparator<VarianceAggregatorCollector> COMPARATOR = new Comparator<VarianceAggregatorCollector>()
{
@Override
public int compare(VarianceAggregatorCollector o1, VarianceAggregatorCollector o2)
{
int compare = Longs.compare(o1.count, o2.count);
public static final Comparator<VarianceAggregatorCollector> COMPARATOR = (o1, o2) -> {
int compare = Longs.compare(o1.count, o2.count);
if (compare == 0) {
compare = Doubles.compare(o1.sum, o2.sum);
if (compare == 0) {
compare = Doubles.compare(o1.sum, o2.sum);
if (compare == 0) {
compare = Doubles.compare(o1.nvariance, o2.nvariance);
}
compare = Doubles.compare(o1.nvariance, o2.nvariance);
}
return compare;
}
return compare;
};

void fold(@Nullable VarianceAggregatorCollector other)
Expand Down Expand Up @@ -114,13 +109,6 @@ public VarianceAggregatorCollector()
this(0, 0, 0);
}

public void reset()
{
count = 0;
sum = 0;
nvariance = 0;
}

void copyFrom(VarianceAggregatorCollector other)
{
this.count = other.count;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@
import io.druid.query.aggregation.NoopAggregator;
import io.druid.query.aggregation.NoopBufferAggregator;
import io.druid.query.aggregation.ObjectAggregateCombiner;
import io.druid.query.cache.CacheKeyBuilder;
import io.druid.segment.ColumnSelectorFactory;
import io.druid.segment.ColumnValueSelector;
import io.druid.segment.NilColumnValueSelector;
import org.apache.commons.codec.binary.Base64;

import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
Expand Down Expand Up @@ -184,7 +184,7 @@ public AggregatorFactory getCombiningFactory()
@Override
public List<AggregatorFactory> getRequiredColumns()
{
return Arrays.<AggregatorFactory>asList(new VarianceAggregatorFactory(fieldName, fieldName, estimator, inputType));
return Collections.singletonList(new VarianceAggregatorFactory(fieldName, fieldName, estimator, inputType));
}

@Override
Expand Down Expand Up @@ -258,25 +258,23 @@ public List<String> requiredFields()
@Override
public byte[] getCacheKey()
{
byte[] fieldNameBytes = StringUtils.toUtf8(fieldName);
byte[] inputTypeBytes = StringUtils.toUtf8(inputType);
return ByteBuffer.allocate(2 + fieldNameBytes.length + 1 + inputTypeBytes.length)
.put(AggregatorUtil.VARIANCE_CACHE_TYPE_ID)
.put(isVariancePop ? (byte) 1 : 0)
.put(fieldNameBytes)
.put((byte) 0xFF)
.put(inputTypeBytes)
.array();
return new CacheKeyBuilder(AggregatorUtil.VARIANCE_CACHE_TYPE_ID)
.appendString(fieldName)
.appendString(inputType)
.appendBoolean(isVariancePop)
.appendString(estimator)
.build();
}

@Override
public String toString()
{
return getClass().getSimpleName() + "{" +
return "VarianceAggregatorFactory{" +
"fieldName='" + fieldName + '\'' +
", name='" + name + '\'' +
", isVariancePop='" + isVariancePop + '\'' +
", estimator='" + estimator + '\'' +
", inputType='" + inputType + '\'' +
", isVariancePop=" + isVariancePop +
'}';
}

Expand All @@ -289,29 +287,18 @@ public boolean equals(Object o)
if (o == null || getClass() != o.getClass()) {
return false;
}

VarianceAggregatorFactory that = (VarianceAggregatorFactory) o;

if (!Objects.equals(name, that.name)) {
return false;
}
if (!Objects.equals(isVariancePop, that.isVariancePop)) {
return false;
}
if (!Objects.equals(inputType, that.inputType)) {
return false;
}

return true;
return isVariancePop == that.isVariancePop &&
Objects.equals(fieldName, that.fieldName) &&
Objects.equals(name, that.name) &&
Objects.equals(estimator, that.estimator) &&
Objects.equals(inputType, that.inputType);
}

@Override
public int hashCode()
{
int result = fieldName.hashCode();
result = 31 * result + Objects.hashCode(name);
result = 31 * result + Objects.hashCode(isVariancePop);
result = 31 * result + Objects.hashCode(inputType);
return result;

return Objects.hash(fieldName, name, estimator, inputType, isVariancePop);
}
}

0 comments on commit 8e441cd

Please sign in to comment.