Re: BUG #19472: CAST(-32768::SMALLINT AS REAL) fails with "SMALLINT out of range" but -32768 is valid SMALLINT value - Mailing list pgsql-bugs

From Laurenz Albe
Subject Re: BUG #19472: CAST(-32768::SMALLINT AS REAL) fails with "SMALLINT out of range" but -32768 is valid SMALLINT value
Date
Msg-id e75bd6c3ef6b975ca5e0ef56d4fa58505e22b8ef.camel@cybertec.at
Whole thread
In response to BUG #19472: CAST(-32768::SMALLINT AS REAL) fails with "SMALLINT out of range" but -32768 is valid SMALLINT value  (PG Bug reporting form <noreply@postgresql.org>)
List pgsql-bugs
On Wed, 2026-05-06 at 09:09 +0000, PG Bug reporting form wrote:
> When casting -32768 to SMALLINT and then to REAL using the :: operator,
> PostgreSQL throws an error "smallint out of range", even though -32768 is a
> valid value within the SMALLINT range (-32768 to +32767).
>
> Expected Behavior:
> -32768 should be recognized as a valid SMALLINT value (the minimum value for
> smallint is -32768), and the cast to REAL should succeed.

This is not a bug, but working as expected.
The documentation at https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-PRECEDENCE
clearly states that the typecast operator :: has higher precedence than
an unary minus.

Yours,
Laurenz Albe



pgsql-bugs by date:

Previous
From: Alex Masterov
Date:
Subject: Re: BUG #18354: Aborted transaction aborted during cleanup when temp_file_limit exceeded
Next
From: "David G. Johnston"
Date:
Subject: Re: BUG #19472: CAST(-32768::SMALLINT AS REAL) fails with "SMALLINT out of range" but -32768 is valid SMALLINT value