deniskuzZ commented on code in PR #4998:
URL: https://github.com/apache/hive/pull/4998#discussion_r1568668529


##########
ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/FilterStringColLikeStringScalar.java:
##########
@@ -51,93 +53,83 @@ public FilterStringColLikeStringScalar(int colNum, byte[] 
likePattern) {
 
   @Override
   protected List<CheckerFactory> getCheckerFactories() {
-    return checkerFactories;
+    return CHECKER_FACTORIES;
   }
 
-  /**
-   * Accepts simple LIKE patterns like "abc%" and creates corresponding 
checkers.
-   */
-  private static class BeginCheckerFactory implements CheckerFactory {
-    private static final Pattern BEGIN_PATTERN = Pattern.compile("([^_%]+)%");
-
-    public Checker tryCreate(String pattern) {
-      Matcher matcher = BEGIN_PATTERN.matcher(pattern);
-      if (matcher.matches()) {
-        return new BeginChecker(matcher.group(1));
+  private enum UDFLikePattern {
+    // Accepts simple LIKE patterns like "abc%" and creates corresponding 
checkers.
+    BEGIN(BeginChecker.class) {
+      @Override
+      public String format(String pattern) {
+        return pattern.substring(0, pattern.length() - 1);
       }
-      return null;
-    }
-  }
-
-  /**
-   * Accepts simple LIKE patterns like "%abc" and creates a corresponding 
checkers.
-   */
-  private static class EndCheckerFactory implements CheckerFactory {
-    private static final Pattern END_PATTERN = Pattern.compile("%([^_%]+)");
-
-    public Checker tryCreate(String pattern) {
-      Matcher matcher = END_PATTERN.matcher(pattern);
-      if (matcher.matches()) {
-        return new EndChecker(matcher.group(1));
+    },
+    // Accepts simple LIKE patterns like "%abc" and creates a corresponding 
checkers.
+    END(EndChecker.class) {
+      @Override
+      public String format(String pattern) {
+        return pattern.substring(1);
       }
-      return null;
-    }
-  }
-
-  /**
-   * Accepts simple LIKE patterns like "%abc%" and creates a corresponding 
checkers.
-   */
-  private static class MiddleCheckerFactory implements CheckerFactory {
-    private static final Pattern MIDDLE_PATTERN = 
Pattern.compile("%([^_%]+)%");
-
-    public Checker tryCreate(String pattern) {
-      Matcher matcher = MIDDLE_PATTERN.matcher(pattern);
-      if (matcher.matches()) {
-        return new MiddleChecker(matcher.group(1));
+    },
+    // Accepts simple LIKE patterns like "%abc%" and creates a corresponding 
checkers.
+    MIDDLE(MiddleChecker.class) {
+      @Override
+      public String format(String pattern) {
+        return pattern.substring(1, pattern.length() - 1);
       }
-      return null;
-    }
-  }
+    },
+    // Accepts any LIKE patterns and creates corresponding checkers.
+    COMPLEX(ComplexChecker.class) {
+      @Override
+      public String format(String pattern) {
+        return "^" + UDFLike.likePatternToRegExp(pattern) + "$";
+      }
+    },
+    // Accepts chained LIKE patterns without escaping like "abc%def%ghi%" and
+    // creates corresponding checkers.
+    CHAINED(ChainedChecker.class),
+    // Accepts simple LIKE patterns like "abc" and creates corresponding 
checkers.
+    NONE(NoneChecker.class);
 
-  /**
-   * Accepts simple LIKE patterns like "abc" and creates corresponding 
checkers.
-   */
-  private static class NoneCheckerFactory implements CheckerFactory {
-    private static final Pattern NONE_PATTERN = Pattern.compile("[^%_]+");
+    Class<? extends Checker> checker;
 
-    public Checker tryCreate(String pattern) {
-      Matcher matcher = NONE_PATTERN.matcher(pattern);
-      if (matcher.matches()) {
-        return new NoneChecker(pattern);
-      }
-      return null;
+    UDFLikePattern(Class<? extends Checker> checker) {
+      this.checker = checker;
     }
-  }
 
-  /**
-   * Accepts chained LIKE patterns without escaping like "abc%def%ghi%" and 
creates corresponding
-   * checkers.
-   *
-   */
-  private static class ChainedCheckerFactory implements CheckerFactory {
-    private static final Pattern CHAIN_PATTERN = 
Pattern.compile("(%?[^%_\\\\]+%?)+");
-
-    public Checker tryCreate(String pattern) {
-      Matcher matcher = CHAIN_PATTERN.matcher(pattern);
-      if (matcher.matches()) {
-        return new ChainedChecker(pattern);
+    private static UDFLikePattern matcher(String pattern) {
+      UDFLikePattern lastType = NONE;
+      int length = pattern.length();
+      char lastChar = 0;
+
+      for (int i = 0; i < length; i++) {
+        char n = pattern.charAt(i);
+        if (n == '_' && lastChar != '\\') { // such as "a_bc"
+          return COMPLEX;
+        } else if (n == '%') {
+          if (i == 0) { // such as "%abc"
+            lastType = END;
+          } else if (i < length - 1) {
+            if (lastChar != '\\') { // such as "a%bc"
+              lastType = CHAINED;
+            }
+          } else {
+            if (lastChar != '\\') {
+              if (lastType == END) { // such as "%abc%"
+                lastType = MIDDLE;
+              } else if (lastType != CHAINED) {
+                lastType = BEGIN; // such as "abc%"
+              }
+            }
+          }
+        }
+        lastChar = n;
       }
-      return null;
+      return lastType;
     }
-  }
 
-  /**
-   * Accepts any LIKE patterns and creates corresponding checkers.
-   */
-  private static class ComplexCheckerFactory implements CheckerFactory {
-    public Checker tryCreate(String pattern) {
-      // anchor the pattern to the start:end of the whole string.
-      return new ComplexChecker("^" + UDFLike.likePatternToRegExp(pattern) + 
"$");
+    public String format(String pattern) {

Review Comment:
   no need for `public`, package-private is enough, same for overrides



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