summaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
authorTom Lane2021-10-14 16:43:43 +0000
committerTom Lane2021-10-14 16:43:55 +0000
commit4d5f651f1d651c6fa79f9188e7b9a04654c7125a (patch)
tree9864402172ecd0b01e36fd3945bc73e6e3042c41 /src/test
parent811051c2e7af1b030467760baf7ee0f4a22bc992 (diff)
Fix planner error with pulling up subquery expressions into function RTEs.
If a function-in-FROM laterally references the output of some sub-SELECT earlier in the FROM clause, and we are able to flatten that sub-SELECT into the outer query, the expression(s) copied into the function RTE missed being processed by eval_const_expressions. This'd lead to trouble and probable crashes at execution if such expressions contained named-argument function call syntax or functions with defaulted arguments. The bug is masked if the query contains any explicit JOIN syntax, which may help explain why we'd not noticed. Per bug #17227 from Bernd Dorn. This is an oversight in commit 7266d0997, so back-patch to v13 where that came in. Discussion: https://postgr.es/m/17227-5a28ed1512189fa4@postgresql.org
Diffstat (limited to 'src/test')
-rw-r--r--src/test/regress/expected/rangefuncs.out29
-rw-r--r--src/test/regress/sql/rangefuncs.sql15
2 files changed, 44 insertions, 0 deletions
diff --git a/src/test/regress/expected/rangefuncs.out b/src/test/regress/expected/rangefuncs.out
index cafca1f9ae..2334a1321e 100644
--- a/src/test/regress/expected/rangefuncs.out
+++ b/src/test/regress/expected/rangefuncs.out
@@ -2416,3 +2416,32 @@ select *, row_to_json(u) from unnest(array[]::rngfunc2[]) u;
(0 rows)
drop type rngfunc2;
+-- check handling of functions pulled up into function RTEs (bug #17227)
+explain (verbose, costs off)
+select * from
+ (select jsonb_path_query_array(module->'lectures', '$[*]') as lecture
+ from unnest(array['{"lectures": [{"id": "1"}]}'::jsonb])
+ as unnested_modules(module)) as ss,
+ jsonb_to_recordset(ss.lecture) as j (id text);
+ QUERY PLAN
+--------------------------------------------------------------------------------------------------------------------------------------------------------
+ Nested Loop
+ Output: jsonb_path_query_array((unnested_modules.module -> 'lectures'::text), '$[*]'::jsonpath, '{}'::jsonb, false), j.id
+ -> Function Scan on pg_catalog.unnest unnested_modules
+ Output: unnested_modules.module
+ Function Call: unnest('{"{\"lectures\": [{\"id\": \"1\"}]}"}'::jsonb[])
+ -> Function Scan on pg_catalog.jsonb_to_recordset j
+ Output: j.id
+ Function Call: jsonb_to_recordset(jsonb_path_query_array((unnested_modules.module -> 'lectures'::text), '$[*]'::jsonpath, '{}'::jsonb, false))
+(8 rows)
+
+select * from
+ (select jsonb_path_query_array(module->'lectures', '$[*]') as lecture
+ from unnest(array['{"lectures": [{"id": "1"}]}'::jsonb])
+ as unnested_modules(module)) as ss,
+ jsonb_to_recordset(ss.lecture) as j (id text);
+ lecture | id
+---------------+----
+ [{"id": "1"}] | 1
+(1 row)
+
diff --git a/src/test/regress/sql/rangefuncs.sql b/src/test/regress/sql/rangefuncs.sql
index 3c436028da..7e5cde14c4 100644
--- a/src/test/regress/sql/rangefuncs.sql
+++ b/src/test/regress/sql/rangefuncs.sql
@@ -767,3 +767,18 @@ select *, row_to_json(u) from unnest(array[null::rngfunc2, (1,'foo')::rngfunc2,
select *, row_to_json(u) from unnest(array[]::rngfunc2[]) u;
drop type rngfunc2;
+
+-- check handling of functions pulled up into function RTEs (bug #17227)
+
+explain (verbose, costs off)
+select * from
+ (select jsonb_path_query_array(module->'lectures', '$[*]') as lecture
+ from unnest(array['{"lectures": [{"id": "1"}]}'::jsonb])
+ as unnested_modules(module)) as ss,
+ jsonb_to_recordset(ss.lecture) as j (id text);
+
+select * from
+ (select jsonb_path_query_array(module->'lectures', '$[*]') as lecture
+ from unnest(array['{"lectures": [{"id": "1"}]}'::jsonb])
+ as unnested_modules(module)) as ss,
+ jsonb_to_recordset(ss.lecture) as j (id text);