Skip to content

Commit

Permalink
Fix Bug - handle unsigned long in assertion of LongHashSet
Browse files Browse the repository at this point in the history
Signed-off-by: Shailesh Singh <shaikumm@amazon.com>
  • Loading branch information
Shailesh Singh committed Jan 30, 2025
1 parent 2847695 commit 5844005
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
18 changes: 15 additions & 3 deletions server/src/main/java/org/apache/lucene/util/LongHashSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,12 @@ public final class LongHashSet implements Accountable {
/** maximum value in the set, or Long.MIN_VALUE for an empty set */
public final long maxValue;

/** Construct a set. Values must be in sorted order. */
public LongHashSet(long[] values) {
this(values, false);
}

Check warning on line 35 in server/src/main/java/org/apache/lucene/util/LongHashSet.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/apache/lucene/util/LongHashSet.java#L34-L35

Added lines #L34 - L35 were not covered by tests

/** Construct a set. Values must be in sorted order. */
public LongHashSet(long[] values, boolean isUnsignedLong) {
int tableSize = Math.toIntExact(values.length * 3L / 2);
tableSize = 1 << PackedInts.bitsRequired(tableSize); // make it a power of 2
assert tableSize >= values.length * 3L / 2;
Expand All @@ -40,15 +44,15 @@ public LongHashSet(long[] values) {
mask = tableSize - 1;
boolean hasMissingValue = false;
int size = 0;
long previousValue = Long.MIN_VALUE; // for assert
long previousValue = isUnsignedLong ? 0 : Long.MIN_VALUE; // for assert
for (long value : values) {
if (value == MISSING) {
size += hasMissingValue ? 0 : 1;
hasMissingValue = true;
} else if (add(value)) {
++size;
}
assert value >= previousValue : "values must be provided in sorted order";
assertBasedOnDataType(value, previousValue, isUnsignedLong);
previousValue = value;
}
this.hasMissingValue = hasMissingValue;
Expand All @@ -57,6 +61,14 @@ public LongHashSet(long[] values) {
this.maxValue = values.length == 0 ? Long.MIN_VALUE : values[values.length - 1];
}

private void assertBasedOnDataType(long value, long previousValue, boolean isUnsignedLong) {
if (isUnsignedLong) {
assert Long.compareUnsigned(value, previousValue) >= 0 : "values must be provided in sorted order";
} else {
assert value >= previousValue : "values must be provided in sorted order";
}
}

private boolean add(long l) {
assert l != MISSING;
final int slot = Long.hashCode(l) & mask;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public abstract class SortedUnsignedLongDocValuesSetQuery extends Query {
SortedUnsignedLongDocValuesSetQuery(String field, BigInteger[] numbers) {
this.field = Objects.requireNonNull(field);
Arrays.sort(numbers);
this.numbers = new LongHashSet(Arrays.stream(numbers).mapToLong(n -> n.longValue()).toArray());
this.numbers = new LongHashSet(Arrays.stream(numbers).mapToLong(n -> n.longValue()).toArray(), true);
}

@Override
Expand Down

0 comments on commit 5844005

Please sign in to comment.