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

From: Laurenz Albe <laurenz(dot)albe(at)cybertec(dot)at>
To: vectorplanck(at)gmail(dot)com, pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: BUG #19472: CAST(-32768::SMALLINT AS REAL) fails with "SMALLINT out of range" but -32768 is valid SMALLINT value
Date: 2026-05-06 12:53:34
Message-ID: e75bd6c3ef6b975ca5e0ef56d4fa58505e22b8ef.camel@cybertec.at
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: 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

In response to

Browse pgsql-bugs by date

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