Cast has higher precedence than -

Started by Jim Nasbyover 10 years ago2 messages
#1Jim Nasby
Jim.Nasby@BlueTreble.com

Given this domain...
CREATE DOMAIN loan.loan_amount numeric(7,2) --loan.loan_amount_raw
CONSTRAINT loan_amount__greater_equal_0 CHECK( VALUE >= 0 )
CONSTRAINT loan_amount__less_equal_20000 CHECK( VALUE <= 20000 )
;

I was rather surprised by

select -1::loan.loan_amount;
?column?
----------
-1.00
(1 row)

The problem is that :: binds more tightly than -:

select (-1)::loan.loan_amount;
ERROR: value for domain loan.loan_amount violates check constraint
"loan_amount__greater_equal_0"

Is this by design? If not, any ideas how bad it'd be to fix?
--
Jim Nasby, Data Architect, Blue Treble Consulting
Data in Trouble? Get it in Treble! http://BlueTreble.com

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

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Jim Nasby (#1)
Re: Cast has higher precedence than -

Jim Nasby <Jim.Nasby@BlueTreble.com> writes:

The problem is that :: binds more tightly than -

This is well known, and even well documented.

Is this by design? If not, any ideas how bad it'd be to fix?

It is by design. The core argument for doing it is that '-' might have
type-dependent semantics that would not be reproduced by negating first
and casting afterwards.

Even if you could convince people that that concern is baseless, there
would be a backwards-compatibility problem. For a comparison point,
note the hurdles I had to jump to persuade people that we should change
the precedence of comparison operators --- something that has clear
support in the standard, as this change would not.

regards, tom lane

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