-
Notifications
You must be signed in to change notification settings - Fork 29.4k
[SPARK-59185][SQL] Derive a StartsWith prefix filter from leading-literal LIKE patterns #58484
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -813,6 +813,11 @@ object LikeSimplification extends Rule[LogicalPlan] with PredicateHelper { | |
| private val contains = "%+([^_%]+)%+".r | ||
| private val equalTo = "([^_%]*)".r | ||
|
|
||
| // Marks a residual `Like` that `derivePrefixStartsWith` has already guarded with a leading | ||
| // `StartsWith`, so the rule does not re-wrap it on later fixed-point iterations (which would | ||
| // otherwise loop and break the batch's idempotence). Tags are ignored by `fastEquals`. | ||
| private[sql] val LIKE_PREFIX_GUARDED = TreeNodeTag[Unit]("likePrefixStartsWithAdded") | ||
|
|
||
| private def simplifyLike( | ||
| input: Expression, pattern: String, escapeChar: Char = '\\'): Option[Expression] = { | ||
| if (pattern.contains(escapeChar)) { | ||
|
|
@@ -863,6 +868,43 @@ object LikeSimplification extends Rule[LogicalPlan] with PredicateHelper { | |
| } | ||
| } | ||
|
|
||
| // For a leading-literal pattern that `simplifyLike` leaves as a full `Like` (e.g. 'a%b%'), | ||
| // derive the necessary condition `StartsWith(input, <leading literal>)` and keep the `Like` | ||
| // as the exact residual: `StartsWith(input, prefix) && (input LIKE pattern)`. `StartsWith` is | ||
| // placed first so the cheap check short-circuits the regex, and it can be pushed to data | ||
| // sources (e.g. Parquet prunes row groups on `StringStartsWith`) while the `Like` re-checks | ||
| // the match exactly. | ||
| // | ||
| // Restricted to collations with binary equality: only then do the `Like` regex match and | ||
| // `StartsWith` agree byte-for-byte, so `StartsWith(prefix)` is a sound necessary condition of | ||
| // the `Like` (under e.g. UTF8_LCASE the two matchers can disagree, risking a false negative), | ||
| // and only then does `StringStartsWith` push down. The residual `Like` is tagged so the rule | ||
| // stays idempotent under the fixed-point batch. | ||
| private def derivePrefixStartsWith( | ||
| input: Expression, | ||
| pattern: String, | ||
| escapeChar: Char, | ||
| like: Expression): Option[Expression] = { | ||
| val binaryCollation = input.dataType match { | ||
| case st: StringType => st.supportsBinaryEquality | ||
| case _ => false | ||
| } | ||
| if (!binaryCollation || !CollapseProject.isCheap(input) || pattern.contains(escapeChar) || | ||
| like.containsTag(LIKE_PREFIX_GUARDED)) { | ||
| None | ||
| } else { | ||
| val prefix = pattern.takeWhile(c => c != '%' && c != '_') | ||
| if (prefix.isEmpty || prefix.length == pattern.length) { | ||
| // No leading literal (pattern starts with a wildcard), or no wildcard at all (the | ||
| // latter is already turned into `EqualTo` by `simplifyLike`). | ||
| None | ||
| } else { | ||
| like.setTagValue(LIKE_PREFIX_GUARDED, ()) | ||
| Some(And(StartsWith(input, Literal.create(prefix, input.dataType)), like)) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private def simplifyMultiLike( | ||
| child: Expression, patterns: Seq[UTF8String], multi: MultiLikeBase): Expression = { | ||
| val (remainPatternMap, replacementMap) = | ||
|
|
@@ -898,7 +940,10 @@ object LikeSimplification extends Rule[LogicalPlan] with PredicateHelper { | |
| // If pattern is null, return null value directly, since "col like null" == null. | ||
| Literal(null, BooleanType) | ||
| } else { | ||
| simplifyLike(input, pattern.toString, escapeChar).getOrElse(l) | ||
| val patternStr = pattern.toString | ||
| simplifyLike(input, patternStr, escapeChar) | ||
| .orElse(derivePrefixStartsWith(input, patternStr, escapeChar, l)) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This duplicates
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @stevomitric Updated. I have also identified another code path that lacked this gate: #58663 |
||
| .getOrElse(l) | ||
| } | ||
| case l @ LikeAll(child, patterns) if CollapseProject.isCheap(child) => | ||
| simplifyMultiLike(child, patterns, l) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this bails whenever the escape char appears anywhere in the pattern, but the leading literal can be escape-free while the escape only appears later, e.g. 'ab%c%d%', whose prefix ab is clean.
we could have something like: