Re: array_to_string

From: Richard Huxton <dev(at)archonet(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Sabin Coanda <sabin(dot)coanda(at)deuromedia(dot)ro>, pgsql-sql(at)postgresql(dot)org
Subject: Re: array_to_string
Date: 2007-03-27 15:45:01
Message-ID: 46093BFD.8060007@archonet.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Tom Lane wrote:
> Richard Huxton <dev(at)archonet(dot)com> writes:
>> Tom Lane wrote:
>>> "Sabin Coanda" <sabin(dot)coanda(at)deuromedia(dot)ro> writes:
>>>> I used the function array_to_string, and I found it ignores NULL values,
>>>> e.g. array_to_string( 'ARRAY[1,NULL,3]', ',' ) returns '1,3'.
>>> Do you have a better idea?
>
>> If you're being strict it should presumably return NULL for the whole
>> string. Or you'd need a "coalesce-to-this" parameter.
>
> If the whole array is NULL it would return NULL, but that seems
> unhelpful for a single element being NULL ...

It's the least surprising choice though. You'd expect array_to_string()
to be a loop doing something like:
output = output || connector || arr[i]
Of course this will produce NULL if any of the inputs are NULL. Now that
might be unhelpful, but that's NULL for you.

All you're doing is (presumably) adding a hidden test to the loop where
Sabin seemed to expect COALESCE(arr[i],<something>). I'm not sure either
is better across the board.

The problem comes with what <something> should be. You're either looking
at a third parameter or perhaps more cleanly you need a
coalesce_array(ARRAY,VAL) function which produces an output array where
all the null elemts are replaced by VAL.

--
Richard Huxton
Archonet Ltd

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Peter Eisentraut 2007-03-27 15:58:42 Re: Foreign Unique Constraint
Previous Message Tom Lane 2007-03-27 15:35:44 Re: array_to_string