pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
BUG #13934: wrong result of split_part with char value
5+ messages / 4 participants
[nested] [flat]

* BUG #13934: wrong result of split_part with char value
@ 2016-02-08 15:11 dominik.kosiorek@infobright.com
  2016-02-09 02:00 ` Re: BUG #13934: wrong result of split_part with char value David G. Johnston <david.g.johnston@gmail.com>
  0 siblings, 1 reply; 5+ messages in thread

From: dominik.kosiorek@infobright.com @ 2016-02-08 15:11 UTC (permalink / raw)
  To: pgsql-bugs

The following bug has been logged on the website:

Bug reference:      13934
Logged by:          Dominik Kosiorek
Email address:      dominik.kosiorek@infobright.com
PostgreSQL version: 9.2.2
Operating system:   Ubuntu 14
Description:        

create table string1postgres(
a1 int,
a2 char(65),
a3pattern char(15),
a4field int
) 
insert into string1postgres values(72,'abcd defg poir abcde m ert g d c v b
gthb',' ',1);

select split_part(a2,a3pattern,a4field) from string1postgres;

-------------------
the result of split part is:
-------------------
abcd defg poir abcde m ert g d c v b gthb

-------------------
instead of:
-------------------
abcd

-------------------
This defect is only with char type. On varchar result is correct.


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: BUG #13934: wrong result of split_part with char value
  2016-02-08 15:11 BUG #13934: wrong result of split_part with char value dominik.kosiorek@infobright.com
@ 2016-02-09 02:00 ` David G. Johnston <david.g.johnston@gmail.com>
  2016-02-09 15:29   ` Re: BUG #13934: wrong result of split_part with char value Joe Conway <mail@joeconway.com>
  0 siblings, 1 reply; 5+ messages in thread

From: David G. Johnston @ 2016-02-09 02:00 UTC (permalink / raw)
  To: dominik.kosiorek@infobright.com <dominik.kosiorek@infobright.com>; +Cc: pgsql-bugs

On Monday, February 8, 2016, <dominik.kosiorek@infobright.com> wrote:

> The following bug has been logged on the website:
>
> Bug reference:      13934
> Logged by:          Dominik Kosiorek
> Email address:      dominik.kosiorek@infobright.com <javascript:;>
> PostgreSQL version: 9.2.2
> Operating system:   Ubuntu 14
> Description:
>
> create table string1postgres(
> a1 int,
> a2 char(65),
> a3pattern char(15),
> a4field int
> )
> insert into string1postgres values(72,'abcd defg poir abcde m ert g d c v b
> gthb',' ',1);
>
> select split_part(a2,a3pattern,a4field) from string1postgres;
>
> -------------------
> the result of split part is:
> -------------------
> abcd defg poir abcde m ert g d c v b gthb
>
> -------------------
> instead of:
> -------------------
> abcd
>
> -------------------
> This defect is only with char type. On varchar result is correct.
>
>
This calls for another round of "don't use char” advice...

It is not a bug but a result of the fact that trailing white space in char
values is able to be trimmed away thus leaving you the empty string and no
splitting.

You may read the documentation for details:

http://www.postgresql.org/docs/9.5/static/datatype-character.html

And search the Internet for numerous postings as to why you should avoid
char.

Use to text or varchar instead.

David J.

^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: BUG #13934: wrong result of split_part with char value
  2016-02-08 15:11 BUG #13934: wrong result of split_part with char value dominik.kosiorek@infobright.com
  2016-02-09 02:00 ` Re: BUG #13934: wrong result of split_part with char value David G. Johnston <david.g.johnston@gmail.com>
@ 2016-02-09 15:29   ` Joe Conway <mail@joeconway.com>
  2016-02-09 15:55     ` Re: BUG #13934: wrong result of split_part with char value Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 5+ messages in thread

From: Joe Conway @ 2016-02-09 15:29 UTC (permalink / raw)
  To: David G. Johnston <david.g.johnston@gmail.com>; dominik.kosiorek@infobright.com <dominik.kosiorek@infobright.com>; +Cc: pgsql-bugs

On 02/08/2016 06:00 PM, David G. Johnston wrote:
> On Monday, February 8, 2016, <dominik.kosiorek@infobright.com
> This calls for another round of "don't use char” advice...
> 
> It is not a bug but a result of the fact that trailing white space in
> char values is able to be trimmed away thus leaving you the empty string
> and no splitting.

In other words, when you insert ' ' into string1postgres.a3pattern,
which is defined as char(15), the single space is trimmed leaving an
empty string:

test=# select '***' || a3pattern || '!!!' from string1postgres;
 ?column?
----------
 ***!!!
(1 row)

And if you feed an actual space to split_part(), it works as expected.

test=# select split_part(a2,' ',a4field) from string1postgres;
 split_part
------------
 abcd
(1 row)

HTH,

Joe

-- 
Crunchy Data - http://crunchydata.com
PostgreSQL Support for Secure Enterprises
Consulting, Training, & Open Source Development

Attachments:

  signature.asc (application/pgp-signature, 835B) download

^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: BUG #13934: wrong result of split_part with char value
  2016-02-08 15:11 BUG #13934: wrong result of split_part with char value dominik.kosiorek@infobright.com
  2016-02-09 02:00 ` Re: BUG #13934: wrong result of split_part with char value David G. Johnston <david.g.johnston@gmail.com>
  2016-02-09 15:29   ` Re: BUG #13934: wrong result of split_part with char value Joe Conway <mail@joeconway.com>
@ 2016-02-09 15:55     ` Tom Lane <tgl@sss.pgh.pa.us>
  2016-02-09 16:14       ` Re: BUG #13934: wrong result of split_part with char value David G. Johnston <david.g.johnston@gmail.com>
  0 siblings, 1 reply; 5+ messages in thread

From: Tom Lane @ 2016-02-09 15:55 UTC (permalink / raw)
  To: Joe Conway <mail@joeconway.com>; +Cc: David G. Johnston <david.g.johnston@gmail.com>; dominik.kosiorek@infobright.com <dominik.kosiorek@infobright.com>; pgsql-bugs

Joe Conway <mail@joeconway.com> writes:
> In other words, when you insert ' ' into string1postgres.a3pattern,
> which is defined as char(15), the single space is trimmed leaving an
> empty string:

> test=# select '***' || a3pattern || '!!!' from string1postgres;
>  ?column?
> ----------
>  ***!!!
> (1 row)

Actually, I believe the space-trimming happens when the char(n) value
is coerced to type text in preparation for passing it to the || operator
(which takes text).  Since trailing spaces are considered insignificant
in char(n), whereas they definitely are significant in text, this is a
reasonable thing to do, at least in some contexts.

			regards, tom lane


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: BUG #13934: wrong result of split_part with char value
  2016-02-08 15:11 BUG #13934: wrong result of split_part with char value dominik.kosiorek@infobright.com
  2016-02-09 02:00 ` Re: BUG #13934: wrong result of split_part with char value David G. Johnston <david.g.johnston@gmail.com>
  2016-02-09 15:29   ` Re: BUG #13934: wrong result of split_part with char value Joe Conway <mail@joeconway.com>
  2016-02-09 15:55     ` Re: BUG #13934: wrong result of split_part with char value Tom Lane <tgl@sss.pgh.pa.us>
@ 2016-02-09 16:14       ` David G. Johnston <david.g.johnston@gmail.com>
  0 siblings, 0 replies; 5+ messages in thread

From: David G. Johnston @ 2016-02-09 16:14 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Joe Conway <mail@joeconway.com>; dominik.kosiorek@infobright.com <dominik.kosiorek@infobright.com>; pgsql-bugs

On Tue, Feb 9, 2016 at 8:55 AM, Tom Lane <tgl@sss.pgh.pa.us> wrote:

> Joe Conway <mail@joeconway.com> writes:
> > In other words, when you insert ' ' into string1postgres.a3pattern,
> > which is defined as char(15), the single space is trimmed leaving an
> > empty string:
>
> > test=# select '***' || a3pattern || '!!!' from string1postgres;
> >  ?column?
> > ----------
> >  ***!!!
> > (1 row)
>
> Actually, I believe the space-trimming happens when the char(n) value
> is coerced to type text in preparation for passing it to the || operator
> (which takes text).  Since trailing spaces are considered insignificant
> in char(n), whereas they definitely are significant in text, this is a
> reasonable thing to do, at least in some contexts.
>
>
​And in the OP:

​
 select split_part(a2,a3pattern,a4field) from string1postgres;

​split_part likewise takes text, not char, and so an implicit conversion
and trimming takes place.

David j.
​

^ permalink  raw  reply  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2016-02-09 16:14 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2016-02-08 15:11 BUG #13934: wrong result of split_part with char value dominik.kosiorek@infobright.com
2016-02-09 02:00 ` David G. Johnston <david.g.johnston@gmail.com>
2016-02-09 15:29   ` Joe Conway <mail@joeconway.com>
2016-02-09 15:55     ` Tom Lane <tgl@sss.pgh.pa.us>
2016-02-09 16:14       ` David G. Johnston <david.g.johnston@gmail.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox