Improve readability and error detection of array_in().

Rewrite array_in() and its subroutines so that we make only one
pass over the input text, rather than two.  This requires
potentially re-pallocing the working arrays values[] and nulls[]
larger than our initial guess, but that cost will hopefully be made
up by avoiding duplicate parsing.  In any case this coding seems
much clearer and more straightforward than what we had before.

This also fixes array_in() to reject non-rectangular input (that is,
different brace depths in different parts of the input) more reliably
than before, and to give a better error message when it does so.
This is analogous to the plpython and plperl fixes in 0553528e7 and
f47004add.  Like those PLs, we now accept input such as '{{},{}}'
as a valid representation of an empty array, which we did not before.

Additionally, reject explicit array subscripts that are outside the
integer range (previously you just got whatever atoi() converted
them to), and make some other minor improvements in error reporting.

Although this is arguably a bug fix, it's also a behavioral change
that might trip somebody up, so no back-patch.

Tom Lane, Heikki Linnakangas, and Jian He.  Thanks to Alexander Lakhin
for the initial report and for review/testing.

Discussion: https://postgr.es/m/2794005.1683042087@sss.pgh.pa.us
This commit is contained in:
Tom Lane 2023-11-13 13:01:47 -05:00
parent 57d6a198c9
commit 83472de606
7 changed files with 799 additions and 630 deletions

View File

@ -171,7 +171,8 @@ INSERT INTO sal_emp
VALUES ('Bill',
'{10000, 10000, 10000, 10000}',
'{{"meeting", "lunch"}, {"meeting"}}');
ERROR: multidimensional arrays must have array expressions with matching dimensions
ERROR: malformed array literal: "{{"meeting", "lunch"}, {"meeting"}}"
DETAIL: Multidimensional arrays must have sub-arrays with matching dimensions.
</programlisting>
</para>

File diff suppressed because it is too large Load Diff

View File

@ -43,21 +43,6 @@ ArrayGetOffset(int n, const int *dim, const int *lb, const int *indx)
return offset;
}
/*
* Same, but subscripts are assumed 0-based, and use a scale array
* instead of raw dimension data (see mda_get_prod to create scale array)
*/
int
ArrayGetOffset0(int n, const int *tup, const int *scale)
{
int i,
lin = 0;
for (i = 0; i < n; i++)
lin += tup[i] * scale[i];
return lin;
}
/*
* Convert array dimensions into number of elements
*

View File

@ -455,7 +455,6 @@ extern void array_free_iterator(ArrayIterator iterator);
*/
extern int ArrayGetOffset(int n, const int *dim, const int *lb, const int *indx);
extern int ArrayGetOffset0(int n, const int *tup, const int *scale);
extern int ArrayGetNItems(int ndim, const int *dims);
extern int ArrayGetNItemsSafe(int ndim, const int *dims,
struct Node *escontext);

View File

@ -1492,21 +1492,36 @@ select '{{1,{2}},{2,3}}'::text[];
ERROR: malformed array literal: "{{1,{2}},{2,3}}"
LINE 1: select '{{1,{2}},{2,3}}'::text[];
^
DETAIL: Unexpected "{" character.
select '{{},{}}'::text[];
ERROR: malformed array literal: "{{},{}}"
LINE 1: select '{{},{}}'::text[];
^
DETAIL: Unexpected "}" character.
DETAIL: Multidimensional arrays must have sub-arrays with matching dimensions.
select E'{{1,2},\\{2,3}}'::text[];
ERROR: malformed array literal: "{{1,2},\{2,3}}"
LINE 1: select E'{{1,2},\\{2,3}}'::text[];
^
DETAIL: Unexpected "\" character.
DETAIL: Multidimensional arrays must have sub-arrays with matching dimensions.
select '{"a"b}'::text[];
ERROR: malformed array literal: "{"a"b}"
LINE 1: select '{"a"b}'::text[];
^
DETAIL: Incorrectly quoted array element.
select '{a"b"}'::text[];
ERROR: malformed array literal: "{a"b"}"
LINE 1: select '{a"b"}'::text[];
^
DETAIL: Incorrectly quoted array element.
select '{"a""b"}'::text[];
ERROR: malformed array literal: "{"a""b"}"
LINE 1: select '{"a""b"}'::text[];
^
DETAIL: Incorrectly quoted array element.
select '{{"1 2" x},{3}}'::text[];
ERROR: malformed array literal: "{{"1 2" x},{3}}"
LINE 1: select '{{"1 2" x},{3}}'::text[];
^
DETAIL: Incorrectly quoted array element.
select '{{"1 2"} x,{3}}'::text[];
ERROR: malformed array literal: "{{"1 2"} x,{3}}"
LINE 1: select '{{"1 2"} x,{3}}'::text[];
^
DETAIL: Unexpected array element.
select '{}}'::text[];
ERROR: malformed array literal: "{}}"
@ -1518,11 +1533,116 @@ ERROR: malformed array literal: "{ }}"
LINE 1: select '{ }}'::text[];
^
DETAIL: Junk after closing right brace.
select '}{'::text[];
ERROR: malformed array literal: "}{"
LINE 1: select '}{'::text[];
^
DETAIL: Array value must start with "{" or dimension information.
select '{foo{}}'::text[];
ERROR: malformed array literal: "{foo{}}"
LINE 1: select '{foo{}}'::text[];
^
DETAIL: Unexpected "{" character.
select '{"foo"{}}'::text[];
ERROR: malformed array literal: "{"foo"{}}"
LINE 1: select '{"foo"{}}'::text[];
^
DETAIL: Unexpected "{" character.
select '{foo,,bar}'::text[];
ERROR: malformed array literal: "{foo,,bar}"
LINE 1: select '{foo,,bar}'::text[];
^
DETAIL: Unexpected "," character.
select '{{1},{{2}}}'::text[];
ERROR: malformed array literal: "{{1},{{2}}}"
LINE 1: select '{{1},{{2}}}'::text[];
^
DETAIL: Multidimensional arrays must have sub-arrays with matching dimensions.
select '{{{1}},{2}}'::text[];
ERROR: malformed array literal: "{{{1}},{2}}"
LINE 1: select '{{{1}},{2}}'::text[];
^
DETAIL: Multidimensional arrays must have sub-arrays with matching dimensions.
select '{{},{{}}}'::text[];
ERROR: malformed array literal: "{{},{{}}}"
LINE 1: select '{{},{{}}}'::text[];
^
DETAIL: Multidimensional arrays must have sub-arrays with matching dimensions.
select '{{{}},{}}'::text[];
ERROR: malformed array literal: "{{{}},{}}"
LINE 1: select '{{{}},{}}'::text[];
^
DETAIL: Multidimensional arrays must have sub-arrays with matching dimensions.
select '{{1},{}}'::text[];
ERROR: malformed array literal: "{{1},{}}"
LINE 1: select '{{1},{}}'::text[];
^
DETAIL: Multidimensional arrays must have sub-arrays with matching dimensions.
select '{{},{1}}'::text[];
ERROR: malformed array literal: "{{},{1}}"
LINE 1: select '{{},{1}}'::text[];
^
DETAIL: Multidimensional arrays must have sub-arrays with matching dimensions.
select '[1:0]={}'::int[];
ERROR: upper bound cannot be less than lower bound
LINE 1: select '[1:0]={}'::int[];
^
select '[2147483646:2147483647]={1,2}'::int[];
ERROR: array upper bound is too large: 2147483647
LINE 1: select '[2147483646:2147483647]={1,2}'::int[];
^
select '[1:-1]={}'::int[];
ERROR: upper bound cannot be less than lower bound
LINE 1: select '[1:-1]={}'::int[];
^
select '[2]={1}'::int[];
ERROR: malformed array literal: "[2]={1}"
LINE 1: select '[2]={1}'::int[];
^
DETAIL: Specified array dimensions do not match array contents.
select '[1:]={1}'::int[];
ERROR: malformed array literal: "[1:]={1}"
LINE 1: select '[1:]={1}'::int[];
^
DETAIL: Missing array dimension value.
select '[:1]={1}'::int[];
ERROR: malformed array literal: "[:1]={1}"
LINE 1: select '[:1]={1}'::int[];
^
DETAIL: "[" must introduce explicitly-specified array dimensions.
select array[];
ERROR: cannot determine type of empty array
LINE 1: select array[];
^
HINT: Explicitly cast to the desired type, for example ARRAY[]::integer[].
select '{{1,},{1},}'::text[];
ERROR: malformed array literal: "{{1,},{1},}"
LINE 1: select '{{1,},{1},}'::text[];
^
DETAIL: Unexpected "}" character.
select '{{1,},{1}}'::text[];
ERROR: malformed array literal: "{{1,},{1}}"
LINE 1: select '{{1,},{1}}'::text[];
^
DETAIL: Unexpected "}" character.
select '{{1,}}'::text[];
ERROR: malformed array literal: "{{1,}}"
LINE 1: select '{{1,}}'::text[];
^
DETAIL: Unexpected "}" character.
select '{1,}'::text[];
ERROR: malformed array literal: "{1,}"
LINE 1: select '{1,}'::text[];
^
DETAIL: Unexpected "}" character.
select '[21474836488:21474836489]={1,2}'::int[];
ERROR: array bound is out of integer range
LINE 1: select '[21474836488:21474836489]={1,2}'::int[];
^
select '[-2147483649:-2147483648]={1,2}'::int[];
ERROR: array bound is out of integer range
LINE 1: select '[-2147483649:-2147483648]={1,2}'::int[];
^
-- none of the above should be accepted
-- all of the following should be accepted
select '{}'::text[];
@ -1531,12 +1651,30 @@ select '{}'::text[];
{}
(1 row)
select '{{},{}}'::text[];
text
------
{}
(1 row)
select '{{{1,2,3,4},{2,3,4,5}},{{3,4,5,6},{4,5,6,7}}}'::text[];
text
-----------------------------------------------
{{{1,2,3,4},{2,3,4,5}},{{3,4,5,6},{4,5,6,7}}}
(1 row)
select '{null,n\ull,"null"}'::text[];
text
----------------------
{NULL,"null","null"}
(1 row)
select '{ ab\c , "ab\"c" }'::text[];
text
---------------
{abc,"ab\"c"}
(1 row)
select '{0 second ,0 second}'::interval[];
interval
---------------
@ -1570,12 +1708,30 @@ select array[]::text[];
{}
(1 row)
select '[2]={1,7}'::int[];
int4
-------
{1,7}
(1 row)
select '[0:1]={1.1,2.2}'::float8[];
float8
-----------------
[0:1]={1.1,2.2}
(1 row)
select '[2147483646:2147483646]={1}'::int[];
int4
-----------------------------
[2147483646:2147483646]={1}
(1 row)
select '[-2147483648:-2147483647]={1,2}'::int[];
int4
---------------------------------
[-2147483648:-2147483647]={1,2}
(1 row)
-- all of the above should be accepted
-- tests for array aggregates
CREATE TEMP TABLE arraggtest ( f1 INT[], f2 TEXT[][], f3 FLOAT[]);

View File

@ -473,17 +473,45 @@ select 'foo' ilike all (array['F%', '%O']); -- t
-- none of the following should be accepted
select '{{1,{2}},{2,3}}'::text[];
select '{{},{}}'::text[];
select E'{{1,2},\\{2,3}}'::text[];
select '{"a"b}'::text[];
select '{a"b"}'::text[];
select '{"a""b"}'::text[];
select '{{"1 2" x},{3}}'::text[];
select '{{"1 2"} x,{3}}'::text[];
select '{}}'::text[];
select '{ }}'::text[];
select '}{'::text[];
select '{foo{}}'::text[];
select '{"foo"{}}'::text[];
select '{foo,,bar}'::text[];
select '{{1},{{2}}}'::text[];
select '{{{1}},{2}}'::text[];
select '{{},{{}}}'::text[];
select '{{{}},{}}'::text[];
select '{{1},{}}'::text[];
select '{{},{1}}'::text[];
select '[1:0]={}'::int[];
select '[2147483646:2147483647]={1,2}'::int[];
select '[1:-1]={}'::int[];
select '[2]={1}'::int[];
select '[1:]={1}'::int[];
select '[:1]={1}'::int[];
select array[];
select '{{1,},{1},}'::text[];
select '{{1,},{1}}'::text[];
select '{{1,}}'::text[];
select '{1,}'::text[];
select '[21474836488:21474836489]={1,2}'::int[];
select '[-2147483649:-2147483648]={1,2}'::int[];
-- none of the above should be accepted
-- all of the following should be accepted
select '{}'::text[];
select '{{},{}}'::text[];
select '{{{1,2,3,4},{2,3,4,5}},{{3,4,5,6},{4,5,6,7}}}'::text[];
select '{null,n\ull,"null"}'::text[];
select '{ ab\c , "ab\"c" }'::text[];
select '{0 second ,0 second}'::interval[];
select '{ { "," } , { 3 } }'::text[];
select ' { { " 0 second " , 0 second } }'::text[];
@ -492,7 +520,10 @@ select '{
@ 1 hour @ 42 minutes @ 20 seconds
}'::interval[];
select array[]::text[];
select '[2]={1,7}'::int[];
select '[0:1]={1.1,2.2}'::float8[];
select '[2147483646:2147483646]={1}'::int[];
select '[-2147483648:-2147483647]={1,2}'::int[];
-- all of the above should be accepted
-- tests for array aggregates

View File

@ -148,8 +148,8 @@ ArrayIOData
ArrayIterator
ArrayMapState
ArrayMetaState
ArrayParseState
ArraySubWorkspace
ArrayToken
ArrayType
AsyncQueueControl
AsyncQueueEntry