Github user viirya commented on a diff in the pull request:
https://github.com/apache/spark/pull/7561#discussion_r35082615
--- Diff:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/stringOperations.scala
---
@@ -187,6 +187,64 @@ case class Like(left: Expression, right: Expression)
override def matches(regex: Pattern, str: String): Boolean =
regex.matcher(str).matches()
override def toString: String = s"$left LIKE $right"
+
+ override protected def genCode(ctx: CodeGenContext, ev:
GeneratedExpressionCode): String = {
+ val patternClass = classOf[Pattern].getName
+
+ val literalRight: String = right match {
+ case x @ Literal(value: String, StringType) => escape(value)
+ case _ => null
+ }
+
+ val leftGen = left.gen(ctx)
+ val rightGen = right.gen(ctx)
+
+ val patternCode =
+ if (literalRight != null) {
+ s"${patternClass} pattern = $patternClass.compile($literalRight);"
+ } else {
+ s"""
+ StringBuilder regex = new StringBuilder("(?s)");
+ for (int idx = 1; idx < rightStr.length(); idx++) {
+ char prev = rightStr.charAt(idx - 1);
+ char curr = rightStr.charAt(idx);
+ if (prev == '\\\\') {
+ if (curr == '_') {
+ regex.append("_");
+ } else if (curr == '%') {
+ regex.append("%");
+ } else {
+ regex.append(${patternClass}.quote("\\\\" + curr));
+ }
+ } else {
+ if (curr != '\\\\') {
+ if (curr == '_') {
+ regex.append(".");
+ } else if (curr == '%') {
+ regex.append(".*");
+ } else {
+ regex.append(${patternClass}.quote((new
Character(curr)).toString()));
+ }
+ }
+ }
+ }
+ ${patternClass} pattern =
${patternClass}.compile(regex.toString());
+ """
+ }
--- End diff --
OK. I got your point. I was thinking to have inlined a java version of
escape for better performance. I will put the current `escape` codes to other
place and reuse it.
---
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.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]