Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1W9P7Q-0001Wr-2p for pgsql-bugs@arkaria.postgresql.org; Sat, 01 Feb 2014 01:11:36 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1W9P7P-0003xh-Ij for pgsql-bugs@arkaria.postgresql.org; Sat, 01 Feb 2014 01:11:35 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1W9P7O-0003xV-Gd for pgsql-bugs@postgresql.org; Sat, 01 Feb 2014 01:11:34 +0000 Received: from momjian.us ([72.94.173.45]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1W9P7K-0003LZ-W7 for pgsql-bugs@postgresql.org; Sat, 01 Feb 2014 01:11:34 +0000 Received: from bruce by momjian.us with local (Exim 4.72) (envelope-from ) id 1W9P7F-0005B7-6r; Fri, 31 Jan 2014 20:11:25 -0500 Date: Fri, 31 Jan 2014 20:11:25 -0500 From: Bruce Momjian To: borz_off@cs.msu.su Cc: pgsql-bugs@postgresql.org, Joe Conway Subject: Re: BUG #8399: inconsistent input of multidimensional arrays Message-ID: <20140201011125.GG19957@momjian.us> References: MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="s5/bjXLgkIwAv6Hi" Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.20 (2009-06-14) X-Pg-Spam-Score: -2.5 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org --s5/bjXLgkIwAv6Hi Content-Type: text/plain; charset=us-ascii Content-Disposition: inline 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 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 http://momjian.us EnterpriseDB http://enterprisedb.com + Everyone has their own god. + --s5/bjXLgkIwAv6Hi Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="array.diff" diff --git a/src/backend/utils/adt/arrayfuncs.c b/src/backend/utils/adt/arrayfuncs.c new file mode 100644 index 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.out new file mode 100644 index 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.sql new file mode 100644 index 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; --s5/bjXLgkIwAv6Hi Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs --s5/bjXLgkIwAv6Hi--