Re: string_to_array with empty input

From: justin <justin(at)emproshunts(dot)com>
To: Greg Stark <stark(at)enterprisedb(dot)com>
Cc: Sam Mason <sam(at)samason(dot)me(dot)uk>, pgsql-general(at)postgresql(dot)org, pgsql-hackers(at)postgresql(dot)org
Subject: Re: string_to_array with empty input
Date: 2009-03-31 17:44:19
Message-ID: 49D25673.20406@emproshunts.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-hackers

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
<title></title>
</head>
<body bgcolor="#ffffff" text="#000000">
Greg Stark wrote:
<blockquote
cite="mid:4136ffa0903311006v15eb3a25xf3fa94a8ca089fd6(at)mail(dot)gmail(dot)com"
type="cite">
<pre wrap="">On Tue, Mar 31, 2009 at 5:48 PM, justin <a class="moz-txt-link-rfc2396E" href="mailto:justin(at)emproshunts(dot)com">&lt;justin(at)emproshunts(dot)com&gt;</a> wrote:
</pre>
<blockquote type="cite">
<pre wrap="">But consider &nbsp;this fails also

select string_to_array('1, , 3', ',' )::int[] =&gt; ERROR: &nbsp;invalid input
syntax for integer: " "

yet this works

select string_to_array('1, 2, 3',',')::int[]
</pre>
</blockquote>
<pre wrap=""><!---->

Sure, and the analogous pair string_to_array(' ',',')::int[] and
string_to_array('1 ',',')::int[] behave similarly.

The point is that '' could represent no items or one empty string. We
get to pick which one and in any use case where the string was a list
of objects it's almost certainly intended to be an empty list. And
databases are almost always processing lists of things.

I think the only use case where you want it to be a singleton list of
an empty string is when you're doing string parsing such as building a
lexer or something like that, which is isn't a typical use for sql
code.
</pre>
</blockquote>
<br>
I disagree.&nbsp;&nbsp; Casting a string to something else can be a very error
prone to begin with. <br>
Having&nbsp; string_to_array() to deal with that possibility&nbsp; is out of its
scope IMHO.<br>
<br>
Consider this.&nbsp; I have intelligent part numbers&nbsp; that need to be split
apart to simplify searching&nbsp; and do math with. <br>
<br>
string_to_array(' F-2500-50 ', '-' ) ::int[]<br>
<br>
Still fails with an error as expected.&nbsp; what is the difference between
' '&nbsp; and 'F'&nbsp; <br>
<br>
So before doing any thing a test needs to be done to verify the
contents, so it can be casted to something else.<br>
</body>
</html>

Attachment Content-Type Size
unknown_filename text/html 2.0 KB

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Joshua D. Drake 2009-03-31 17:50:44 Pg Conference East: Registration closing
Previous Message Alban Hertroys 2009-03-31 17:39:21 Re: [GENEAL] dynamically changing table

Browse pgsql-hackers by date

  From Date Subject
Next Message Greg Stark 2009-03-31 17:53:33 Re: string_to_array with empty input
Previous Message Josh Berkus 2009-03-31 17:16:49 Re: Partitioning feature ...