agora inbox for pgsql-sql@postgresql.org  
help / color / mirror / Atom feed
PostgreSQL 9.5 operator precedence
3+ messages / 3 participants
[nested] [flat]

* PostgreSQL 9.5 operator precedence
@ 2016-09-20 06:02  =?koi8-r?B?4c7E0sXKIOHXwcvJzc/X?= <aquarius1993@rambler.ru>
  0 siblings, 1 reply; 3+ messages in thread

From: Андрей Авакимов @ 2016-09-20 06:02 UTC (permalink / raw)
  To: pgsql-sql

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.

For me it means that postgres calculated value in the left side of the =
operator, then it decided not to calculate right side and compare boolean and
integer.
Why did it not compare null and 2 firstly?
Maybe the truth is IS operator and = operator has the same precedence, and then
calculated from left to right?
Maybe I miss something?

Best Regards,
Andrew

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

* Re: PostgreSQL 9.5 operator precedence
@ 2016-09-20 14:10  David G. Johnston <david.g.johnston@gmail.com>
  parent: =?koi8-r?B?4c7E0sXKIOHXwcvJzc/X?= <aquarius1993@rambler.ru>
  0 siblings, 1 reply; 3+ messages in thread

From: David G. Johnston @ 2016-09-20 14:10 UTC (permalink / raw)
  To: Андрей Авакимов <aquarius1993@rambler.ru>; +Cc: pgsql-sql

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.
​

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

* Re: PostgreSQL 9.5 operator precedence
@ 2016-09-20 14:47  Tom Lane <tgl@sss.pgh.pa.us>
  parent: David G. Johnston <david.g.johnston@gmail.com>
  0 siblings, 0 replies; 3+ messages in thread

From: Tom Lane @ 2016-09-20 14:47 UTC (permalink / raw)
  To: David G. Johnston <david.g.johnston@gmail.com>; +Cc: Андрей Авакимов <aquarius1993@rambler.ru>; pgsql-sql

"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


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



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


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

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2016-09-20 06:02 PostgreSQL 9.5 operator precedence =?koi8-r?B?4c7E0sXKIOHXwcvJzc/X?= <aquarius1993@rambler.ru>
2016-09-20 14:10 ` David G. Johnston <david.g.johnston@gmail.com>
2016-09-20 14:47   ` Tom Lane <tgl@sss.pgh.pa.us>

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