[SPARK-58443][SQL] Fix wrong results/exception in instr and substring_index for an Integer.MIN_VALUE position or count - #57648
Open
jiwen624 wants to merge 2 commits into
Open
[SPARK-58443][SQL] Fix wrong results/exception in instr and substring_index for an Integer.MIN_VALUE 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
jiwen624
force-pushed
the
SPARK-58443
branch
3 times, most recently
from
August 1, 2026 04:06
9b69733 to
b4709df
Compare
…index and all collations
jiwen624
marked this pull request as ready for review
August 1, 2026 06:01
uros-b
approved these changes
Aug 1, 2026
Member
|
Thank you @jiwen624! |
HyukjinKwon
reviewed
Aug 5, 2026
HyukjinKwon
left a comment
Member
There was a problem hiding this comment.
0 blocking, 0 non-blocking, 0 nits.
Correct, minimal integer-overflow edge fix applied consistently across the binary and ICU-collation paths.
Verification
Confirmed the overflow root cause (int negation of MIN_VALUE) and that widening to long before negation removes it, making MIN_VALUE behave like any other large-magnitude negative per the function contract. Applied identically in both string implementations; tests cover MIN_VALUE across UTF8_BINARY/LCASE/UNICODE/UNICODE_CI.
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_VALUEwraps 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 performs the negation in
long(-(long) start/-(long) count), soInteger.MIN_VALUEstays positive after negation and flows through the same range checks and search loops as every other negative value. No value is special-cased, and the loops still terminate as soon as the string runs out of characters or delimiters.UTF8String.indexOf(pattern, start, occurrence)instrstart = -1instead of returning 0UTF8String.subStringIndexsubstring_indexNegativeArraySizeExceptionCollationAwareUTF8String.lowercaseSubStringIndexsubstring_indexCollationAwareUTF8String.subStringIndexsubstring_indexAssertionErrorunder-ea; correct otherwiseThe last row is latent: assertions are enabled in Spark's test JVMs but not in deployments, so it is hardening rather than a user-visible defect.
Why are the changes needed?
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