AW: Type and CAST error on lowest negative integer values for smallint, int and bigint

From: Hans Buschmann <buschmann(at)nidsa(dot)net>
To: David Rowley <dgrowleyml(at)gmail(dot)com>
Cc: "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: AW: Type and CAST error on lowest negative integer values for smallint, int and bigint
Date: 2024-05-02 12:14:08
Message-ID: d8ed71ab9e3f4a179bc4c434091570bd@nidsa.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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(at)gmail(dot)com>
Gesendet: Donnerstag, 2. Mai 2024 13:33
An: Hans Buschmann
Cc: pgsql-hackers(at)postgresql(dot)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(at)nidsa(dot)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

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Richard Guo 2024-05-02 13:14:20 Re: Removing unneeded self joins
Previous Message Alexander Lakhin 2024-05-02 12:00:00 Re: Typos in the code and README