Array parsing incorrectly accepts ragged multidimensional arrays

Started by Nikhil Beneschalmost 3 years ago3 messagesbugs
Jump to latest
#1Nikhil Benesch
nikhil.benesch@gmail.com

While working on Postgres-compatible array support in Materialize, my
colleague Sean Loiselle (CC'd) discovered what a class of seemingly
invalid array values that are accepted by Postgres's array input
routine:

benesch=# select '{{{9}},{8},{7}}'::int[];
int4
------
{}
(1 row)

benesch=# select '{{9},{{8}},{7}}'::int[];
int4
------
{}
(1 row)

benesch=# select '{{9},{8},{{7}}}'::int[];
int4
------------------------
{{{9}},{{NULL}},{{7}}}
(1 row)

As far as we can tell, Postgres *should* reject all these array
values, as the elements of each dimension do not have uniform length.

Postgres does correctly reject the analogous test cases with one less
layer of nesting:

benesch=# select '{{1}, 2, 3}'::int[];
ERROR: malformed array literal: "{{1}, 2, 3}"
LINE 1: select '{{1}, 2, 3}'::int[];
^
DETAIL: Unexpected array element.
benesch=# select '{1, {2}, 3}'::int[];
ERROR: malformed array literal: "{1, {2}, 3}"
LINE 1: select '{1, {2}, 3}'::int[];
^
DETAIL: Unexpected "{" character.
benesch=# select '{1, 2, {3}}'::int[];
ERROR: malformed array literal: "{1, 2, {3}}"
LINE 1: select '{1, 2, {3}}'::int[];
^
DETAIL: Unexpected "{" character.

I whipped up a patch that causes Postgres to correctly reject the
three examples above [0]https://gist.github.com/benesch/2e77712f81625deeb0e2246098fd8089, but I am not convinced of its correctness.

Do folks agree that this is a bug? Is it worth me or Sean formally
submitting the proposed patch, or would someone with more experience
like to do a more thorough refactor of the ArrayCount routine?

[0]: https://gist.github.com/benesch/2e77712f81625deeb0e2246098fd8089

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Nikhil Benesch (#1)
Re: Array parsing incorrectly accepts ragged multidimensional arrays

Nikhil Benesch <nikhil.benesch@gmail.com> writes:

While working on Postgres-compatible array support in Materialize, my
colleague Sean Loiselle (CC'd) discovered what a class of seemingly
invalid array values that are accepted by Postgres's array input
routine:

Yeah, see existing thread (and patch) at

/messages/by-id/2794005.1683042087@sss.pgh.pa.us

Please review that if you'd like.

regards, tom lane

#3Nikhil Benesch
nikhil.benesch@gmail.com
In reply to: Tom Lane (#2)
Re: Array parsing incorrectly accepts ragged multidimensional arrays

Ah, thanks for the pointer. Funny how these bugs can go unnoticed for
two decades and then get noticed twice in the same month.

Show quoted text

On Sun, Jun 4, 2023 at 6:53 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:

Nikhil Benesch <nikhil.benesch@gmail.com> writes:

While working on Postgres-compatible array support in Materialize, my
colleague Sean Loiselle (CC'd) discovered what a class of seemingly
invalid array values that are accepted by Postgres's array input
routine:

Yeah, see existing thread (and patch) at

/messages/by-id/2794005.1683042087@sss.pgh.pa.us

Please review that if you'd like.

regards, tom lane