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

BUG #3822: Nonstandard precedence for comparison operators

From: "Pedro Gimeno" <pgsql-001(at)personal(dot)formauri(dot)es>
To: pgsql-bugs(at)postgresql(dot)org
Subject: BUG #3822: Nonstandard precedence for comparison operators
Date: 2007-12-17 19:58:24
Message-ID: 200712171958.lBHJwOBb037317@wwwmaster.postgresql.org (view raw or flat)
Thread:
Lists: pgsql-bugs
The following bug has been logged online:

Bug reference:      3822
Logged by:          Pedro Gimeno
Email address:      pgsql-001(at)personal(dot)formauri(dot)es
PostgreSQL version: 8.2.5
Operating system:   Any
Description:        Nonstandard precedence for comparison operators
Details: 

The operators <>, <= and >= are expected to have the same precedence as =, <
and >, but according to the docs (and matching actual behaviour) they have
the same precedence as e.g. ||.

This leads to surprises such as this one:

SELECT 'a' = 'b' || 'c';
SELECT 'a' < 'b' || 'c';
SELECT 'a' > 'b' || 'c';

return either FALSE or TRUE, but

SELECT 'a' <> 'b' || 'c';
SELECT 'a' <= 'b' || 'c';
SELECT 'a' >= 'b' || 'c';

give an error because they are equivalent to:

SELECT ('a' <> 'b') || 'c';
SELECT ('a' <= 'b') || 'c';
SELECT ('a' >= 'b') || 'c';

respectively, which try to concatenate a boolean value with 'c'. On the
other hand, the following work as expected:

SELECT 'b' || 'c' <> 'a';
SELECT 'b' || 'c' >= 'a';
SELECT 'b' || 'c' <= 'a';

That's because, having || and e.g. <> the same priority, they are evaluated
left-to-right.

Of course the same applies to != since it is converted to <>.

Now being picky, the precedence of < and > should be the same as that of =
(for comparison, not for assignment), which makes a difference in rare cases
like this:

SELECT false = false < false;

which in PostgreSQL returns true instead of false because it is equivalent
to this:

SELECT false = (false < false);

-- Pedro Gimeno

Responses

pgsql-bugs by date

Next:From: Dimitri FontaineDate: 2007-12-17 20:03:02
Subject: Re: bug found in 8.3 beta 3.
Previous:From: Tom LaneDate: 2007-12-17 18:19:13
Subject: Re: bug found in 8.3 beta 3.

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