Skip to content

Commit

Permalink
Trying another approach for gracefully handling unsigned long convers…
Browse files Browse the repository at this point in the history
…ions

Signed-off-by: expani <[email protected]>
  • Loading branch information
expani committed Dec 27, 2024
1 parent d50e56e commit 7bc5cbf
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
4 changes: 2 additions & 2 deletions libs/common/src/main/java/org/opensearch/common/Numbers.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public final class Numbers {
public static final long MIN_UNSIGNED_LONG_VALUE_AS_LONG = MIN_UNSIGNED_LONG_VALUE.longValue();
public static final long MAX_UNSIGNED_LONG_VALUE_AS_LONG = MAX_UNSIGNED_LONG_VALUE.longValue();

private static final BigInteger MAX_LONG_VALUE = BigInteger.valueOf(Long.MAX_VALUE);
private static final BigInteger MIN_LONG_VALUE = BigInteger.valueOf(Long.MIN_VALUE);
public static final BigInteger MAX_LONG_VALUE = BigInteger.valueOf(Long.MAX_VALUE);
public static final BigInteger MIN_LONG_VALUE = BigInteger.valueOf(Long.MIN_VALUE);

private Numbers() {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import org.apache.lucene.search.Pruning;
import org.apache.lucene.search.comparators.NumericComparator;
import org.apache.lucene.util.BitUtil;
import org.apache.lucene.util.NumericUtils;

import java.io.IOException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ public LeafFieldComparator getLeafComparator(LeafReaderContext context) throws I

@Override
protected long missingValueAsComparableLong() {
return missingValue.longValue();
return unsignedLongToSignedLong(missingValue);
}

@Override
protected long sortableBytesToLong(byte[] bytes) {
return NumericUtils.sortableBytesToBigInt(bytes, 0, BigIntegerPoint.BYTES).longValue();
return unsignedLongToSignedLong(NumericUtils.sortableBytesToBigInt(bytes, 0, BigIntegerPoint.BYTES));
}

/** Leaf comparator for {@link UnsignedLongComparator} that provides skipping functionality */
Expand Down Expand Up @@ -100,12 +100,27 @@ public void copy(int slot, int doc) throws IOException {

@Override
protected long bottomAsComparableLong() {
return bottom.longValue();
return unsignedLongToSignedLong(bottom);
}

@Override
protected long topAsComparableLong() {
return topValue.longValue();
return unsignedLongToSignedLong(topValue);
}
}

/**
* Handles overflow in conversion.
* @param value : Unsigned long
* @return : Signed long representing value's position in number line as much as possible within bounds.
*/
private static long unsignedLongToSignedLong(BigInteger value) {
if (value.compareTo(Numbers.MIN_LONG_VALUE) < 0) {
return Long.MIN_VALUE;
} else if (value.compareTo(Numbers.MAX_LONG_VALUE) > 0) {
return Long.MAX_VALUE;
} else {
return value.longValue();
}
}
}

0 comments on commit 7bc5cbf

Please sign in to comment.