Re: jsonpath: Missing regex_like && starts with Errors?

From: Chapman Flack <jcflack(at)acm(dot)org>
To: "David E(dot) Wheeler" <david(at)justatheory(dot)com>, PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: jsonpath: Missing regex_like && starts with Errors?
Date: 2024-06-15 02:29:34
Message-ID: 666CFC8E.1000108@acm.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 06/14/24 12:21, David E. Wheeler wrote:
> I noticed that neither `regex_like` nor `starts with`, the jsonpath operators, raise an error when the operand is not a string (or array of strings):
>
> david=# select jsonb_path_query('true', '$ like_regex "^hi"');
> jsonb_path_query
> ------------------
> null
> (1 row)
>
> david=# select jsonb_path_query('{"x": "hi"}', '$ starts with "^hi"');
> jsonb_path_query
> ------------------
> null
> (1 row)

To begin with, both of those path queries should have been rejected at
the parsing stage, just like the one David Johnson pointed out:

On 06/13/24 22:14, David G. Johnston wrote:
> On Thursday, June 13, 2024, Chapman Flack <jcflack(at)acm(dot)org> wrote:
>> On 06/13/24 21:46, David G. Johnston wrote:
>>>>> david=# select jsonb_path_query('1', '$ >= 1');
>>>>
>>>> Good point. I can't either. No way I can see to parse that as
>>>> a <JSON path wff>.
>>>
>>> Whether we note it as non-standard or not is an open question then, but
>> it
>>> does work and opens up a documentation question.

All of these are <JSON path predicate> appearing where a <JSON path wff>
is needed, and that's not allowed in the standard. Strictly speaking, the
only place <JSON path predicate> can appear is within <JSON filter expression>.

So I should go look at our code to see what grammar we've implemented,
exactly. It is beginning to seem as if we have simply added
<JSON path predicate> as another choice for an expression, not restricted
to only appearing in a filter. If so, and we add documentation about how
we diverge from the standard, that's probably the way to say it.

On 06/13/24 22:14, David G. Johnston wrote:
> I don’t get why the outcome of a boolean producing operation isn’t just
> generally allowed to be produced

I understand; after all, what is a 'predicate' but another 'boolean
producing operation'? But the committee (at least in this edition) has
stuck us with this clear division in the grammar: there is no
<JSON path wff>, boolean as it may be, that can appear as a
<JSON path predicate>, and there is no <JSON path predicate> that
can appear outside of a filter and be treated as a boolean-valued
expression.

As for the error behavior of a <JSON path predicate> (which strictly,
again, can only appear inside a <JSON filter expression>), the standard
says what seems to be the same thing, in a couple different ways.

In 4.48.5 Overview of SQL/JSON path language, this is said: "The SQL/JSON
path language traps any errors that occur during the evaluation of a
<JSON filter expression>. Depending on the precise <JSON path predicate> ...
the result may be Unknown, True, or False, ...".

Later in 9.46's General Rules where the specific semantics of the
various predicates are laid out, each predicate has rules spelling out
which of Unknown, True, or False results when an error condition is
encountered (usually Unknown, except where something already seen allows
returning True or False). Finally, the <JSON filter expression> itself
collapses the three-valued logic to two; it includes the items for which
the predicate returns True, and excludes them for False or Unknown.

So that's where the errors went.

The question of what should happen to the errors when a
<JSON path predicate> appears outside of a <JSON filter expression>
of course isn't answered in the standard, because that's not supposed
to be possible. So if we're allowing predicates to appear on their own
as expressions, it's also up to us to say what should happen with errors
when they do.

Regards,
-Chap

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Chapman Flack 2024-06-15 03:21:52 Re: jsonpath: Missing regex_like && starts with Errors?
Previous Message Melanie Plageman 2024-06-14 23:56:42 Re: BitmapHeapScan streaming read user and prelim refactoring