Re: [GENERAL] string_to_array with empty input - Mailing list pgsql-hackers

From David E. Wheeler
Subject Re: [GENERAL] string_to_array with empty input
Date
Msg-id 64185906-1AF5-4CFD-BA24-CE1D07B6E264@kineticode.com
Whole thread Raw
In response to Re: [GENERAL] string_to_array with empty input  (Tom Lane <tgl@sss.pgh.pa.us>)
Responses Re: [GENERAL] string_to_array with empty input  (Tom Lane <tgl@sss.pgh.pa.us>)
Re: [GENERAL] string_to_array with empty input  (Robert Haas <robertmhaas@gmail.com>)
List pgsql-hackers
On Apr 1, 2009, at 9:02 AM, Tom Lane wrote:

>> +1.  I find this argument much more compelling than anything else
>> that's been offered up so far.
>
> Yeah.  It seems to me that if you consider only the case where the
> array
> elements are text, there's a weak preference for considering '' to
> be a
> single empty string; but as soon as you think about any other
> datatype,
> there's a strong preference to consider it a zero-element list.  So I
> too have come around to favor the latter interpretation.  Do we have
> any remaining holdouts?

Well, I'd just point out that the return value of string_to_array() is
text[]. Thus, this is not a problem with string_to_array(), but a
casting problem from text[] to int[]. Making string_to_array() return
a NULL for this case to make casting simpler is addressing the problem
in the wrong place, IMHO. If I want to do this in Perl, for example,
I'd do something like this:

my @ints = grep { defined $_ && $_ ne '' } split ',', $string;

So I split the string into an array, and then remove unreasonable
values. This also allows me to set defaults:

my @ints = map { $_ || 0 } split ',', $string;

This ensures that I get the proper number of records in the example of
something like '1,2,,4'.

So I still think that string_to_array('', ',') should return '{""}',
and how casting is handled should be left to the user to flexibly
handle.

That said, I'm not seeing a simple function for modifying an array.
I'd have to write one for each specific case. :-(

Best,

David




pgsql-hackers by date:

Previous
From: Robert Haas
Date:
Subject: Re: 8.4 open items list
Next
From: justin
Date:
Subject: Re: [GENERAL] string_to_array with empty input