Re: BUG #4436: (E'\\' LIKE E'\\') => f

Lists: pgsql-bugs
From: "Mathieu Fenniak" <hjoiiv(at)mathieu(dot)fenniak(dot)net>
To: pgsql-bugs(at)postgresql(dot)org
Subject: BUG #4436: (E'\\' LIKE E'\\') => f
Date: 2008-09-24 16:00:52
Message-ID: 200809241600.m8OG0q3Z095759@wwwmaster.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs


The following bug has been logged online:

Bug reference: 4436
Logged by: Mathieu Fenniak
Email address: hjoiiv(at)mathieu(dot)fenniak(dot)net
PostgreSQL version: 8.3.3
Operating system: Linux x86-64
Description: (E'\\' LIKE E'\\') => f
Details:

I noticed that (SELECT E'\\' LIKE E'\\') returns false, where I would expect
it to return true. I asked on the #postgresql/freenode IRC channel, and
nobody had a good explanation for this return value, suggesting it may be a
minor bug.


From: Bruce Momjian <bruce(at)momjian(dot)us>
To: Mathieu Fenniak <hjoiiv(at)mathieu(dot)fenniak(dot)net>
Cc: pgsql-bugs(at)postgresql(dot)org
Subject: Re: BUG #4436: (E'\\' LIKE E'\\') => f
Date: 2008-09-24 16:07:06
Message-ID: 200809241607.m8OG76w23363@momjian.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs

Mathieu Fenniak wrote:
>
> The following bug has been logged online:
>
> Bug reference: 4436
> Logged by: Mathieu Fenniak
> Email address: hjoiiv(at)mathieu(dot)fenniak(dot)net
> PostgreSQL version: 8.3.3
> Operating system: Linux x86-64
> Description: (E'\\' LIKE E'\\') => f
> Details:
>
> I noticed that (SELECT E'\\' LIKE E'\\') returns false, where I would expect
> it to return true. I asked on the #postgresql/freenode IRC channel, and
> nobody had a good explanation for this return value, suggesting it may be a
> minor bug.

I believe this is caused because backslash is the default escape
character for LIKE, so you need:

test=> SELECT E'\\' LIKE E'\\\\';
?column?
----------
t
(1 row)

or change the escape character:

test=> SELECT E'\\' LIKE E'\\' escape 'a';
?column?
----------
t
(1 row)

--
Bruce Momjian <bruce(at)momjian(dot)us> http://momjian.us
EnterpriseDB http://enterprisedb.com

+ If your life is a hard drive, Christ can be your backup. +


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Bruce Momjian <bruce(at)momjian(dot)us>
Cc: Mathieu Fenniak <hjoiiv(at)mathieu(dot)fenniak(dot)net>, pgsql-bugs(at)postgresql(dot)org
Subject: Re: BUG #4436: (E'\\' LIKE E'\\') => f
Date: 2008-09-24 20:00:54
Message-ID: 10530.1222286454@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs

Bruce Momjian <bruce(at)momjian(dot)us> writes:
> Mathieu Fenniak wrote:
>> I noticed that (SELECT E'\\' LIKE E'\\') returns false,

> I believe this is caused because backslash is the default escape
> character for LIKE, so you need:
> test=> SELECT E'\\' LIKE E'\\\\';

Yeah. The given case is actually an invalid LIKE pattern. I wonder
whether we should make LIKE throw error for an invalid pattern.
You get an error for the corresponding case in regex:

regression=# select E'\\' ~ E'\\';
ERROR: invalid regular expression: invalid escape \ sequence

but IIRC the LIKE code just silently ignores a trailing escape
character.

regards, tom lane


From: Bruce Momjian <bruce(at)momjian(dot)us>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Mathieu Fenniak <hjoiiv(at)mathieu(dot)fenniak(dot)net>, pgsql-bugs(at)postgresql(dot)org
Subject: Re: BUG #4436: (E'\\' LIKE E'\\') => f
Date: 2008-09-25 20:57:52
Message-ID: 200809252057.m8PKvr521890@momjian.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs

Tom Lane wrote:
> Bruce Momjian <bruce(at)momjian(dot)us> writes:
> > Mathieu Fenniak wrote:
> >> I noticed that (SELECT E'\\' LIKE E'\\') returns false,
>
> > I believe this is caused because backslash is the default escape
> > character for LIKE, so you need:
> > test=> SELECT E'\\' LIKE E'\\\\';
>
> Yeah. The given case is actually an invalid LIKE pattern. I wonder
> whether we should make LIKE throw error for an invalid pattern.
> You get an error for the corresponding case in regex:
>
> regression=# select E'\\' ~ E'\\';
> ERROR: invalid regular expression: invalid escape \ sequence
>
> but IIRC the LIKE code just silently ignores a trailing escape
> character.

Yes, I think we should throw an error; the original query looked odd to
me too.

--
Bruce Momjian <bruce(at)momjian(dot)us> http://momjian.us
EnterpriseDB http://enterprisedb.com

+ If your life is a hard drive, Christ can be your backup. +


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Bruce Momjian <bruce(at)momjian(dot)us>
Cc: Mathieu Fenniak <hjoiiv(at)mathieu(dot)fenniak(dot)net>, pgsql-bugs(at)postgresql(dot)org
Subject: Re: BUG #4436: (E'\\' LIKE E'\\') => f
Date: 2008-09-26 01:54:31
Message-ID: 4016.1222394071@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs

Bruce Momjian <bruce(at)momjian(dot)us> writes:
> Tom Lane wrote:
>> Yeah. The given case is actually an invalid LIKE pattern. I wonder
>> whether we should make LIKE throw error for an invalid pattern.

> Yes, I think we should throw an error; the original query looked odd to
> me too.

A quick check in the standard supports the idea of throwing an error.
In fact, SQL92 saith

ii) If there is not a partitioning of the string P into sub-
strings such that each substring has length 1 or 2, no
substring of length 1 is the escape character E, and each
substring of length 2 is the escape character E followed by
either the escape character E, an <underscore> character,
or the <percent> character, then an exception condition is
raised: data exception-invalid escape sequence.

which not only requires E to not be the last character, but says that
it's a bug to escape anything but % _ or the escape character. That
last part is too anal for me, but it does seem we're on safe ground to
throw error for escape with nothing to escape. I'll go make it so.

regards, tom lane