fix: prevent arithmetic overflow in U64Segment encoding selection for sparse/extreme row id ranges#6516
Open
ivscheianu wants to merge 3 commits intolance-format:mainfrom
Open
Conversation
… sparse/extreme row ID ranges
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
U64Segment::from_stats_and_sequencecrashes when row IDs span a large range or include values nearu64::MAX. Fixes #6515There are two independent overflow classes:
Cost estimation:
n_holes()andsorted_sequence_sizes()compute range spans inu64/usizethat wrap for large ranges, making infeasible encodings (RangeWithHoles, RangeWithBitmap) appear cheapest. The code then attempts to materialize billions of holes or allocate multi-exabyte bitmaps.Exclusive-end: All range-backed encodings construct
Range<u64>withstats.max + 1as the exclusive end. Whenmax == u64::MAX, this overflows even for small, memory-feasible sets (e.g.,[u64::MAX - 3, u64::MAX - 1, u64::MAX]).Both classes cause process aborts in debug and OOM in release. Across JNI this kills the JVM with no recoverable exception.
Fix
n_holes()→u128return type: The total slot countmax - min + 1can be up to2^64, which exceedsu64::MAX. Widening tou128gives the correct value instead of wrapping.sorted_sequence_sizes()→u128arithmetic: All cost estimates computed inu128with saturating arithmetic, then converted viausize::try_from(...).unwrap_or(usize::MAX). Infeasible encodings saturate and always lose themin()comparison.from_stats_and_sequence()→checked_add(1)gate:exclusive_end = stats.max.checked_add(1)computed once and used as a gate for all range-backed branches. WhenNone(i.e.,max == u64::MAX), falls through toSortedArray. The bare expressionstats.max + 1no longer appears in the function.