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

From Sam Mason
Subject Re: [GENERAL] string_to_array with empty input
Date
Msg-id 20090331153401.GC12225@frubble.xen.chris-lamb.co.uk
Whole thread Raw
In response to Re: [GENERAL] string_to_array with empty input  (Greg Stark <stark@enterprisedb.com>)
Responses Re: [GENERAL] string_to_array with empty input  ("David E. Wheeler" <david@kineticode.com>)
Re: string_to_array with empty input  (Greg Stark <stark@enterprisedb.com>)
Re: [GENERAL] string_to_array with empty input  (justin <justin@emproshunts.com>)
List pgsql-hackers
On Tue, Mar 31, 2009 at 03:43:37PM +0100, Greg Stark wrote:
> 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.

Really? I think it's strange not to!

> 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'd expect 3,2,1 and 1.

That's also a disingenuous example; what would you expect back from:

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

I think the semantics you want is what you'd get from:

  array_filter_blanks(string_to_array($1,$2))

where I defined "array_filter_blanks" in my previous post.

--
  Sam  http://samason.me.uk/

pgsql-hackers by date:

Previous
From: Tom Lane
Date:
Subject: Re: [GENERAL] pgstattuple triggered checkpoint failure and database outage?
Next
From: Tom Lane
Date:
Subject: Re: [GENERAL] string_to_array with empty input