Skip to content

fix long overflow in GCD-encoded DocValues range transform - #16896

Merged
romseygeek merged 17 commits into
apache:mainfrom
kkewwei:gcdEncodingOverflow
Nov 23, 2026
Merged

romseygeek merged 17 commits into
apache:mainfrom
kkewwei:gcdEncodingOverflow

Conversation

@kkewwei

@kkewwei kkewwei commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Description

In NumericDocValues, GCD encoding stores (value - min) / gcd, where min may be Long.MIN_VALUE. When querying the range [-1, 1], the query bounds are shifted by subtracting min. Both subtractions overflow and saturate to Long.MAX_VALUE, causing the encoded range to become [Long.MAX_VALUE, Long.MAX_VALUE] and producing incorrect results.

@kkewwei

kkewwei commented Oct 26, 2026

Copy link
Copy Markdown
Contributor Author

@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.

@costin

costin commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

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.
How about gating on the encoding instead?
Stored values only go negative when bitsPerValue == 64 so keep the saturating helpers for narrower fields and use the decoded path only for 64-bit ones.

Please also add open-bound queries and sparse sorted-numeric to the test.

@kkewwei kkewwei left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. 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

  2. delta < 0:
    minValue <= delta -> encodedMin = 0
    maxValue < delta -> empty range
    (minValue/maxValue) <= Long.MAX_VALUE + delta -> subtraction is safe
    otherwise -> fallback

  3. 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

@kkewwei

kkewwei commented Nov 4, 2026

Copy link
Copy Markdown
Contributor Author

@costin Would you mind take another look in your spare time?

@costin

costin commented Nov 11, 2026

Copy link
Copy Markdown
Contributor

LGTM.

Two nits:

  • add a randomized test for rangeIntoBitSet with more random values from a variety of values: extremes (Long.MIN/MAX_VALUE), dense, random gcd, etc... instead of the current fixed values.
  • consider allocating decodedValues lazily similar to its use instead of for every gcd-encoded field

@costin costin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@kkewwei

kkewwei commented Nov 13, 2026

Copy link
Copy Markdown
Contributor Author

@costin Thank you very much for your review! I've made the changes.

@romseygeek

Copy link
Copy Markdown
Contributor

Another DV range bug! I think we need to beef up our testing here.

I think this probably warrants a 10.6.1 release?

@kkewwei

kkewwei commented Nov 21, 2026

Copy link
Copy Markdown
Contributor Author

Very thank you for the review, I've placed it under 11.0.0 for now. please feel free to move it if needed.

@romseygeek
romseygeek merged commit 3cee9ed into apache:main Nov 23, 2026
13 checks passed
romseygeek pushed a commit that referenced this pull request Nov 23, 2026
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants