Re: join of array

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Joe Conway <mail(at)joeconway(dot)com>
Cc: elein <elein(at)varlena(dot)com>, Pavel Stehule <stehule(at)kix(dot)fsv(dot)cvut(dot)cz>, pgsql-general(at)postgresql(dot)org
Subject: Re: join of array
Date: 2003-08-15 17:58:35
Message-ID: 19609.1060970315@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-patches

Joe Conway <mail(at)joeconway(dot)com> writes:
> elein wrote:
>> I do not think this is right. I think the current behaviour
>> is right. You are effectively dereferencing or flattening
>> the second array which changes the definition of the second
>> object.

> It makes sense in analogy to
> ARRAY[1,2] || ARRAY[3,4] == '{1,2,3,4}'

I agree with Joe. The spec is quite clear about what to do in the
one-dimensional case: the original arrays lose their separate identity.
In the multi-dimensional case, they should lose their separate
identities in the outermost dimension.

I believe the behavior Elein wants can be had by writing
ARRAY[ n_d_array , n_d_array ]
(Joe, would you confirm that's true, and document it? I don't think
either section 8.10 or section 4.2.8 makes clear that you can build
arrays from smaller array values rather than just scalars.) As long as
we have that alternative, it's not necessary that concatenation do the
same thing.

Another argument for doing it this way is that it makes array
concatenation associative, which is a nice bit of symmetry. Consider

ARRAY[[1,1],[2,2]] || ARRAY[[3,3],[4,4]] ||
ARRAY[[5,5],[6,6]] || ARRAY[[7,7],[8,8]]

Right now, with the default left-to-right association you get

{{{1,1},{2,2}},{{3,3},{4,4}},{{5,5},{6,6}},{{7,7},{8,8}}}

but if you parenthesize it differently you can get a different answer:

regression=# select (ARRAY[[1,1],[2,2]] || ARRAY[[3,3],[4,4]]) || (ARRAY[[5,5],[6,6]] || ARRAY[[7,7],[8,8]]);
?column?
---------------------------------------------------------------
{{{{1,1},{2,2}},{{3,3},{4,4}}},{{{5,5},{6,6}},{{7,7},{8,8}}}}
(1 row)

With the flattening approach all the intermediate results will remain
2-D arrays and so you get the same answer for all parenthesizations,
namely {{1,1},{2,2},{3,3},{4,4},{5,5},{6,6},{7,7},{8,8}}. That strikes
me as more nearly the intuitive meaning of "concatenation" than what
we've got now.

(Cases involving some N-D and some N+1-D inputs remain non-associative,
though, which is a tad annoying. Maybe it's okay seeing that the inputs
are of different kinds.)

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message The Hermit Hacker 2003-08-15 18:00:36 Re: Why the duplicate messages to pgsql-general?
Previous Message Jan Wieck 2003-08-15 17:51:23 Re: Why the duplicate messages to pgsql-general?

Browse pgsql-patches by date

  From Date Subject
Next Message Joe Conway 2003-08-15 18:12:54 Re: join of array
Previous Message Joe Conway 2003-08-15 17:41:15 Re: join of array