Dispatch array conversion on the SQL type category, fixing RETURNS jsonb[] - #42
Open
sfc-gh-okalaci wants to merge 1 commit into
Open
Dispatch array conversion on the SQL type category, fixing RETURNS jsonb[]#42sfc-gh-okalaci wants to merge 1 commit into
sfc-gh-okalaci wants to merge 1 commit into
Conversation
…onb[]
The choice between building a SQL array and building a JSON array was made by
comparing the type OID against JSONOID and JSONBOID. By that point
pljs_type_fill() has already rewritten the OID to the *element* type, and the
element type of jsonb[] is jsonb -- so an array target was indistinguishable from
a scalar json target.
Those calls fell through to the scalar json branch, which stringified the whole
JavaScript array to "[object Object],[object Object]" and handed it to the json
input function. The array construction never ran. What surfaces is a cache lookup
failure for a type OID read out of uninitialised memory:
CREATE FUNCTION f() RETURNS jsonb[] AS $$ return [{a:1},{b:2}]; $$ LANGUAGE pljs;
SELECT f();
ERROR: cache lookup failed for type 1701470799
The OID differs from run to run, which is the tell. So RETURNS jsonb[] and
RETURNS json[] have never worked, for any element shape -- objects, scalars,
strings or nested arrays.
The condition now asks the SQL type's category, which still describes an array
after the element rewrite. A bare json or jsonb target continues to turn a
JavaScript array into a JSON array, which is what the original condition was for.
Adds sql/pg_jsonb_array_return.sql, covering objects, scalars, nested arrays, the
empty array, json[] as well as jsonb[], and -- as the regression guard -- that a
bare jsonb target still produces a JSON array.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stacked on #41.
The choice between building a SQL array and building a JSON array was made by comparing
the type OID against
JSONOID/JSONBOID. By that pointpljs_type_fill()has alreadyrewritten the OID to the element type — and the element type of
jsonb[]isjsonb, so an array target was indistinguishable from a scalar json target.Those calls fell through to the scalar json branch, which stringified the entire
JavaScript array to
"[object Object],[object Object]"and handed that to the json inputfunction. The array construction never ran:
The OID differs from run to run —
1701470799,2116838773,2139062143— because itis read from uninitialised memory.
RETURNS jsonb[]andRETURNS json[]have thereforenever worked, for any element shape: objects, scalars, strings or nested arrays.
The condition now asks the SQL type's category, which still says "array" after the
element rewrite. A bare
json/jsonbtarget keeps turning a JavaScript array into aJSON array, which is what the original condition was there for.
Test plan
sql/pg_jsonb_array_return.sqlcovers objects, scalars, nested arrays, the empty array,json[]as well asjsonb[], a plainint[]for contrast, and — as the regression guard— that a bare
jsonbtarget still yields a JSON array ([1, 2, 3],jsonb_typeof=array).It discriminates: reverted, six statements fail with
cache lookup failed for type ….Full suite green on PostgreSQL 17 (54 tests); builds clean on 16, 18 and 19beta3.