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

From Greg Stark
Subject Re: [GENERAL] string_to_array with empty input
Date
Msg-id 4136ffa0903310743n6ad22f51h91bc0b9616e2ed5d@mail.gmail.com
Whole thread Raw
In response to Re: [GENERAL] string_to_array with empty input  (Brendan Jurd <direvus@gmail.com>)
Responses Re: [GENERAL] string_to_array with empty input  (Sam Mason <sam@samason.me.uk>)
List pgsql-hackers
On Tue, Mar 31, 2009 at 7:45 AM, Brendan Jurd <direvus@gmail.com> wrote:
> My first thought was that it should be a zero-element array, because
> then the string_to_array() behaviour would conform to the notion that
> it returns an array with 1 element per string fragment bounded by the
> delimiter.
>
> However, I note that if you provide an empty delimiter, or one which
> doesn't occur anywhere in the source string, you get an array with one
> element, being the entire source string.

Yeah, actually the more I think about it the more I think it would be
strange for most uses to get a singleton array for this case.

What do you really expect to be returned for things like

select count_elements(string_to_array('butter,tea,milk',','))
select count_elements(string_to_array('butter,tea',','))
select count_elements(string_to_array('butter',','))
select count_elements(string_to_array('',','))

...

I could see lists like this being stored when people gather data using
a web form or something and don't want to bother normalizing some
trivial bit of data collection which they'll never individually, but
have to unnest the list for some display purposes.

The cases where it makes more sense to return a singleton array are
going to be parsing things like /etc/password where there are specific
meanings for each element, but when some are optional. I can't think
of any examples offhand though.

--
greg

pgsql-hackers by date:

Previous
From: Sam Mason
Date:
Subject: Re: [GENERAL] string_to_array with empty input
Next
From: Greg Stark
Date:
Subject: Re: [GENERAL] string_to_array with empty input