Skip to content

Commit 3b0d8d0

Browse files
committed
Include minimum in equality
1 parent 29530ac commit 3b0d8d0

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

libs/exponential-histogram/src/main/java/org/elasticsearch/exponentialhistogram/ExponentialHistogram.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,19 @@ static boolean equals(ExponentialHistogram a, ExponentialHistogram b) {
153153

154154
return a.scale() == b.scale()
155155
&& a.sum() == b.sum()
156+
&& equalsIncludingNaN(a.min(), b.min())
156157
&& a.zeroBucket().equals(b.zeroBucket())
157158
&& bucketIteratorsEqual(a.negativeBuckets().iterator(), b.negativeBuckets().iterator())
158159
&& bucketIteratorsEqual(a.positiveBuckets().iterator(), b.positiveBuckets().iterator());
159160
}
160161

162+
private static boolean equalsIncludingNaN(double a, double b) {
163+
if (Double.isNaN(a)) {
164+
return Double.isNaN(b);
165+
}
166+
return a == b;
167+
}
168+
161169
private static boolean bucketIteratorsEqual(BucketIterator a, BucketIterator b) {
162170
if (a.scale() != b.scale()) {
163171
return false;
@@ -180,8 +188,9 @@ private static boolean bucketIteratorsEqual(BucketIterator a, BucketIterator b)
180188
static int hashCode(ExponentialHistogram histogram) {
181189
int hash = histogram.scale();
182190
hash = 31 * hash + Double.hashCode(histogram.sum());
183-
hash = 31 * hash + histogram.zeroBucket().hashCode();
184191
hash = 31 * hash + Long.hashCode(histogram.valueCount());
192+
hash = 31 * hash + Double.hashCode(histogram.min());
193+
hash = 31 * hash + histogram.zeroBucket().hashCode();
185194
// we intentionally don't include the hash of the buckets here, because that is likely expensive to compute
186195
// instead, we assume that the value count and sum are a good enough approximation in most cases to minimize collisions
187196
// the value count is typically available as a cached value and doesn't involve iterating over all buckets

libs/exponential-histogram/src/test/java/org/elasticsearch/exponentialhistogram/ExponentialHistogramEqualityTests.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public class ExponentialHistogramEqualityTests extends ExponentialHistogramTestC
4040
public enum Modification {
4141
SCALE,
4242
SUM,
43+
MIN,
4344
ZERO_THRESHOLD,
4445
ZERO_COUNT,
4546
POSITIVE_BUCKETS,
@@ -80,7 +81,7 @@ public void testHashQuality() {
8081
}
8182

8283
private ExponentialHistogram randomHistogram() {
83-
return createAutoReleasedHistogram(randomIntBetween(1, 20), randomDoubles(randomIntBetween(0, 200)).toArray());
84+
return createAutoReleasedHistogram(randomIntBetween(4, 20), randomDoubles(randomIntBetween(0, 200)).toArray());
8485
}
8586

8687
private ExponentialHistogram copyWithModification(ExponentialHistogram toCopy, Modification modification) {
@@ -96,6 +97,11 @@ private ExponentialHistogram copyWithModification(ExponentialHistogram toCopy, M
9697
} else {
9798
copy.setSum(toCopy.sum());
9899
}
100+
if (modification == Modification.MIN) {
101+
copy.setMin(randomDouble());
102+
} else {
103+
copy.setMin(toCopy.min());
104+
}
99105
long zeroCount = toCopy.zeroBucket().count();
100106
double zeroThreshold = toCopy.zeroBucket().zeroThreshold();
101107
if (modification == Modification.ZERO_COUNT) {
@@ -144,7 +150,7 @@ private void insertRandomBucket(List<Long> indices, List<Long> counts) {
144150
do {
145151
newIndex = randomLongBetween(Math.max(MIN_INDEX, minIndex - 10), Math.min(MAX_INDEX, maxIndex + 10));
146152
} while (indices.contains(newIndex));
147-
int position = Collections.binarySearch(indices, newIndex) + 1;
153+
int position = -(Collections.binarySearch(indices, newIndex) + 1);
148154
indices.add(position, newIndex);
149155
counts.add(position, randomLongBetween(1, Long.MAX_VALUE));
150156
}

libs/exponential-histogram/src/test/java/org/elasticsearch/exponentialhistogram/ExponentialHistogramUtilsTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public void testMinimumEstimation() {
113113
ExponentialHistogram histo = createAutoReleasedHistogram(bucketCount, values);
114114

115115
OptionalDouble estimatedMin = ExponentialHistogramUtils.estimateMin(
116-
new ZeroBucket(zeroThreshold, zeroValueCount),
116+
ZeroBucket.create(zeroThreshold, zeroValueCount),
117117
histo.negativeBuckets(),
118118
histo.positiveBuckets()
119119
);

0 commit comments

Comments
 (0)