Github user marmbrus commented on a diff in the pull request:
https://github.com/apache/spark/pull/224#discussion_r11002505
--- Diff:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/stringOperations.scala
---
@@ -19,11 +19,113 @@ 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
+
+
+/**
+ * Thrown when an invalid RegEx string is found.
+ */
+class InvalidRegExException[TreeType <: TreeNode[_]](tree: TreeType,
reason: String) extends
+ errors.TreeNodeException(tree, s"$reason", null)
+
+trait StringRegexExpression {
+ self: BinaryExpression =>
+
+ type EvaluatedType = Any
+
+ def escape(v: String): String
+ 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: Any): Pattern = str match {
+ // TODO or let it be null if couldn't compile the regex?
+ case x: String if(x != null) => Pattern.compile(escape(x))
+ case x: String => null
+ case _ => throw new InvalidRegExException(this, "$str can not be
compiled to regex pattern")
+ }
+
+ protected def pattern(str: String) = if(cache == null) compile(str) else
cache
+
+ protected def filter: PartialFunction[(Row, (String, String)), Any] = {
+ case (row, (null, r)) => { false }
--- End diff --
A few comments on this function:
- I think you are already checking for null values below, so these cases
will never match.
- In match statements you can use _ to denote values where you do not need
to check the value or use it.
- I'm not sure why this is a partial function. Since you are lifting the
partial function and using `get` to retrieve the result, it ends up being
equivalent to just using a `match` statement, which would be syntactically much
simpler.
- The use of tuples as arguments to functions should be discouraged.
Where there is a very clear `(key,value)` relationship or when they are only
used locally it is maybe okay, but here it is very difficult to trace what the
arguments to this function are supposed to be.
- Given the above, I'd remove this PartialFunction and just embed the last
case in the `apply` function.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---