spark icon indicating copy to clipboard operation
spark copied to clipboard

[SPARK-57436][SQL] Support start and occurrence parameters in instr function

Open loftiest opened this issue 5 months ago • 2 comments

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 StringInstrWithOccurrence that handles 3‑ and 4‑argument invocations, while the existing two‑argument instr continues to use StringInstr.
  • A new StringInstrExpressionBuilder that 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 (positive start) and backward (negative start) search, using efficient single‑pass UTF‑8 byte scanning.
  • Extended CollationAwareUTF8String.lowercaseIndexOf and ICU indexOf to support multi‑occurrence search for UTF8_BINARY_LCASE and ICU collations.
  • Updated FunctionRegistry to use the new StringInstrExpressionBuilder.
  • New overloads in functions.scala and corresponding Python APIs (pyspark.sql.functions.builtin) with proper default‑parameter handling.
  • New error class INVALID_PARAMETER_VALUE.OCCURRENCE for invalid (≤ 0) occurrence values.
  • Comprehensive unit tests covering binary, lowercase, and ICU collations, forward/backward/overlapping searches, multi‑byte characters, and boundary conditions.
  • Registered StringInstrWithOccurrence in CollationTypeCoercion to align collation handling for multi-arg instr.
  • Added StringInstrWithOccurrence to the ResolverGuard allowlist 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]]) where start and occurrence are optional integers (default 1). Negative start performs backward search.
  • New Scala/Python API overloads with corresponding optional parameters.
  • An INVALID_PARAMETER_VALUE.OCCURRENCE error is thrown unconditionally when occurrence <= 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, and UTF8StringSuite covering:
    • 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, start out 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

loftiest avatar Jun 14 '26 13:06 loftiest

@uros-b @sarutak Could you please take a look at this PR when you have a chance? Thanks!

loftiest avatar Jun 16 '26 12:06 loftiest

There is one very important consideration here - missing CollationTypeCoercion registration for the new expression. Another important consideration is ResolverGuard allowlist 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!

loftiest avatar Jun 19 '26 13:06 loftiest

thanks, merging to master/4.x

cloud-fan avatar Jun 30 '26 13:06 cloud-fan