[SPARK-58443][SQL] Fix wrong results in instr and substring_index for an Int.MinValue position or count - #57648
Draft
jiwen624 wants to merge 2 commits into
Draft
[SPARK-58443][SQL] Fix wrong results in instr and substring_index for an Int.MinValue position or count#57648jiwen624 wants to merge 2 commits into
jiwen624 wants to merge 2 commits into
Conversation
…lue start due to negation overflow in UTF8String.indexOf
…index and all collations
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.
What changes were proposed in this pull request?
Four backward-search paths negate a user-supplied
intand use the result as a number of characters or delimiters to skip.-Integer.MIN_VALUEoverflows back toInteger.MIN_VALUE, so the value stays negative: the search loop never runs and the out-of-range check that should have fired is bypassed.Each site now returns the not-found result up front. A string of
nbytes holds at mostncharacters and at mostnnon-overlapping delimiters, so any position or count with a larger magnitude is out of range regardless of overflow. The guard is therefore a range check rather than a special case for a single value, and it also short-circuits the backward scan for every other out-of-range negative input.UTF8String.indexOf(pattern, start, occurrence)instrUTF8String.subStringIndexsubstring_indexNegativeArraySizeExceptionCollationAwareUTF8String.lowercaseSubStringIndexsubstring_indexCollationAwareUTF8String.subStringIndexsubstring_indexAssertionErrorwhen assertions are enabled; correct without them only by falling throughassert(count >= 0)Why are the changes needed?
Every other out-of-range position or count is handled correctly -- instr('abcabc', 'abc', -7, 1) returns 0 and substring_index('a.b.c', '.', -9) returns a.b.c -- so Int.MinValue is the one input where these functions diverge, and they diverge differently per collation, making the same query return different answers under UTF8_BINARY, UTF8_LCASE and ICU.
The substring_index defects affect released versions. instr with a start and occurrence is new in master (SPARK-57436) and has not been released.
Does this PR introduce any user-facing change?
Yes - bug fix
How was this patch tested?
New test cases added
Was this patch authored or co-authored using generative AI tooling?
Yes