Thread: PostgreSQL 9.5 operator precedence

PostgreSQL 9.5 operator precedence

From
Андрей Авакимов
Date:
<p><span style="font-family: 'Courier New','Courier';">Hello, </span><br /><span style="font-family: 'Courier
New','Courier';">I'vegot a question about operator precedence is version 9.5</span><br /><br /><span
style="font-family:'Courier New','Courier';">Versions lower than 9.5 worked well with constructions like</span><br
/><spanstyle="font-family: 'Courier New','Courier';">    select a is null = b is null</span><br /><span
style="font-family:'Courier New','Courier';">(it calculates if a is null, then if b is null and then compare two
booleans)</span><br/><br /><span style="font-family: 'Courier New','Courier';">But now everything is changed, and
official9.5 documentations says that </span><br /><span style="font-family: 'Courier New','Courier';">IS operator has
lowerprecedence than = operator</span><br /><br /><span style="font-family: 'Courier New','Courier';">The thing I don't
understandis the error message that I receive:</span><br /><br /><span style="font-family: 'Courier New','Courier';">  
 select1 is null = 2 is null;</span><br /><span style="font-family: 'Courier New','Courier';">  
 -----------------------------</span><br/><span style="font-family: 'Courier New','Courier';">    ERROR:  operator does
notexist: boolean = integer</span><br /><span style="font-family: 'Courier New','Courier';">    LINE 1: select 1 is
null= 2 is null</span><br /><span style="font-family: 'Courier New','Courier';">                            
^</span><br/><span style="font-family: 'Courier New','Courier';">    HINT:  No operator matches the given name and
argumenttype(s). You might need to add explicit type casts.</span><br /><br /><span style="font-family: 'Courier
New','Courier';">Forme it means that postgres calculated value in the left side of the = operator, then it decided not
tocalculate right side and compare boolean and integer.</span><br /><span style="font-family: 'Courier
New','Courier';">Whydid it not compare null and 2 firstly? </span><br /><span style="font-family: 'Courier
New','Courier';">Maybethe truth is IS operator and = operator has the same precedence, and then calculated from left to
right?</span><br/><span style="font-family: 'Courier New','Courier';">Maybe I miss something?</span><br /><br /><span
style="font-family:'Courier New','Courier';">Best Regards,</span><br /><span style="font-family: 'Courier
New','Courier';">Andrew</span><br/><br /><br /><br /><br /><br /> 

Re: PostgreSQL 9.5 operator precedence

From
"David G. Johnston"
Date:
On Mon, Sep 19, 2016 at 11:02 PM, Андрей Авакимов <aquarius1993@rambler.ru> wrote:

Hello,
I've got a question about operator precedence is version 9.5

Versions lower than 9.5 worked well with constructions like
    select a is null = b is null
(it calculates if a is null, then if b is null and then compare two booleans)

But now everything is changed, and official 9.5 documentations says that
IS operator has lower precedence than = operator

The thing I don't understand is the error message that I receive:

    select 1 is null = 2 is null;
    -----------------------------
    ERROR:  operator does not exist: boolean = integer
    LINE 1: select 1 is null = 2 is null
                             ^
    HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.

​Your query reads:

SELECT ( ( 1 IS (NULL = 2) ) IS NULL​
 
​)

So the error is in:

1 IS (NULL = 2)

which indeed is integer = boolean

David J.

Re: PostgreSQL 9.5 operator precedence

From
Tom Lane
Date:
"David G. Johnston" <david.g.johnston@gmail.com> writes:
> On Mon, Sep 19, 2016 at 11:02 PM, Андрей Авакимов <aquarius1993@rambler.ru>
>> The thing I don't understand is the error message that I receive:
>> select 1 is null = 2 is null;
>> -----------------------------
>> ERROR:  operator does not exist: boolean = integer
>> LINE 1: select 1 is null = 2 is null

> ​Your query reads:
> SELECT ( ( 1 IS (NULL = 2) ) IS NULL​

No, certainly not that --- IS isn't some sort of standalone operator,
rather IS NULL is an indivisible combination of tokens representing a
postfix operator.  The query's really getting parsed like this:

select ((1 is null) = 2) is null;

whereas the pre-9.5 interpretation was

select (1 is null) = (2 is null);

If you add those parentheses explicitly then your query will work fine
in either version.
        regards, tom lane