[SPARK-57436][SQL] Support start and occurrence parameters in instr function
What changes were proposed in this pull request?
This PR extends the instr function to accept optional start and occurrence parameters, supporting forward and backward search with full collation awareness. The changes include:
- A new expression
StringInstrWithOccurrencethat handles 3‑ and 4‑argument invocations, while the existing two‑argumentinstrcontinues to useStringInstr. - A new
StringInstrExpressionBuilderthat routes calls based on the number of arguments: 2 arguments →StringInstr, 3 or 4 arguments →StringInstrWithOccurrence. - Extended
UTF8String.indexOf(pattern, start, occurrence)with both forward (positivestart) and backward (negativestart) search, using efficient single‑pass UTF‑8 byte scanning. - Extended
CollationAwareUTF8String.lowercaseIndexOfand ICUindexOfto support multi‑occurrence search forUTF8_BINARY_LCASEand ICU collations. - Updated
FunctionRegistryto use the newStringInstrExpressionBuilder. - New overloads in
functions.scalaand corresponding Python APIs (pyspark.sql.functions.builtin) with proper default‑parameter handling. - New error class
INVALID_PARAMETER_VALUE.OCCURRENCEfor invalid (≤ 0) occurrence values. - Comprehensive unit tests covering binary, lowercase, and ICU collations, forward/backward/overlapping searches, multi‑byte characters, and boundary conditions.
- Registered
StringInstrWithOccurrenceinCollationTypeCoercionto align collation handling for multi-arginstr. - Added
StringInstrWithOccurrenceto theResolverGuardallowlist so Spark Connect and the hybrid analyzer support it.
The behavior when substr is empty remains unchanged (returns start position), preserving backward compatibility with the existing two‑argument instr.
Why are the changes needed?
Currently, Spark's instr only supports two arguments, forcing users to write complex workarounds when they need to start searching from a specific position or find a particular occurrence. Oracle, Impala, and Db2 all provide a four‑argument INSTR with these capabilities. This PR aligns Spark SQL with those databases, simplifies user queries, and reduces migration friction.
Does this PR introduce any user-facing change?
Yes, but only additive.
- New SQL syntax:
instr(str, substr [, start [, occurrence]])wherestartandoccurrenceare optional integers (default 1). Negativestartperforms backward search. - New Scala/Python API overloads with corresponding optional parameters.
- An
INVALID_PARAMETER_VALUE.OCCURRENCEerror is thrown unconditionally whenoccurrence <= 0. - All existing two‑argument usage remains completely unaffected.
Documentation for the new parameters is included in the expression description and will be surfaced in SQL function docs.
How was this patch tested?
- Added extensive unit tests to
StringFunctionsSuite,CollationAwareUTF8StringSuite, andUTF8StringSuitecovering:- Forward and backward search with multiple occurrences
- Overlapping matches (e.g.,
'aa'in'aaa') - Multi‑byte and supplementary characters
- Collation scenarios:
UTF8_BINARY,UTF8_BINARY_LCASE,UNICODE_CI(including sigma variants, Germanß, and Turkishİexpansion) - Boundary conditions (
start = 0,occurrence <= 0,startout of range, empty substring)
- Verified Python API behavior matches SQL execution through existing PySpark doctests.
Was this patch authored or co-authored using generative AI tooling?
DeepSeek
@uros-b @sarutak Could you please take a look at this PR when you have a chance? Thanks!
There is one very important consideration here - missing
CollationTypeCoercionregistration for the new expression. Another important consideration isResolverGuardallowlist entry.
@uros-b Thanks for catching both the missing CollationTypeCoercion and ResolverGuard registrations! I've added StringInstrWithOccurrence to both match sites in CollationTypeCoercion.scala and to the allowlist in ResolverGuard.scala. I also updated the golden files and added a Connect test accordingly. Thanks!
thanks, merging to master/4.x