Skip to content

fix: LIKE ignores the backslash escape character - #481

Open
spokodev wants to merge 1 commit into
oguimbal:masterfrom
spokodev:fix/like-escape-char
Open

fix: LIKE ignores the backslash escape character#481
spokodev wants to merge 1 commit into
oguimbal:masterfrom
spokodev:fix/like-escape-char

Conversation

@spokodev

@spokodev spokodev commented Aug 3, 2026

Copy link
Copy Markdown

Problem

LIKE ignored the escape character (backslash by default), so an escaped wildcard did not match its literal:

select 'a%b' like 'a\%b';   -- pg-mem: false, postgres: true
select 'a_b' like 'a\_b';   -- pg-mem: false, postgres: true

The same happened on an indexed column: the literal prefix was taken from the raw pattern (a\ instead of a%b), so the index narrowing dropped matching rows.

Ref: Postgres LIKE

Cause

buildLikeMatcher regex-escaped the whole pattern and then replaced every %/_, with no notion of the escape character, so \% stayed a wildcard (and left a stray backslash in the regex). The index fast-path in build-filter extracted its prefix straight from the raw pattern.

Fix

buildLikeMatcher now parses the pattern in a single pass: a backslash escapes the next character (so \%, \_ and \\ match a literal %, _ and \), while unescaped %/_ remain wildcards. The index prefix optimisation bails out for patterns containing an escape character and falls back to a sequential scan with the corrected matcher. Tests added in operators.queries.spec.ts, including an indexed case.

LIKE ignored the escape character, so an escaped wildcard did not match
its literal:

  'a%b' LIKE 'a\%b';  -- was false, postgres true
  'a_b' LIKE 'a\_b';  -- was false, postgres true

buildLikeMatcher now parses the pattern in one pass, treating a backslash
as an escape for the next character. The index prefix optimisation is
skipped for patterns containing an escape and falls back to a seq scan.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant