panbingkun commented on code in PR #43751:
URL: https://github.com/apache/spark/pull/43751#discussion_r1505253654


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/StringUtils.scala:
##########
@@ -107,27 +107,82 @@ object StringUtils extends Logging {
   def isFalseString(s: UTF8String): Boolean = 
falseStrings.contains(s.trimAll().toLowerCase)
   // scalastyle:on caselocale
 
+  def getAllMatchWildcard: String = {
+    if (SQLConf.get.legacyUseStarAndVerticalBarAsWildcardsInLikePattern) {
+      "*"
+    } else {
+      "%"
+    }
+  }
+
+  def filterPattern(names: Seq[String], pattern: String): Seq[String] = {
+    if (SQLConf.get.legacyUseStarAndVerticalBarAsWildcardsInLikePattern) {
+      filterPatternLegacy(names, pattern)
+    } else {
+      filterBySQLLikePattern(names, pattern)
+    }
+  }
+
   /**
-   * This utility can be used for filtering pattern in the "Like" of "Show 
Tables / Functions" DDL
+   * This legacy utility can be used for filtering pattern in the "Like" of
+   * "Show Tables / Functions" DDL.
    * @param names the names list to be filtered
    * @param pattern the filter pattern, only '*' and '|' are allowed as 
wildcards, others will
    *                follow regular expression convention, case insensitive 
match and white spaces
    *                on both ends will be ignored
    * @return the filtered names list in order
    */
-  def filterPattern(names: Seq[String], pattern: String): Seq[String] = {
+  def filterPatternLegacy(names: Seq[String], pattern: String): Seq[String] = {
     val funcNames = scala.collection.mutable.SortedSet.empty[String]
     pattern.trim().split("\\|").foreach { subPattern =>
       try {
         val regex = ("(?i)" + subPattern.replaceAll("\\*", ".*")).r
-        funcNames ++= names.filter{ name => 
regex.pattern.matcher(name).matches() }
+        funcNames ++= names.filter { name => 
regex.pattern.matcher(name).matches() }
       } catch {
         case _: PatternSyntaxException =>
       }
     }
     funcNames.toSeq
   }
 
+  /**
+   * This utility can be used for filtering pattern in the "Like" of "Show 
Tables / Functions" DDL.
+   * @param names the names list to be filtered
+   * @param pattern the filter pattern, SQL type like expressions:
+   *                '%' for any character(s), and '_' for a single character
+   * @return the filtered names list
+   */
+  def filterBySQLLikePattern(names: Seq[String], pattern: String): Seq[String] 
= {
+    try {
+      val p = Pattern.compile(likePatternToRegExp(pattern), 
Pattern.CASE_INSENSITIVE)
+      names.filter { name => p.matcher(name).matches() }
+    } catch {
+      case _: PatternSyntaxException => Seq.empty[String]
+    }
+  }
+
+  private[util] def likePatternToRegExp(pattern: String): String = {

Review Comment:
   Follow the implementation logic of hive:
   
https://github.com/apache/hive/blob/89005659d1d8e167208ba4f9f9aaa2de7703229d/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFLike.java#L64-L86



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to