Re: So what's an "empty" array anyway? - Mailing list pgsql-hackers

From Sam Mason
Subject Re: So what's an "empty" array anyway?
Date
Msg-id 20081112154552.GJ2459@frubble.xen.chris-lamb.co.uk
Whole thread Raw
In response to Re: So what's an "empty" array anyway?  (Peter Eisentraut <peter_e@gmx.net>)
List pgsql-hackers
On Wed, Nov 12, 2008 at 02:12:19PM +0200, Peter Eisentraut wrote:
> Tom Lane wrote:
> >Seems like we ought to clean this up.  I'm not sure which way to jump
> >though: should we decree that arrays of no elements must always have
> >zero dimensions, or should we get rid of that and standardize on, say,
> >1-D array with lower bound 1 and upper bound 0?
> 
> It was pointed out to me today that a zero-dimensional matrix is a 
> scalar.  This makes a bit of sense, if you say that
> 
> '{{56}}' is of type int[][], 2 dimensions
> '{56}' is of type int[], 1 dimension
> '56' is of type int, 0 dimensions
> 
> Notice that the number of brace pairs in the literal matches the number 
> of bracket pairs in the type declaration.
> 
> By that logic, '{}' has one dimension.  I think this also works best in 
> practice, for example with array concatenation.

I could argue a very convincing case that '{}' should be of *at least*
one dimension.  The fact that you can concat it onto any other size
array suggests the current implementation of PG agrees---probably for
very different reasons though. '{{}}' would imply an array of at least
two dimensions, but doesn't appear to be currently supported.

Another weirdo is the fact that this works:
 (ARRAY[ARRAY[3]])[1][1];

whereas this doesn't:
 ((ARRAY[ARRAY[3]])[1])[1];

which seems a little strange, although from a historical point of view
I can understand why it behaves the way it does.  From the current
behavior, the following syntax would make more sense:
 (ARRAY[ARRAY[3]])[1,1];

I.e. it's obvious that you're pulling stuff out of a two dimensional
array.  With the current syntax, I'd expect to get slices of higher
dimensional arrays back:
 (ARRAY[ARRAY[3,4]])[1];

would evaluate to:
 ARRAY[3,4]

We then get onto issues of arrays currently having to be rectangular;
even though it's trivial to express things that don't want rectangular
arrays:
 SELECT array(VALUES (ARRAY[1]),(ARRAY[2,3]));

I'd expect to get a 1D array of 1D arrays back from this---note that
this is distinct from a 2D array, which would indeed be rectangular and
wouldn't work at all.  I'd expect the following to be invalid:
 SELECT array(VALUES (ARRAY[1]),(ARRAY[ARRAY[2,3]]));

I could go on, but I'm not sure of any good ways of keeping this all
compatible with the current syntax or semantics.


 Sam


pgsql-hackers by date:

Previous
From: "Robert Haas"
Date:
Subject: Re: So what's an "empty" array anyway?
Next
From: Tom Lane
Date:
Subject: Re: [GENERAL] Very slow queries w/ NOT IN preparation (seems like a bug, test case)