pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Alexander Korotkov <aekorotkov@gmail.com>
To: David G. Johnston <david.g.johnston@gmail.com>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: PostgreSQL Bug List <pgsql-bugs@lists.postgresql.org>
Subject: Re: Bug in jsonb_path_exists (maybe _match) one-element scalar/variable jsonpath handling
Date: Tue, 6 Dec 2022 02:57:49 +0300
Message-ID: <CAPpHfdtweZwYrf0uTXRWiFRW6_L0eAWMBQFH9fM-SCbwqikJUA@mail.gmail.com> (raw)
In-Reply-To: <CAPpHfdvuHTjZLOWy1BEr0b1qw=mNXqw0OEndnLsdaw85ftDc-A@mail.gmail.com>
References: <CAKFQuwbeytffJkVnEqDyLZ=rQsznoTh1OgDoOF3VmOMkxcTMjA@mail.gmail.com>
	<CAPpHfdvuHTjZLOWy1BEr0b1qw=mNXqw0OEndnLsdaw85ftDc-A@mail.gmail.com>

On Fri, Dec 2, 2022 at 3:18 PM Alexander Korotkov <aekorotkov@gmail.com> wrote:
> Draft patch fixing the issue is attached.  Let me know what you think
> about this.

Revised patch is attached, wrong pfree() is fixed.  I was intended to
backpatch it.  But the behavior change makes me uneasy.

select * from jsonb_path_query('{"a": 10}', '$ ? (@.a < $value)');

Currently, this query generates an error because of missing "value"
variable.  The patch suppress this error.  I'm not sure this error
should be suppressed.  Especially, I'm sure this should be
backpatched.

Should we fix only existence checking behaviour and let other cases
throw an error?  Thoughts?

------
Regards,
Alexander Korotkov

Attachments:

0001-Teach-jsonpath-to-handle-missing-variables-v2.patch (application/octet-stream, 4.9K) download From 042ae7d0cfdb8683e1a07f691a21e47d17ea2b39 Mon Sep 17 00:00:00 2001 From: Alexander Korotkov <akorotkov@postgresql.org> Date: Mon, 5 Dec 2022 17:02:29 +0300 Subject: [PATCH] Teach jsonpath to handle missing variables The current jsonpath code assumes that the referenced variable always exists. It could only throw an error at the value valuation time. At the same time existence checking assumes variable is present without valuation, and error suppression doesn't work for missing variables. This commit makes jsonpath code to handle variables in the similar way to key accessors. The existence is now checked runtime, and error suppression is also applied. Backpatch to 12 where jsonpath was introduced. Reported-by: David G. Johnston Discussion: https://postgr.es/m/CAKFQuwbeytffJkVnEqDyLZ%3DrQsznoTh1OgDoOF3VmOMkxcTMjA%40mail.gmail.com Author: Alexander Korotkov Backpatch-through: 12 --- src/backend/utils/adt/jsonpath_exec.c | 47 +++++++++++++++----- src/test/regress/expected/jsonb_jsonpath.out | 5 ++- 2 files changed, 40 insertions(+), 12 deletions(-) diff --git a/src/backend/utils/adt/jsonpath_exec.c b/src/backend/utils/adt/jsonpath_exec.c index 8d83b2edb35..e15ced19c4e 100644 --- a/src/backend/utils/adt/jsonpath_exec.c +++ b/src/backend/utils/adt/jsonpath_exec.c @@ -224,8 +224,9 @@ static JsonPathExecResult appendBoolResult(JsonPathExecContext *cxt, JsonPathItem *jsp, JsonValueList *found, JsonPathBool res); static void getJsonPathItem(JsonPathExecContext *cxt, JsonPathItem *item, JsonbValue *value); -static void getJsonPathVariable(JsonPathExecContext *cxt, - JsonPathItem *variable, Jsonb *vars, JsonbValue *value); +static JsonPathExecResult getJsonPathVariable(JsonPathExecContext *cxt, + JsonPathItem *variable, + JsonbValue *value); static int JsonbArraySize(JsonbValue *jb); static JsonPathBool executeComparison(JsonPathItem *cmp, JsonbValue *lv, JsonbValue *rv, void *p); @@ -952,7 +953,6 @@ executeItemOptUnwrapTarget(JsonPathExecContext *cxt, JsonPathItem *jsp, case jpiBool: case jpiNumeric: case jpiString: - case jpiVariable: { JsonbValue vbuf; JsonbValue *v; @@ -975,6 +975,24 @@ executeItemOptUnwrapTarget(JsonPathExecContext *cxt, JsonPathItem *jsp, } break; + case jpiVariable: + { + JsonbValue vbuf; + JsonbValue *v; + bool hasNext = jspGetNext(jsp, &elem); + + v = hasNext ? &vbuf : palloc(sizeof(*v)); + + baseObject = cxt->baseObject; + res = getJsonPathVariable(cxt, jsp, v); + + if (res == jperOk) + res = executeNextItem(cxt, jsp, &elem, + v, found, hasNext); + cxt->baseObject = baseObject; + } + break; + case jpiType: { JsonbValue *jbv = palloc(sizeof(*jbv)); @@ -2092,9 +2110,6 @@ getJsonPathItem(JsonPathExecContext *cxt, JsonPathItem *item, value->val.string.val = jspGetString(item, &value->val.string.len); break; - case jpiVariable: - getJsonPathVariable(cxt, item, cxt->vars, value); - return; default: elog(ERROR, "unexpected jsonpath item type"); } @@ -2103,19 +2118,20 @@ getJsonPathItem(JsonPathExecContext *cxt, JsonPathItem *item, /* * Get the value of variable passed to jsonpath executor */ -static void +static JsonPathExecResult getJsonPathVariable(JsonPathExecContext *cxt, JsonPathItem *variable, - Jsonb *vars, JsonbValue *value) + JsonbValue *value) { char *varName; int varNameLength; JsonbValue tmp; JsonbValue *v; + Jsonb *vars = cxt->vars; + JsonPathExecResult res; if (!vars) { - value->type = jbvNull; - return; + return jperNotFound; } Assert(variable->type == jpiVariable); @@ -2130,17 +2146,26 @@ getJsonPathVariable(JsonPathExecContext *cxt, JsonPathItem *variable, { *value = *v; pfree(v); + res = jperOk; } - else + else if (!jspIgnoreStructuralErrors(cxt)) { + if (!jspThrowErrors(cxt)) + res = jperError; ereport(ERROR, (errcode(ERRCODE_UNDEFINED_OBJECT), errmsg("could not find jsonpath variable \"%s\"", pnstrdup(varName, varNameLength)))); } + else + { + res = jperNotFound; + } JsonbInitBinary(&tmp, vars); setBaseObject(cxt, &tmp, 1); + + return res; } /**************** Support functions for JsonPath execution *****************/ diff --git a/src/test/regress/expected/jsonb_jsonpath.out b/src/test/regress/expected/jsonb_jsonpath.out index 508ddd797ed..19c6bdc72c7 100644 --- a/src/test/regress/expected/jsonb_jsonpath.out +++ b/src/test/regress/expected/jsonb_jsonpath.out @@ -486,7 +486,10 @@ select * from jsonb_path_query('{"a": 10}', '$'); (1 row) select * from jsonb_path_query('{"a": 10}', '$ ? (@.a < $value)'); -ERROR: could not find jsonpath variable "value" + jsonb_path_query +------------------ +(0 rows) + select * from jsonb_path_query('{"a": 10}', '$ ? (@.a < $value)', '1'); ERROR: "vars" argument is not an object DETAIL: Jsonpath parameters should be encoded as key-value pairs of "vars" object. -- 2.24.3 (Apple Git-128)

view thread (13+ messages)  latest in thread

Message-ID: <CAPpHfdtweZwYrf0uTXRWiFRW6_L0eAWMBQFH9fM-SCbwqikJUA@mail.gmail.com>
Permalink:  ../CAPpHfdtweZwYrf0uTXRWiFRW6_L0eAWMBQFH9fM-SCbwqikJUA@mail.gmail.com/
Also on:    postgresql.org/message-id/CAPpHfdtweZwYrf0uTXRWiFRW6_L0eAWMBQFH9fM-SCbwqikJUA@mail.gmail.com

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: aekorotkov@gmail.com, david.g.johnston@gmail.com, tgl@sss.pgh.pa.us
  Subject: Re: Bug in jsonb_path_exists (maybe _match) one-element scalar/variable jsonpath handling
  In-Reply-To: <CAPpHfdtweZwYrf0uTXRWiFRW6_L0eAWMBQFH9fM-SCbwqikJUA@mail.gmail.com>
  References: <CAKFQuwbeytffJkVnEqDyLZ=rQsznoTh1OgDoOF3VmOMkxcTMjA@mail.gmail.com> <CAPpHfdvuHTjZLOWy1BEr0b1qw=mNXqw0OEndnLsdaw85ftDc-A@mail.gmail.com> <CAPpHfdtweZwYrf0uTXRWiFRW6_L0eAWMBQFH9fM-SCbwqikJUA@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox