Re: BUG #19957: jsonpath `last` returns the wrong index after a filter containing `exists($x[0])`

From: Zsolt Parragi <zsolt(dot)parragi(at)percona(dot)com>
To: Ishjaap Singh <ishjaap(dot)singh07(at)gmail(dot)com>
Cc: pgsql-bugs(at)lists(dot)postgresql(dot)org, e1638616(at)u(dot)nus(dot)edu
Subject: Re: BUG #19957: jsonpath `last` returns the wrong index after a filter containing `exists($x[0])`
Date: 2026-11-30 23:17:42
Message-ID: CAN4CZFMUVU-YsdptKMXNeSmtCyReEocnaDuPfwPyzXRuL=CQZw@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Hello

The patch seems to fix the reported bug, but an LLM-review found a few
more related cases that could be possibly fixed together:

select jsonb_path_query_array('[10,20,30]', '$[$x[0 to last] ? (@ ==
last)]', '{"x":[1,2]}');
-- [20]
select jsonb_path_query_array('[10,20,30]', '$[$x[0] ? (@ == last)]',
'{"x":[2,1]}');
-- ERROR: jsonpath array subscript is not a single numeric value

Maybe we could set the size only while the subscript expressions are
evaluated, in getArrayIndex(), instead of for the whole loop?

-getArrayIndex(JsonPathExecContext *cxt, JsonPathItem *jsp, JsonbValue *jb,
- int32 *index)
+getArrayIndex(JsonPathExecContext *cxt, JsonPathItem *jsp, JsonbValue *jb,
+ int arraySize, int32 *index)
...
+ cxt->innermostArraySize = arraySize; /* for LAST evaluation */
res = executeItem(cxt, jsp, jb, &found);
+ cxt->innermostArraySize = innermostArraySize;

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message shihao zhong 2026-11-30 23:52:58 Re: BUG #19824: OMIT QUOTES breaks non-string results: array / composite / array-domain coercions in JSON_QUERY / JS
Previous Message Zsolt Parragi 2026-11-30 22:27:45 Re: BUG #19928: pg_upgrade fails on v18 schema with NOT NULL ... NO INHERIT and inheritance