java.lang.Object
com.darkcollective.relix.ast.internal.LikePatterns

public final class LikePatterns extends Object
Translation of a SQL LIKE pattern into an anchored regular expression.

% becomes .* and _ becomes .; every regex metacharacter is escaped so it matches literally; the result is anchored with ^…$ so it matches the whole value, as LIKE does.

This lives beside PatternPredicate because more than one consumer needs it — the in-engine predicate evaluator and the MongoDB $regex renderer — and they previously carried byte-identical private copies, including the same hand-typed metacharacter set. They agreed only by luck.

Known divergences from SQL LIKE

A σ that is pushed down to a SQL backend is evaluated by that backend's own LIKE, not by this translation, so the two are not interchangeable in every case. The differences, all pre-existing:

  • Case sensitivity. This translation is case-sensitive. MySQL's LIKE is case-insensitive under its default collation.
  • Newlines. _ maps to regex ., which does not match a line terminator; SQL's _ matches any character.
  • Escape clause. LIKE … ESCAPE is not modelled, so a backslash in a pattern is treated as a literal backslash rather than as an escape for a following % or _.
Centralising the translation here is what makes those differences visible in one place and fixable in one place.
  • Method Details

    • toRegex

      public static String toRegex(String pattern)
      Converts a SQL LIKE pattern to an anchored regular expression.
      Parameters:
      pattern - the LIKE pattern, using % and _ wildcards; must not be null
      Returns:
      an anchored regex matching the same values, e.g. a% becomes ^a.*$