Re: ARRAY[1] || NULL <> array_append(ARRAY[1], NULL)

From: Bruce Momjian <bruce(at)momjian(dot)us>
To: Michael Herold <quabla(at)hemio(dot)de>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, pgsql-docs(at)postgresql(dot)org
Subject: Re: ARRAY[1] || NULL <> array_append(ARRAY[1], NULL)
Date: 2015-11-30 23:59:19
Message-ID: 20151130235919.GC19371@momjian.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-docs

On Mon, Oct 26, 2015 at 11:04:40AM +0100, Michael Herold wrote:
> I just got confused by another behavior of the || operator. I
> thought it might be appropriate to report it here.
>
> The docs say "the result retains the lower bound subscript of the
> left-hand operand’s outer dimension" [1]. That's again not true for
> corner cases.

You didn't quote the entire sentence:

When two arrays with an equal number of dimensions are concatenated, the
-------------------------------------------------------------------
result retains the lower bound subscript of the left-hand operand's
outer dimension.

> Expected:
> # SELECT '{0}'::int[] || '[15:16]={1,2}';
> ----------
> {0,1,2}
>
> Unexpected (lower bound is untouched):
> # SELECT '{}'::int[] || '[15:16]={1,2}';
> ---------------
> [15:16]={1,2}

I would argue that '{}'::int[] is zero dimmensions, so there is no
documented behavior for this.

The C code is:

/*
* short circuit - if one input array is empty, and the other is not, we
* return the non-empty one as the result
*
* if both are empty, return the first one
*/
if (ndims1 == 0 && ndims2 > 0)
PG_RETURN_ARRAYTYPE_P(v2);

I doubt we want to change this.

--
Bruce Momjian <bruce(at)momjian(dot)us> http://momjian.us
EnterpriseDB http://enterprisedb.com

+ As you are, so once was I. As I am, so you will be. +
+ Roman grave inscription +

In response to

Responses

Browse pgsql-docs by date

  From Date Subject
Next Message Michael Herold 2015-12-01 12:22:25 Re: ARRAY[1] || NULL <> array_append(ARRAY[1], NULL)
Previous Message Oleg Bartunov 2015-11-23 15:39:21 Re: pg_rewind source-server connection