Re: join of array - Mailing list pgsql-general

From Tom Lane
Subject Re: join of array
Date
Msg-id 19609.1060970315@sss.pgh.pa.us
Whole thread Raw
In response to Re: join of array  (Joe Conway <mail@joeconway.com>)
Responses Re: join of array  (Joe Conway <mail@joeconway.com>)
List pgsql-general
Joe Conway <mail@joeconway.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

pgsql-general by date:

Previous
From: Fernando Nasser
Date:
Subject: Re: query tuning
Next
From: Dennis Gearon
Date:
Subject: Re: Why the duplicate messages to pgsql-general?