Repository navigation
ADP-1966 | Escape regex metacharacters in Postgres LIKE filter values - #330
Closed
sanjulksharma wants to merge 1 commit into
Closed
sanjulksharma wants to merge 1 commit into
sanjulksharma wants to merge 1 commit into
Conversation
sanjulksharma
requested review from
avinashkolluru,
kotharironak,
puneet-traceable,
skjindal93,
suddendust and
suresh-prakash
as code owners
October 21, 2026 05:26
Postgres's `~*` operator (used to implement the LIKE/"contains" filter)
is a case-insensitive, unanchored regex match using POSIX ARE, whereas
MongoDB's $regex uses PCRE2. PCRE2 tolerates malformed patterns (e.g. an
unmatched quantifier brace like "{{") by treating them literally, while
Postgres's ARE engine hard-errors with "invalid regular expression:
quantifier operand invalid" on the same input - breaking LIKE/"contains"
filters on Postgres for values containing regex metacharacters (e.g. API
Inventory domain search values like "{{" or "${${8gs").
Add a validity gate (PostgresUtils#sanitizeRegexValue) that passes a LIKE
value through unchanged if it is already a valid regex (preserving
existing regex-filter semantics, e.g. ".*amp.*", "^Sun"), and otherwise
escapes POSIX ARE metacharacters so the value is matched literally
instead of erroring out. Apply this in both LIKE code paths: the legacy
Filter/Op parser and the v1 expression parser
(PostgresLikeRelationalFilterParser), the latter being the actual path
hit by the reported production stacktrace.
Also drop the now-redundant ".*<value>.*" wrapping previously applied to
LIKE values before the ~* search, since `~*` is already an unanchored
substring regex match.
Note: for a value that is not a valid regex in either engine but IS
syntactically valid Perl/PCRE2 (e.g. "${${8gs", which PCRE2 compiles but
matches 0 documents because "$" is treated as an end-of-line anchor),
this introduces a narrow Mongo/Postgres result divergence (Mongo: 0
matches, Postgres: 1 literal match) - a deliberate, documented trade-off
versus leaving Postgres's LIKE operator broken for such inputs.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
AI-Session-Id: f9a3360e-ecb3-4388-beeb-682ed3b1d400
AI-Tool: claude-code
AI-Model: unknown
sanjulksharma
force-pushed
the
ADP-1966/escape-regex-metacharacters-in-postgres-like
branch
from
October 21, 2026 06:03
3abba57 to
1f38464
Compare
Collaborator
Author
|
Reopening from a branch pushed directly to hypertrace/document-store (now that I have collaborator access), since CI's pull_request_target workflow can't check out fork PR code. |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #330 +/- ##
=============================================
- Coverage 81.23% 58.97% -22.26%
- Complexity 1698 1703 +5
=============================================
Files 243 243
Lines 7780 7793 +13
Branches 771 773 +2
=============================================
- Hits 6320 4596 -1724
- Misses 962 2712 +1750
+ Partials 498 485 -13
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
Fixes ASP-3514: a
LIKEfilter value containing regex metacharacters (e.g.{{) works on MongoDB but throwsinvalid regular expression: quantifier operand invalidon Postgres, since Postgres's~*(POSIX ARE) hard-errors on malformed patterns that MongoDB's PCRE2 tolerates. Adds a validity gate that escapes such values to match literally instead of erroring, applied to both the legacy and v1 LIKE parsers, and drops the now-redundant.*wrapping since~*is already unanchored.🤖 Generated with Claude Code