Skip to content

Bug: Spark RLIKE regex escapes are removed in SQLGlot 30.21.0 #8554

Closed

Description

@ptmertka

In SQLGlot 30.21.0, parsing Snowflake SQL with RLIKE appears to remove backslashes from escaped regex characters. This changes the meaning of the regex and can produce invalid or incorrect Snowflake SQL.

Minimal reproduction:

import sqlglot
from sqlglot import parse_one

sql = r"""
SELECT *
FROM transactions
WHERE merchant_description RLIKE '.*(PAYRO?LL|SALARY|FED SAL|reg(\s*|-|\.|\*)salary|Payrol)'
"""

expression = parse_one(sql, read="spark")
print(expression.sql(dialect="spark"))

Input pattern:

 '.*(PAYRO?LL|SALARY|FED SAL|reg(\s*|-|\.|\*)salary|Payrol)'

Expected behavior:

SQLGlot should preserve the regex escapes when parsing and rendering Snowflake SQL, for example:

RLIKE  '.*(PAYRO?LL|SALARY|FED SAL|reg(\s*|-|\.|\*)salary|Payrol)'

Actual behavior in 30.21.0:

The escaped regex tokens lose their backslashes, changing:

\s* -> s*
\.  -> .
\*  -> *

This changes the intended regex semantics. In particular, \* is intended to match a literal asterisk, but after the backslash is removed it becomes *, which is a regex quantifier.

This was found from a dbt model macro containing this pattern:

".*(PAYRO?LL|SALARY|FED SAL|reg(\\s*|-|\\.|\\*)salary|Payrol)"

When compiled and parsed as Snowflake SQL, the regex escapes should be retained.

Activity

  1. georgesittas commented on Oct 7, 2026

    @georgesittas
    Collaborator

    This is already fixed in main. Minimal reproduction on the latest checkout (542c64b4c):

    from sqlglot import parse_one
    
    sql = r"SELECT 'x' RLIKE '\.'"
    print(parse_one(sql, read="spark").sql("spark"))
    # SELECT 'x' RLIKE '.'

    Both engines return these results:

    SELECT
      'x' RLIKE '\.'  AS original,    -- TRUE
      'x' RLIKE '.'   AS generated,   -- TRUE
      'x' RLIKE '\\.' AS escaped,     -- FALSE
      '.' RLIKE '\\.' AS literal_dot; -- TRUE
  2. ptmertka commented on Oct 14, 2026

    @ptmertka
    Author

    @georgesittas I was looking back at this issue yesterday, I realize that my issue wasn't clear. It's not with escapes themselves in RLike functions, my RLike statement, as seen above, utilizes reg(\s*|-|\.|\*), and it is here that SQLglot is stripping the escapes out of the reg() function. This behavior is still happening in SQLGlot 30.22.0

  3. georgesittas commented on Oct 14, 2026

    @georgesittas
    Collaborator

    Please open a new issue with exact steps to reproduce the bug. Show exactly what you're doing (including driving python code) and how the input vs output produces different results in the engine. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions