Repository navigation
fix long overflow in GCD-encoded DocValues range transform - #16896
Conversation
|
@costin Sorry to bother you — I saw that you have optimized the code before, could you help review in your spare time? Very thank you, and sorry for the ping. |
|
Thanks for looking into this. I've noticed the overflow check is per query rather than per field, so common open-ended ranges also lose the encoded path: ts <= x arrives as [Long.MIN_VALUE, x] and overflows whenever min > 0, and v >= x overflows whenever min < 0. Please also add open-bound queries and sparse sorted-numeric to the test. |
kkewwei
left a comment
There was a problem hiding this comment.
@costin Very thank you for the detailed review and insightful feedback.
I’ve refined the boundary checks to allow some cases withbitsPerValue == 64 to safely userangeGcdDeltaIntoBitSet. Please see the comments for details.
| } | ||
|
|
||
| /** Checks whether query bounds can be safely transformed to encoded bounds. */ | ||
| static boolean canTransformGcdBounds(long minValue, long maxValue, long delta, int bitsPerValue) { |
There was a problem hiding this comment.
-
delta >= 0:
minValue <= delta -> encodedMin = 0
maxValue < delta -> empty range
otherwise -> subtraction is safe
We can see it's aways safe to transform gcdBounds for the case -
delta < 0:
minValue <= delta -> encodedMin = 0
maxValue < delta -> empty range
(minValue/maxValue) <= Long.MAX_VALUE + delta -> subtraction is safe
otherwise -> fallback -
delta < 0 && maxValue == Long.MAX_VALUE:
bitsPerValue < 64 -> encodedMax = Long.MAX_VALUE
otherwise -> fallback
Special case3: when bitsPerValue < 64, the stored value must be < Long.MAX_VALUE, encodedMax = Long.MAX_VALUE is safe
|
@costin Would you mind take another look in your spare time? |
|
LGTM. Two nits:
|
|
@costin Thank you very much for your review! I've made the changes. |
|
Another DV range bug! I think we need to beef up our testing here. I think this probably warrants a 10.6.1 release? |
|
Very thank you for the review, I've placed it under 11.0.0 for now. please feel free to move it if needed. |
In NumericDocValues, GCD encoding stores (value - min) / gcd, where min may be Long.MIN_VALUE. When querying the range [-1, 1], the query bounds were shifted by subtracting min. Both subtractions overflowed and saturated to Long.MAX_VALUE, causing the encoded range to become [Long.MAX_VALUE, Long.MAX_VALUE] and producing incorrect results.
Description
In
NumericDocValues, GCD encoding stores(value - min) / gcd, whereminmay beLong.MIN_VALUE. When querying the range[-1, 1], the query bounds are shifted by subtractingmin. Both subtractions overflow and saturate toLong.MAX_VALUE, causing the encoded range to become[Long.MAX_VALUE, Long.MAX_VALUE]and producing incorrect results.