Avoid double transformation of json_array()'s subquery.
authorTom Lane <[email protected]>
Sat, 5 Apr 2025 16:13:35 +0000 (12:13 -0400)
committerTom Lane <[email protected]>
Sat, 5 Apr 2025 16:13:35 +0000 (12:13 -0400)
commit717e8a1e5256fd01ed5b4cd60f2aac97dceb00bd
tree767eb8271ae57ca1dd48140b4b82624036b84e91
parent5e6e97fbf4031de0e7c84d87a002c0eef2ca7abe
Avoid double transformation of json_array()'s subquery.

transformJsonArrayQueryConstructor() applied transformStmt() to
the same subquery tree twice.  While this causes no issue in many
cases, there are some where it causes a coredump, thanks to the
parser's habit of scribbling on its input.

Fix by making a copy before the first transformation (compare
0f43083d1).  This is quite brute-force, but then so is the
whole business of transforming the input twice.  Per discussion
in the bug thread, this implementation of json_array() parsing
should be replaced completely.  But that will take some work
and will surely not be back-patchable, so for the moment let's
take the easy way out.

Oversight in 7081ac46a.  Back-patch to v16 where that came in.

Bug: #18877
Reported-by: Yu Liang <[email protected]>
Author: Tom Lane <[email protected]>
Discussion: https://postgr.es/m/18877-c3c3ad75845833bb@postgresql.org
Backpatch-through: 16
src/backend/parser/parse_expr.c
src/test/regress/expected/sqljson.out
src/test/regress/sql/sqljson.sql