[SPARK-59185][SQL] Derive a StartsWith prefix filter from leading-literal LIKE patterns - #58484
Open
david-mollitor-db wants to merge 1 commit into
Open
Conversation
…eral LIKE patterns
`LikeSimplification` rewrites simple `LIKE` patterns into cheaper predicates
(`'A%'` -> `StartsWith`, `'%B'` -> `EndsWith`, `'A%B'` -> length guard + `StartsWith` +
`EndsWith`, `'%B%'` -> `Contains`, exact -> `EqualTo`). Multi-wildcard patterns with a
leading literal that match none of those shapes -- e.g. `'A%B%'`, `'AB%CD%EF'`, `'A_B%'`
-- fall through unchanged and stay a full regex `Like`, so the data source receives no
predicate and the per-row regex runs on every row.
This derives the leading literal `A` as the necessary condition `StartsWith(col, A)` and
keeps the original `LIKE` as the exact residual:
col LIKE 'A%B%' ==> StartsWith(col, A) && (col LIKE 'A%B%')
`StartsWith` is placed first so the cheap check short-circuits the regex, and it is a
predicate the existing pushdown path understands: on UTF8_BINARY it translates to
`StringStartsWith`, which prunes Parquet row groups via min/max. Results are unchanged --
`StartsWith(A)` is implied by `LIKE 'A%...'` and the exact `LIKE` is retained as the residual.
The derivation is restricted to binary-equality collations (`supportsBinaryEquality`):
under a collation-aware collation the `Like` regex match (Java regex case flags) and
`StartsWith` (`CollationSupport`) can disagree, so `StartsWith(A)` would not be a sound
necessary condition; and `StringStartsWith` only pushes down for UTF8_BINARY. Only
`StartsWith` is derived from the leading literal -- Parquet's `StringEndsWith` and
`StringContains` do not prune -- and the `LikeAll`/`LikeAny` paths are unchanged. A
`TreeNodeTag` on the residual `Like` keeps the rule idempotent under the fixed-point batch.
Generated-by: Claude Opus 4.8
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?
LikeSimplificationrewrites simpleLIKEpatterns into cheaper predicates ('A%'->StartsWith,'%B'->EndsWith,'A%B'-> length guard +StartsWith+EndsWith,'%B%'->Contains, exact string ->EqualTo). Multi-wildcard patterns that have aleading literal but match none of those shapes -- e.g.
'A%B%','AB%CD%EF','A_B%'--fall through unchanged and remain a full regex
Like, so the data source receives nopredicate and the per-row regex runs on every row.
This PR makes
LikeSimplificationadditionally derive the leading literalAas thenecessary condition
StartsWith(col, A), keeping the originalLIKEas the exact residual:StartsWithis placed first so the cheap check short-circuits the regex. The derivation isrestricted to binary-equality collations (
StringType.supportsBinaryEquality, i.e.UTF8_BINARY) and aTreeNodeTagon the residualLikekeeps the rule idempotent under thefixed-point optimizer batch. Only
StartsWithis derived from the leading literal; theLikeAll/LikeAnypaths are unchanged.Why are the changes needed?
UTF8_BINARY,StartsWithtranslates tosources.StringStartsWith, which prunes Parquet row groups via min/max statistics. Readerscannot prune on the raw
Like, so today a leading-literal multi-wildcardLIKEreads everyrow group.
StartsWithshort-circuits the more expensive regex onrows that fail the prefix.
StartsWith(A)is implied byLIKE 'A%...', and the exactLIKEis retained as the residual, so the conjunction accepts exactly the same rows. This mirrors
how PostgreSQL, SQL Server and SQLite turn a leading-literal
LIKEinto a sargable prefixpredicate plus a residual recheck.
The derivation is gated on binary equality for correctness as well as benefit: under a
collation-aware collation (e.g.
UTF8_LCASE) theLikeregex match (Java regex case flags)and
StartsWith(CollationSupport) can disagree, soStartsWith(A)would not be a soundnecessary condition; and
StringStartsWithis pushed down only forUTF8_BINARY(non-binaryis wrapped as
CollatedStringStartsWith, which readers ignore).EndsWith/Containsare notderived from trailing/inner literals because Parquet's
StringEndsWith/StringContainshavecanDrop = false(no pruning).Does this PR introduce any user-facing change?
No. Query results are identical; this is a performance improvement (added pushdown and a
short-circuit conjunct on an otherwise unsimplified
LIKE).How was this patch tested?
LikeSimplificationSuitecovering: derivation for'a%b%', a multi-charprefix with multiple wildcards,
'_'patterns, no derivation when there is no leading literal,no derivation when the pattern contains the escape char, no derivation for a non-binary
(
UTF8_LCASE) collation, and idempotency.ParquetFilterSuiteverifying thatLIKE 'ab%cd%'/'ab%cd%ef'/'a_b%'push aStringStartsWithand prune Parquet row groups.build/sbt 'catalyst/testOnly *LikeSimplificationSuite'andbuild/sbt 'sql/testOnly *ParquetV1FilterSuite -- -z "leading-literal"'pass; scalastyle clean.Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Opus 4.8