pg.ddx.io pgsql-bugs@postgresql.org mailing list archive
help / color / mirror / Atom feedBUG #15069: group by after regexp_replace
3+ messages / 3 participants
[nested] [flat]
* BUG #15069: group by after regexp_replace
@ 2018-02-15 15:14 PG Bug reporting form <noreply@postgresql.org>
0 siblings, 2 replies; 3+ messages in thread
From: PG Bug reporting form @ 2018-02-15 15:14 UTC (permalink / raw)
To: pgsql-bugs@lists.postgresql.org; +Cc: koglep@gmail.com
The following bug has been logged on the website:
Bug reference: 15069
Logged by: Ilhwan Ko
Email address: koglep@gmail.com
PostgreSQL version: 9.6.7
Operating system: macOS 10.12
Description:
create table test_t (a varchar(10), b varchar (20), c int);
insert into test_t (a,b,c) values ('a', 'a', 1);
insert into test_t (a,b,c) values (' a', 'a', 2);
insert into test_t (a,b,c) values (' a', 'a', 4);
insert into test_t (a,b,c) values (' a', 'a', 8);
insert into test_t (a,b,c) values ('b', 'b', 1);
insert into test_t (a,b,c) values (' b', 'b', 2);
insert into test_t (a,b,c) values (' b', 'b', 4);
insert into test_t (a,b,c) values (' b', 'b', 8);
select keyword, sum(t.c) as cc
from
(
select upper(regexp_replace(a, '\\s+', '')) as keyword, c
from test_t
) t
group by keyword;
select upper(regexp_replace(a, '\\s+', '')) as keyword, sum(c)
from test_t
group by upper(regexp_replace(a, '\\s+', ''));
select keyword, sum(t.c) as cc
from
(
select upper(regexp_replace(b, '\\s+', '')) as keyword, c
from test_t
) t
group by keyword;
select upper(regexp_replace(b, '\\s+', '')) as keyword, sum(c)
from test_t
group by upper(regexp_replace(b, '\\s+', ''));
I expected to get the same results regarding to above four queries.
However, they were different.
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: BUG #15069: group by after regexp_replace
@ 2018-02-15 15:25 David G. Johnston <david.g.johnston@gmail.com>
parent: PG Bug reporting form <noreply@postgresql.org>
1 sibling, 0 replies; 3+ messages in thread
From: David G. Johnston @ 2018-02-15 15:25 UTC (permalink / raw)
To: koglep@gmail.com; pgsql-bugs@lists.postgresql.org
On Thu, Feb 15, 2018 at 8:14 AM, PG Bug reporting form <
noreply@postgresql.org> wrote:
> The following bug has been logged on the website:
>
> Bug reference: 15069
> Logged by: Ilhwan Ko
> Email address: koglep@gmail.com
> PostgreSQL version: 9.6.7
> Operating system: macOS 10.12
> Description:
>
> select upper(regexp_replace(b, '\\s+', '')) as keyword, sum(c)
> from test_t
> group by upper(regexp_replace(b, '\\s+', ''));
>
> I expected to get the same results regarding to above four queries.
> However, they were different.
>
>
Not a bug - you mis-understand string literal syntax and escaping. What
you are asking to replace is "a backslash followed by one or more "s"es.
see:
https://www.postgresql.org/docs/10/static/sql-syntax-lexical.html#SQL-SYNTAX-CONSTANTS
If you want to leave the literal as-is you need to write:
E'\\s+'
The way I prefer is to keep the "E" omitted and write:
'\s+'
Without the "E" the backslash is not an escape character in a PostgreSQL
literal and so the backslash in the regex doesn't need to be protected. By
protecting it you are actually protecting the backslash in front of the "s"
thus causing it to become two separate "symbols", "\" and "s" - and the +
then applies to the literal "s".
David J.
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: BUG #15069: group by after regexp_replace
@ 2018-02-15 15:34 Andrew Gierth <andrew@tao11.riddles.org.uk>
parent: PG Bug reporting form <noreply@postgresql.org>
1 sibling, 0 replies; 3+ messages in thread
From: Andrew Gierth @ 2018-02-15 15:34 UTC (permalink / raw)
To: pgsql-bugs@lists.postgresql.org; koglep@gmail.com
>>>>> "PG" == PG Bug reporting form <noreply@postgresql.org> writes:
PG> select upper(regexp_replace(a, '\\s+', '')) as keyword
This regexp doesn't do what you seem to be expecting. With
standard_conforming_strings enabled, which is the default, \ is not a
special character to the string literal parser, so the regexp engine is
seeing a regexp of \\s+ which means "match the literal character \
followed by one or more lowercase "s" characters". Accordingly the
regexp never matches on your input.
To remove whitespace use '\s+' or E'\\s+' or '[[:space:]]+'
--
Andrew (irc:RhodiumToad)
^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~2018-02-15 15:34 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2018-02-15 15:14 BUG #15069: group by after regexp_replace PG Bug reporting form <noreply@postgresql.org>
2018-02-15 15:25 ` David G. Johnston <david.g.johnston@gmail.com>
2018-02-15 15:34 ` Andrew Gierth <andrew@tao11.riddles.org.uk>
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