Thread: About the behavior of array_length() for empty array

About the behavior of array_length() for empty array

From
"iihero"
Date:

postgres=# select '{}'::integer[] = array[[]]::integer[][];
 ?column?
----------
 t
postgres=# select '{}'::integer[] = array[[[]]]::integer[][][];
 ?column?
----------
 t
From this point of view, seems N dimensions of empty array all are equivalent.

But the result of array_length of them always are null.

postgres=# select array_length('{}'::integer[],0);
 array_length
--------------

postgres=# select array_length(array[[[]]]::integer[][][],0);
 array_length
--------------
postgres=# select array_length(array[[[]]]::integer[][][],3) is null;
 ?column?
----------
 t

I just think any "empty" array length should return 0, but not null.  It's not a NULL array.

Is there standard definition of this behavior? 

------------------
----------------------------------------------------------
iihero(Xiong He)  http://www.sql9.com
----------------------------------------------------------

 

Re: About the behavior of array_length() for empty array

From
Robert Haas
Date:
On Thu, Apr 5, 2012 at 8:35 PM, iihero <iihero@qq.com> wrote:
> From this point of view, seems N dimensions of empty array all are
> equivalent.

Yes.  It's effectively viewed as a 0-dimensional array.

> Is there standard definition of this behavior?

No.  Multi-dimensional arrays are a PostgreSQL extension.

-- 
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company