Обсуждение: Type and CAST error on lowest negative integer values for smallint, int and bigint
Type and CAST error on lowest negative integer values for smallint, int and bigint
I tried to initialize a table with values for smallint columns.
The final goal is to get mask values for logical operations.
The insert failed with ERROR: smallint out of range.
Hans Buschmann
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
AW: Type and CAST error on lowest negative integer values for smallint, int and bigint
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
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
> 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