From: Bruce Momjian <bruce@momjian.us>
To: borz_off@cs.msu.su
Cc: pgsql-bugs@postgresql.org
Cc: Joe Conway <mail@joeconway.com>
Subject: Re: BUG #8399: inconsistent input of multidimensional arrays
Date: Fri, 31 Jan 2014 20:11:25 -0500
Message-ID: <20140201011125.GG19957@momjian.us> (raw)
In-Reply-To: <E1VEETa-0007KM-8O@wrigleys.postgresql.org>
References: <E1VEETa-0007KM-8O@wrigleys.postgresql.org>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>
On Tue, Aug 27, 2013 at 08:18:10AM +0000, borz_off@cs.msu.su wrote:
> PostgreSQL documentation states:
>
>
> "Multidimensional arrays must have matching extents for each dimension. A
> mismatch causes an error..."
>
>
> This is not completely true:
> postgres=# select cast('{{1,2}, {3}}' as integer[]);
> ERROR: multidimensional arrays must have array expressions with matching
> dimensions
> LINE 1: select cast('{{1,2}, {3}}' as integer[]);
> ^
> postgres=# select cast('{{1}, {2,3}}' as integer[]);
> int4
> ------------------
> {{1,NULL},{2,3}}
> (1 row)
>
>
> Trying to use an array constructor yields an expected error in both cases.
>
>
> Confirmed on 9.3rc1 and 9.0.13
This is a very interesting report. The behavior you are seeing is based
on this 2004 commit:
commit 0e13d627bebad769498696b5fd0ac821bde5140d
Author: Joe Conway <mail@joeconway.com>
Date: Thu Aug 5 03:30:44 2004 +0000
Require that array literals produce "rectangular" arrays, i.e. all the
subarrays of a given dimension have the same number of elements/subarrays.
Also repair a longstanding undocumented (as far as I can see) ability to
explicitly set array bounds in the array literal syntax. It now can
deal properly with negative array indicies. Modify array_out so that
arrays with non-standard lower bounds (i.e. not 1) are output with
the expicit dimension syntax. This fixes a longstanding issue whereby
arrays with non-default lower bounds had them changed to default
after a dump/reload cycle.
Modify regression tests and docs to suit, and add some minimal
documentation regarding the explicit dimension syntax.
Particularly, this block of code:
if ((nelems_last[nest_level] != 1) &&
(nelems[nest_level] != nelems_last[nest_level]))
ereport(ERROR,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("multidimensional arrays must have "
"array expressions with matching "
"dimensions")));
The nelems_last array is initialized to '1', so when an array comes in
that is one element, no check is made. I doubt this was intended.
What I have done is to change the initalization and test to -1, which
seems to fix the problem.
test=> select cast('{{1}, {2, 3}}' as integer[]);
ERROR: multidimensional arrays must have array expressions with matching dimensions
LINE 1: select cast('{{1}, {2, 3}}' as integer[]);
I was a little worried that this might break existing values stored in
the database, and hence dump/reload, but it seems the arrays are
extended with NULLs so it loads just fine:
CREATE TABLE test(x int[]);
INSERT INTO test SELECT cast('{{1}, {2, 3, 4, 5}}' AS integer[]);
COPY test TO stdout;
{{1,NULL,NULL,NULL},{2,3,4,5}}
Patch attached. The patch controls what strings are accepted as arrays
and will be an incompatibility for 9.4.
Joe, does this sound right?
--
Bruce Momjian <bruce@momjian.us> http://momjian.us
EnterpriseDB http://enterprisedb.com
+ Everyone has their own god. +
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugsAttachments:array.diff (text/x-diff, 2.9K) downloaddiff --git a/src/backend/utils/adt/arrayfuncs.c b/src/backend/utils/adt/arrayfuncs.cnew file mode 100644index 311d0c2..0b05158
*** a/src/backend/utils/adt/arrayfuncs.c
--- b/src/backend/utils/adt/arrayfuncs.c
*************** ArrayCount(const char *str, int *dim, ch
*** 426,432 ****
for (i = 0; i < MAXDIM; ++i)
{
temp[i] = dim[i] = 0;
! nelems_last[i] = nelems[i] = 1;
}
ptr = str;
--- 426,433 ----
for (i = 0; i < MAXDIM; ++i)
{
temp[i] = dim[i] = 0;
! nelems[i] = 1;
! nelems_last[i] = -1;
}
ptr = str;
*************** ArrayCount(const char *str, int *dim, ch
*** 540,547 ****
errmsg("malformed array literal: \"%s\"", str)));
nest_level--;
! if ((nelems_last[nest_level] != 1) &&
! (nelems[nest_level] != nelems_last[nest_level]))
ereport(ERROR,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("multidimensional arrays must have "
--- 541,548 ----
errmsg("malformed array literal: \"%s\"", str)));
nest_level--;
! if (nelems_last[nest_level] != -1 &&
! nelems[nest_level] != nelems_last[nest_level])
ereport(ERROR,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("multidimensional arrays must have "
diff --git a/src/test/regress/expected/arrays.out b/src/test/regress/expected/arrays.outnew file mode 100644index e1b9d7f..6dce5c9
*** a/src/test/regress/expected/arrays.out
--- b/src/test/regress/expected/arrays.out
*************** select cardinality('{{1,2},{3,4},{5,6}}'
*** 1491,1497 ****
6
(1 row)
! select cardinality('{{{1}},{{2,3},{3,4}}}'::int[]);
cardinality
-------------
8
--- 1491,1497 ----
6
(1 row)
! select cardinality('{{{1,9},{5,6}},{{2,3},{3,4}}}'::int[]);
cardinality
-------------
8
diff --git a/src/test/regress/sql/arrays.sql b/src/test/regress/sql/arrays.sqlnew file mode 100644index 64630d9..92af172
*** a/src/test/regress/sql/arrays.sql
--- b/src/test/regress/sql/arrays.sql
*************** select cardinality(array[1,2,3]);
*** 425,431 ****
select cardinality('[2:4]={5,6,7}'::int[]);
select cardinality('{{1,2}}'::int[]);
select cardinality('{{1,2},{3,4},{5,6}}'::int[]);
! select cardinality('{{{1}},{{2,3},{3,4}}}'::int[]);
select array_agg(unique1) from (select unique1 from tenk1 where unique1 < 15 order by unique1) ss;
select array_agg(ten) from (select ten from tenk1 where unique1 < 15 order by unique1) ss;
--- 425,431 ----
select cardinality('[2:4]={5,6,7}'::int[]);
select cardinality('{{1,2}}'::int[]);
select cardinality('{{1,2},{3,4},{5,6}}'::int[]);
! select cardinality('{{{1,9},{5,6}},{{2,3},{3,4}}}'::int[]);
select array_agg(unique1) from (select unique1 from tenk1 where unique1 < 15 order by unique1) ss;
select array_agg(ten) from (select ten from tenk1 where unique1 < 15 order by unique1) ss;
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-bugs@postgresql.org
Cc: bruce@momjian.us, borz_off@cs.msu.su, mail@joeconway.com
Subject: Re: BUG #8399: inconsistent input of multidimensional arrays
In-Reply-To: <20140201011125.GG19957@momjian.us>
References: <E1VEETa-0007KM-8O@wrigleys.postgresql.org> <20140201011125.GG19957@momjian.us>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox