-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-14545][SQL] Improve LikeSimplification by adding a%b rule
#12312
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
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 |
|---|---|---|
|
|
@@ -519,22 +519,28 @@ object LikeSimplification extends Rule[LogicalPlan] { | |
| // Cases like "something\%" are not optimized, but this does not affect correctness. | ||
| private val startsWith = "([^_%]+)%".r | ||
| private val endsWith = "%([^_%]+)".r | ||
| private val startsAndEndsWith = "([^_%]+)%([^_%]+)".r | ||
| private val contains = "%([^_%]+)%".r | ||
| private val equalTo = "([^_%]*)".r | ||
|
|
||
| def apply(plan: LogicalPlan): LogicalPlan = plan transformAllExpressions { | ||
| case Like(l, Literal(utf, StringType)) => | ||
| utf.toString match { | ||
| case startsWith(pattern) if !pattern.endsWith("\\") => | ||
| StartsWith(l, Literal(pattern)) | ||
| case endsWith(pattern) => | ||
| EndsWith(l, Literal(pattern)) | ||
| case contains(pattern) if !pattern.endsWith("\\") => | ||
| Contains(l, Literal(pattern)) | ||
| case equalTo(pattern) => | ||
| EqualTo(l, Literal(pattern)) | ||
| case Like(input, Literal(pattern, StringType)) => | ||
| pattern.toString match { | ||
| case startsWith(prefix) if !prefix.endsWith("\\") => | ||
| StartsWith(input, Literal(prefix)) | ||
| case endsWith(postfix) => | ||
| EndsWith(input, Literal(postfix)) | ||
| // 'a%a' pattern is basically same with 'a%' && '%a'. | ||
| // However, the additional `Length` condition is required to prevent 'a' match 'a%a'. | ||
| case startsAndEndsWith(prefix, postfix) if !prefix.endsWith("\\") => | ||
|
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. while you are at it, can you rename "pattern" in the startsWith case to prefix, and endsWith to suffix?
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. also rename utf to "pattern" and just compute the value of
Member
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. Sure. I will renames those parameters.
Member
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. Thank you for spending time here. I know your are very busy.
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. ah ok - let's leave the l one there |
||
| And(GreaterThanOrEqual(Length(input), Literal(prefix.size + postfix.size)), | ||
| And(StartsWith(input, Literal(prefix)), EndsWith(input, Literal(postfix)))) | ||
| case contains(infix) if !infix.endsWith("\\") => | ||
| Contains(input, Literal(infix)) | ||
| case equalTo(str) => | ||
| EqualTo(input, Literal(str)) | ||
| case _ => | ||
| Like(l, Literal.create(utf, StringType)) | ||
| Like(input, Literal.create(pattern, StringType)) | ||
| } | ||
| } | ||
| } | ||
|
|
||
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.
i think you should have some comments explaining this rewrite, in particular the greaterthanorequal part is not as straightforward as the other ones.
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.
Thank you for review. I'll add comments here.