The jsonpath predicate `like_regex` does not accept variables for pattern (or flags) - Mailing list pgsql-bugs

From sulfinu@gmail.com
Subject The jsonpath predicate `like_regex` does not accept variables for pattern (or flags)
Date
Msg-id CAGH1kmz2QT+m2Lr8ts=YWDq+435JWZ2MPFE2WpS2yFh2HQrQ5g@mail.gmail.com
Whole thread Raw
Responses Re: The jsonpath predicate `like_regex` does not accept variables for pattern (or flags)
Re: The jsonpath predicate `like_regex` does not accept variables for pattern (or flags)
List pgsql-bugs
Hello,

this one is noticed on PostgreSQL 16.3.
One of the predicates that can be used in jsonpath expressions is like_regex, which unfortunately does not accept variables for pattern or flags:
SELECT jsonb_path_match('{"name":"abc"}'::jsonb, '$.name like_regex $p flag "q"', '{"p": "abc"}'::jsonb, true);
SELECT jsonb_path_match('{"name":"abc"}'::jsonb, '$.name like_regex "abc" flag $f', '{"p": "abc", "f": "q"}'::jsonb, true);

The starts with predicate on the other hand accepts variables for prefix (but not for the tested string):
SELECT jsonb_path_match('{"name":"abc"}'::jsonb, '$.name starts with $p', '{"p": "abc"}'::jsonb, true);

I would like to be able to use a variable inside the like_regex predicate, at least for the pattern. I need that since I generate dynamically the involved predicates and this one is a containment test, actually.

If someone worries about self-shooting in the foot, I have two answers to that:
  1. it's my (calculated) risk;
  2. I can already shoot myself in the foot with the plain SQL regular expression test - see
    `SELECT regexp_like('abc', $1, $2);` with parameters ["abc", "q"]
    or

    `SELECT 'abc' ~ $1` with parameters ["abc"]

Thank you.

pgsql-bugs by date:

Previous
From: p2wap3
Date:
Subject: Re: BUG #18570: Drop event trigger for DDL finishes successfully but trigger still executed on DDL
Next
From: "David G. Johnston"
Date:
Subject: Re: The jsonpath predicate `like_regex` does not accept variables for pattern (or flags)