Refuse an array shape pljs cannot represent, instead of changing the value - #43
Open
sfc-gh-okalaci wants to merge 1 commit into
Open
Refuse an array shape pljs cannot represent, instead of changing the value#43sfc-gh-okalaci wants to merge 1 commit into
sfc-gh-okalaci wants to merge 1 commit into
Conversation
…value
pljs represents a SQL array as a flat JavaScript array. Both directions handled an
array that does not fit that model by producing something else, quietly.
Reading: deconstruct_array() flattens, so a multidimensional array arrived in
JavaScript one-dimensional -- {{1,2},{3,4}} as [1,2,3,4] -- and writing it back
gave a different value than was read.
Writing: a nested JavaScript array aimed at a scalar element type went into the
array conversion anyway, because the dispatch condition asked only whether the
target was json/jsonb, which is false for every scalar. The element loop then
converted each inner array to the element type:
CREATE FUNCTION f() RETURNS int[] AS $$ return [[1,2],[3,4]]; $$ LANGUAGE pljs;
SELECT f();
{-1629977232,-1629977184}
Those are the inner arrays' ArrayType pointers reinterpreted as int4. Not an
error, and not distinguishable from data by looking at it.
Reading now raises ERRCODE_FEATURE_NOT_SUPPORTED for a multidimensional array, and
writing raises ERRCODE_DATATYPE_MISMATCH naming the target type when a JavaScript
array is aimed at something that cannot hold one. One-dimensional arrays are
unaffected, and a nested array is still valid for json and jsonb, which are the
targets that can represent it.
Adds sql/pg_array_shape.sql, covering both directions, both error paths, that
one-dimensional arrays still round-trip, and that nested arrays still work for
jsonb and jsonb[].
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 #42.
pljs represents a SQL array as a flat JavaScript array. Both directions handled an array
that does not fit that model by quietly producing something else.
Reading —
deconstruct_array()flattens, so a multidimensional array arrived inJavaScript one-dimensional.
{{1,2},{3,4}}became[1,2,3,4], and writing it back gave adifferent value than was read.
Writing — a nested JavaScript array aimed at a scalar element type went into the array
conversion anyway, because the dispatch condition asked only whether the target was
json/jsonb, which is false for every scalar. The element loop then converted each inner
array to the element type:
Those are the inner arrays'
ArrayTypepointers reinterpreted asint4. No error, andnothing about the output says it isn't data.
Reading now raises
ERRCODE_FEATURE_NOT_SUPPORTEDfor a multidimensional array; writingraises
ERRCODE_DATATYPE_MISMATCHnaming the target type. One-dimensional arrays areunaffected in both directions, and a nested array remains valid for
json/jsonb— thetargets that can actually represent one.
Test plan
sql/pg_array_shape.sqlcovers both directions, both error paths, a scalar target(
int,text), that one-dimensional arrays still round-trip, and that nested arraysstill work for
jsonbandjsonb[].It discriminates: reverted, the read silently returns
[1,2,3,4]and the write returns{-1629977232,-1629977184}. Full suite green on PostgreSQL 17 (55 tests); builds clean on16, 18 and 19beta3.