fix: LIKE ignores the backslash escape character - #481
Open
spokodev wants to merge 1 commit into
Open
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
LIKE ignored the escape character (backslash by default), so an escaped wildcard did not match its literal:
The same happened on an indexed column: the literal prefix was taken from the raw pattern (
a\instead ofa%b), so the index narrowing dropped matching rows.Ref: Postgres LIKE
Cause
buildLikeMatcherregex-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 inbuild-filterextracted its prefix straight from the raw pattern.Fix
buildLikeMatchernow 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 inoperators.queries.spec.ts, including an indexed case.