Github user liancheng commented on a diff in the pull request:

    https://github.com/apache/spark/pull/2908#discussion_r19284542
  
    --- Diff: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/stringOperations.scala
 ---
    @@ -103,21 +103,21 @@ case class Like(left: Expression, right: Expression)
       // 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;
    +    val sb = new StringBuilder("""(?s)""")
    +    var i = 0
         while (i < v.length) {
           // Make a special case for "\\_" and "\\%"
    -      val n = v.charAt(i);
    +      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(".");
    +          sb.append(".")
             } else if (n == '%') {
    -          sb.append(".*");
    +          sb.append(".*")
             } else {
    -          sb.append(Pattern.quote(Character.toString(n)));
    +          sb.append(Pattern.quote(Character.toString(n)))
             }
           }
    --- End diff --
    
    I feel a little complicated about this... This function is not on critical 
path, so I'd like to refactor it in a more functional and readable (but less 
efficient) way, for example:
    
    ```scala
      override def escape(v: String) = "(?s)" + (' ' +: v.init).zip(v).flatMap {
        case (prefix, '_') => if (prefix == '\\') "_" else "."
        case (prefix, '%') => if (prefix == '\\') "%" else ".*"
        case (_, ch) => Character.toString(ch)
      }.mkString
    ```


---
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]

Reply via email to