AW: Type and CAST error on lowest negative integer values for smallint, int and bigint - Mailing list pgsql-hackers

From Hans Buschmann
Subject AW: Type and CAST error on lowest negative integer values for smallint, int and bigint
Date
Msg-id d8ed71ab9e3f4a179bc4c434091570bd@nidsa.net
Whole thread Raw
In response to Re: Type and CAST error on lowest negative integer values for smallint, int and bigint  (David Rowley <dgrowleyml@gmail.com>)
List pgsql-hackers

Thank you for your quick response.


This was very helpfull and resolved my problem.


But for me it is a bit counterintuitive that -32768 is not treated as a negative constant but as a unary operator to a positive constant.


It could be helpfull to remind the user of the nature of negative constants and tthe highest precedence of casts in the numeric type section of the doc.


Perhaps someone could add an index topic "precedence of operators", since this is a very important information for every computer language.

(I just found out that a topic of "operator precedence" exists already in the index)


I just sent this message for the case this could be a problem to be resolved before the next minor versions scheduled for next week.


Regards


Hans Buschmann


Von: David Rowley <dgrowleyml@gmail.com>
Gesendet: Donnerstag, 2. Mai 2024 13:33
An: Hans Buschmann
Cc: pgsql-hackers@postgresql.org
Betreff: Re: Type and CAST error on lowest negative integer values for smallint, int and bigint
 
On Thu, 2 May 2024 at 23:25, Hans Buschmann <buschmann@nidsa.net> wrote:
> postgres=# select -32768::smallint;
> ERROR:  smallint out of range

The precedence order of operations applies the cast before the unary
minus operator.

Any of the following will work:

postgres=# select cast(-32768 as smallint), (-32768)::smallint,
'-32768'::smallint;
  int2  |  int2  |  int2
--------+--------+--------
 -32768 | -32768 | -32768
(1 row)

David

pgsql-hackers by date:

Previous
From: Alexander Lakhin
Date:
Subject: Re: Typos in the code and README
Next
From: Richard Guo
Date:
Subject: Re: Removing unneeded self joins