pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
BUG #8399: inconsistent input of multidimensional arrays
5+ messages / 3 participants
[nested] [flat]

* BUG #8399: inconsistent input of multidimensional arrays
@ 2013-08-27 08:18  borz_off@cs.msu.su
  0 siblings, 1 reply; 5+ messages in thread

From: borz_off@cs.msu.su @ 2013-08-27 08:18 UTC (permalink / raw)
  To: pgsql-bugs

The following bug has been logged on the website:

Bug reference:      8399
Logged by:          Alexey Borzov
Email address:      borz_off@cs.msu.su
PostgreSQL version: 9.3rc1
Operating system:   irrelevant
Description:        

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



-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: BUG #8399: inconsistent input of multidimensional arrays
@ 2014-02-01 01:11  Bruce Momjian <bruce@momjian.us>
  parent: borz_off@cs.msu.su
  0 siblings, 1 reply; 5+ messages in thread

From: Bruce Momjian @ 2014-02-01 01:11 UTC (permalink / raw)
  To: borz_off@cs.msu.su; +Cc: pgsql-bugs; Joe Conway <mail@joeconway.com>

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-bugs

Attachments:

array.diff (text/x-diff, 2.9K) download 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;
^ permalink raw reply [nested|flat] 5+ messages in thread

* Re: BUG #8399: inconsistent input of multidimensional arrays
@ 2014-02-01 01:46  Joe Conway <mail@joeconway.com>
  parent: Bruce Momjian <bruce@momjian.us>
  0 siblings, 1 reply; 5+ messages in thread

From: Joe Conway @ 2014-02-01 01:46 UTC (permalink / raw)
  To: Bruce Momjian <bruce@momjian.us>; borz_off@cs.msu.su; +Cc: pgsql-bugs

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 01/31/2014 05:11 PM, Bruce Momjian wrote:
> 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.

> Joe, does this sound right?

I believe back when that 2004 commit was made, arrays did not allow
NULL elements at all. So I guess this is an unintended side effect of,
or a latent bug exposed by, that change.

Let me take a closer look...

Joe

- -- 
Joe Conway
credativ LLC: http://www.credativ.us
Linux, PostgreSQL, and general Open Source
Training, Service, Consulting, & 24x7 Support
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.14 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQIcBAEBAgAGBQJS7FH1AAoJEDfy90M199hlMzUP/1e6h0UioXC7HkEgW4DLrI4v
V1lVWu3BB9rmQ6RBJMTA6/wJXm28X5etGrDAt0prQPMsvffNcSxZt5gStmMbvCDo
9+90ZrIYdjNJ8GKzojXYsPj7YDzXgkroyYIYW7fUyzjsJqDt8pfR8da5qDJeWyS8
44teI7hWd4RDRK2kBZlpr6gk/J8CSawBR9Noh5PdG1M39oeep2DcdjLbj+XNZB33
by4KiGi+Al8gLLpmyCZCVCukK92MrmOrauz9gyOto6wdcMjyb+rzdLnyAPUG5B/m
Kk+1UZhWSKKVi4DlujHOwzvAOlLpVMSYyPb5nwACkc2dDo42Huy3hyP83lSfaCiQ
Qkfvu4XglLvio0T/Ym1+dHYAEOdxn3ZdJDK2/UUuPTZwDjQbbI1+hHdIL3cJg7eH
imBSQz3aiaoOL98/CBtwOromNEeKlLzqLqVZO9qiJgWJlLgbXtJEGRjp/BKYZwYy
EFwxGPL0kEyS8LcGZbGVrRWrXgYerzhYahKuVgtV26HlNeml2c7bd0g7ldM5Tzzc
K8g6/sH1/Kx0DUsRsx2EiCitC9DE3mXn5ZYsWxMTYduFPR6T6qcCkNML6ZlSWhHS
Oi/bcQs3sNLOyCmkcXbxPHHTfPtrX7Zenj8JPSh8tkDmup9YqmtD1QHsqySxKpK1
eDWexdJBD/Ds5waDGitk
=9ygd
-----END PGP SIGNATURE-----


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: BUG #8399: inconsistent input of multidimensional arrays
@ 2014-02-01 05:49  Joe Conway <mail@joeconway.com>
  parent: Joe Conway <mail@joeconway.com>
  0 siblings, 1 reply; 5+ messages in thread

From: Joe Conway @ 2014-02-01 05:49 UTC (permalink / raw)
  To: Bruce Momjian <bruce@momjian.us>; borz_off@cs.msu.su; +Cc: pgsql-bugs

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 01/31/2014 05:46 PM, Joe Conway wrote:
> On 01/31/2014 05:11 PM, Bruce Momjian wrote:
>> Joe, does this sound right?

> Let me take a closer look...

I had forgotten how convoluted the array code is ;-)

Yes, it looks correct to me, although I think I would initialize
nelems_last[i] to 0 rather than -1.

Joe

- -- 
Joe Conway
credativ LLC: http://www.credativ.us
Linux, PostgreSQL, and general Open Source
Training, Service, Consulting, & 24x7 Support
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.14 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQIcBAEBAgAGBQJS7IsEAAoJEDfy90M199hlH4YP/1ERkU0XEnZc2FWVANg8NLKk
kW3rIa+Q8UUHPVPuhD1GXSXsxBBs5XwJz9rWaTB05iO678eft1FvT2IHciCMKX2m
83FFkXQEEVLr26py+zBnuOVbnmpQ2ubmbr4ZL2fG7KYRzJkPGw4YNmXVWXgyOmOQ
gltB3G1q2bNvovIz8OAXkFxkDOjdxfWdbRpoT6pYTGt/wIa5AfWLYxU5v30WZXSC
rr0b5rJtn6y4+L2ZsU8HptMVOklbZOkD7+Oee7h5UMB2MazBFMKksaCuBuPGXqJj
2MKtb47xccWWY1mNVF9gdPUSIXtYDDtNjlsBnbP5bWO09UOEuSmoBZd4qG4XKI4b
3dLRskgHZfT5gZVEDaF+vS/7OHI0nGPKhgfT1kNGJZpSpbo5M7lRdcX+lCO4mTkz
7JPZoWmsUqAc8n0eEX/UuwTJL/mzFzowJ0YxaVnqRooIhXwDnQjYCydSuA8mtpTP
+qY4IV/4u6yK7c5tM8cOX9ZDkriXvF6yypeIvqKLzHwnu4/6D0RHIusY0fFC+GGY
1jBWHjLYywl59ThauRqWhONiqN1F0roOK0nbdwxe3mw8n+hu/wwmCpyWlPDxUaEa
CFXEN5b6uLXDM3Dy0RzvyFn2o0/c9dlleZWp7L8cV+WcU7iULo0LNMli90I+FK9z
Cho3QAatYJ6QovloFps/
=XbLY
-----END PGP SIGNATURE-----


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: BUG #8399: inconsistent input of multidimensional arrays
@ 2014-02-01 15:50  Bruce Momjian <bruce@momjian.us>
  parent: Joe Conway <mail@joeconway.com>
  0 siblings, 0 replies; 5+ messages in thread

From: Bruce Momjian @ 2014-02-01 15:50 UTC (permalink / raw)
  To: Joe Conway <mail@joeconway.com>; +Cc: borz_off@cs.msu.su; pgsql-bugs

On Fri, Jan 31, 2014 at 09:49:56PM -0800, Joe Conway wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> On 01/31/2014 05:46 PM, Joe Conway wrote:
> > On 01/31/2014 05:11 PM, Bruce Momjian wrote:
> >> Joe, does this sound right?
> 
> > Let me take a closer look...
> 
> I had forgotten how convoluted the array code is ;-)
> 
> Yes, it looks correct to me, although I think I would initialize
> nelems_last[i] to 0 rather than -1.

Thanks for the review.  I went with zero as you suggested.  Applied
patch attached.

-- 
  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-bugs

Attachments:

array.diff (text/x-diff, 2.9K) download diff --git a/src/backend/utils/adt/arrayfuncs.c b/src/backend/utils/adt/arrayfuncs.c new file mode 100644 index 311d0c2..91df184 *** a/src/backend/utils/adt/arrayfuncs.c --- b/src/backend/utils/adt/arrayfuncs.c *************** ArrayCount(const char *str, int *dim, ch *** 425,432 **** for (i = 0; i < MAXDIM; ++i) { ! temp[i] = dim[i] = 0; ! nelems_last[i] = nelems[i] = 1; } ptr = str; --- 425,432 ---- for (i = 0; i < MAXDIM; ++i) { ! temp[i] = dim[i] = nelems_last[i] = 0; ! nelems[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 " --- 540,547 ---- errmsg("malformed array literal: \"%s\"", str))); nest_level--; ! if (nelems_last[nest_level] != 0 && ! 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;
^ permalink raw reply [nested|flat] 5+ messages in thread


end of thread, other threads:[~2014-02-01 15:50 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2013-08-27 08:18 BUG #8399: inconsistent input of multidimensional arrays borz_off@cs.msu.su
2014-02-01 01:11 ` Bruce Momjian <bruce@momjian.us>
2014-02-01 01:46   ` Joe Conway <mail@joeconway.com>
2014-02-01 05:49     ` Joe Conway <mail@joeconway.com>
2014-02-01 15:50       ` Bruce Momjian <bruce@momjian.us>

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