Skip to content

[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
apache:masterfrom
jiwen624:SPARK-58443
Draft

[SPARK-58443][SQL] Fix wrong results in instr and substring_index for an Int.MinValue position or count#57648
jiwen624 wants to merge 2 commits into
apache:masterfrom
jiwen624:SPARK-58443

Conversation

@jiwen624

@jiwen624 jiwen624 commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

What changes were proposed in this pull request?

Four backward-search paths negate a user-supplied int and use the result as a number of characters or delimiters to skip. -Integer.MIN_VALUE overflows back to Integer.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 n bytes holds at most n characters and at most n non-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.

Site Function Collations Behavior before
UTF8String.indexOf(pattern, start, occurrence) instr UTF8_BINARY, UTF8_LCASE (all-ASCII fast path) position of the last match instead of 0
UTF8String.subStringIndex substring_index UTF8_BINARY NegativeArraySizeException
CollationAwareUTF8String.lowercaseSubStringIndex substring_index UTF8_LCASE empty string
CollationAwareUTF8String.subStringIndex substring_index ICU AssertionError when assertions are enabled; correct without them only by falling through assert(count >= 0)

Why are the changes needed?

SELECT instr('abcabc', 'abc', -2147483648, 1);      -- returns 4, expected 0
SELECT substring_index('a.b.c', '.', -2147483648);  -- NegativeArraySizeException, expected a.b.c
SELECT substring_index(collate('a.b.c', 'UTF8_LCASE'), '.', -2147483648);  -- returns '', expected a.b.c

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

…lue start due to negation overflow in UTF8String.indexOf
@jiwen624 jiwen624 changed the title [SPARK-58443][SQL] Fix instr returning a wrong position for Int.MinValue start due to negation overflow in UTF8String.indexOf [SPARK-58443][SQL] Fix wrong results in instr and substring_index for an Int.MinValue position or count Jul 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant