-
Notifications
You must be signed in to change notification settings - Fork 28.9k
Implement the RLike & Like in catalyst #224
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2c8929e
Update the unit test for expression evaluation
chenghao-intel 91cfd33
add implementation for rlike/like
chenghao-intel 319edb7
change to spark code style
chenghao-intel aeeb1d7
Simplify the implementation/unit test of RLike/Like
chenghao-intel 84f72e9
fix bug in RLike/Like & Simplify the unit test
chenghao-intel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,11 +19,103 @@ package org.apache.spark.sql | |
| package catalyst | ||
| package expressions | ||
|
|
||
| import java.util.regex.Pattern | ||
|
|
||
| import org.apache.spark.sql.catalyst.types.DataType | ||
| import org.apache.spark.sql.catalyst.types.StringType | ||
| import org.apache.spark.sql.catalyst.types.BooleanType | ||
| import org.apache.spark.sql.catalyst.trees.TreeNode | ||
| import org.apache.spark.sql.catalyst.errors.`package`.TreeNodeException | ||
|
|
||
|
|
||
| trait StringRegexExpression { | ||
| self: BinaryExpression => | ||
|
|
||
| type EvaluatedType = Any | ||
|
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.
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, forgot that evaluation result can be |
||
|
|
||
| def escape(v: String): String | ||
| def matches(regex: Pattern, str: String): Boolean | ||
|
|
||
| def nullable: Boolean = true | ||
| def dataType: DataType = BooleanType | ||
|
|
||
| // try cache the pattern for Literal | ||
| private lazy val cache: Pattern = right match { | ||
| case x @ Literal(value: String, StringType) => compile(value) | ||
| case _ => null | ||
| } | ||
|
|
||
| protected def compile(str: String): Pattern = if(str == null) { | ||
| null | ||
| } else { | ||
| // Let it raise exception if couldn't compile the regex string | ||
| Pattern.compile(escape(str)) | ||
| } | ||
|
|
||
| case class Like(left: Expression, right: Expression) extends BinaryExpression { | ||
| def dataType = BooleanType | ||
| def nullable = left.nullable // Right cannot be null. | ||
| protected def pattern(str: String) = if(cache == null) compile(str) else cache | ||
|
|
||
| override def apply(input: Row): Any = { | ||
| val l = left.apply(input) | ||
| if(l == null) { | ||
| null | ||
| } else { | ||
| val r = right.apply(input) | ||
| if(r == null) { | ||
| null | ||
| } else { | ||
| val regex = pattern(r.asInstanceOf[String]) | ||
| if(regex == null) { | ||
| null | ||
| } else { | ||
| matches(regex, l.asInstanceOf[String]) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Simple RegEx pattern matching function | ||
| */ | ||
| case class Like(left: Expression, right: Expression) | ||
| extends BinaryExpression with StringRegexExpression { | ||
|
|
||
| def symbol = "LIKE" | ||
|
|
||
| // replace the _ with .{1} exactly match 1 time of any character | ||
| // replace the % with .*, match 0 or more times with any character | ||
| override def escape(v: String) = { | ||
| val sb = new StringBuilder() | ||
| var i = 0; | ||
| while (i < v.length) { | ||
| // Make a special case for "\\_" and "\\%" | ||
| val n = v.charAt(i); | ||
| if (n == '\\' && i + 1 < v.length && (v.charAt(i + 1) == '_' || v.charAt(i + 1) == '%')) { | ||
| sb.append(v.charAt(i + 1)) | ||
| i += 1 | ||
| } else { | ||
| if (n == '_') { | ||
| sb.append("."); | ||
| } else if (n == '%') { | ||
| sb.append(".*"); | ||
| } else { | ||
| sb.append(Pattern.quote(Character.toString(n))); | ||
| } | ||
| } | ||
|
|
||
| i += 1 | ||
| } | ||
|
|
||
| sb.toString() | ||
| } | ||
|
|
||
| override def matches(regex: Pattern, str: String): Boolean = regex.matcher(str).matches() | ||
| } | ||
|
|
||
| case class RLike(left: Expression, right: Expression) | ||
| extends BinaryExpression with StringRegexExpression { | ||
|
|
||
| def symbol = "RLIKE" | ||
| override def escape(v: String): String = v | ||
| override def matches(regex: Pattern, str: String): Boolean = regex.matcher(str).find(0) | ||
| } | ||
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
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
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.
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.
Please add scala doc to this trait and the classes below.