Skip site navigation (1) Skip section navigation (2)

Re: string_to_array with empty input

From: Sam Mason <sam(at)samason(dot)me(dot)uk>
To: pgsql-general(at)postgresql(dot)org, pgsql-hackers(at)postgresql(dot)org
Subject: Re: string_to_array with empty input
Date: 2009-03-31 15:34:01
Message-ID: 20090331153401.GC12225@frubble.xen.chris-lamb.co.uk (view raw or flat)
Thread:
Lists: pgsql-generalpgsql-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(at)gmail(dot)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/

In response to

Responses

pgsql-hackers by date

Next:From: Tom LaneDate: 2009-03-31 15:37:16
Subject: Re: [HACKERS] string_to_array with empty input
Previous:From: Tom LaneDate: 2009-03-31 15:31:18
Subject: Re: [GENERAL] pgstattuple triggered checkpoint failure and database outage?

pgsql-general by date

Next:From: Marcin KrolDate: 2009-03-31 15:35:58
Subject: weird problem with PG 8.1
Previous:From: Tom LaneDate: 2009-03-31 15:31:18
Subject: Re: [GENERAL] pgstattuple triggered checkpoint failure and database outage?

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group