Re: Surprising results from array concatenation

From: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
To: Mike Blackwell <mike(dot)blackwell(at)rrd(dot)com>
Cc: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: Surprising results from array concatenation
Date: 2017-04-25 16:41:30
Message-ID: CAKFQuwY=Y4dy5Xhtj4F2De+0vapHi=BFb71GDLUOW6+d065dgw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Tue, Apr 25, 2017 at 9:26 AM, Mike Blackwell <mike(dot)blackwell(at)rrd(dot)com>
wrote:

> The docs (section 9.18 for PG 9.6) show as an example for array
> concatenation
>
> ARRAY[4,5,6] || 7
>
> which works fine. However, trying the same with an array of text doesn't
> work:
>
> # select array['a','b','c'] || 'd';
> ERROR: malformed array literal: "d"
> LINE 1: select array['a','b','c'] || 'd';
> ^
> DETAIL: Array value must start with "{" or dimension information.
>
> Casting the second value to TEXT works.
>
> # select array['a','b','c'] || 'd'::TEXT;
> ?column?
> -----------
> {a,b,c,d}
> (1 row)
>
> The assumption that the second argument is an array constant seems
> surprising
>

​It has to assume something. And for better and worse it has to assume it
without looking at the actual value.​ Choosing the scalar variant here
would be more convenient but choosing the same type as the left-hand side
is logical. Note that the concatenation operator/function isn't the one
complaining - if it was then intelligence could be inserted. The type
conversion code doesn't have the luxury.

I don't suppose one would get far arguing to modify the array input
function to convert a value that doesn't look like an array into a single
element text array. The "implicit conversion" that involves is something
we've gotten away from and seems like it would be worse that requiring the
explicit typing.

David J.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2017-04-25 17:53:17 Re: Surprising results from array concatenation
Previous Message Mike Blackwell 2017-04-25 16:26:53 Surprising results from array concatenation