Daniel Gustafsson <daniel@yesql.se> writes:
>> On 16 Feb 2018, at 12:57, PG Bug reporting form <noreply@postgresql.org> wrote:
>> PostgreSQL-specific :: type casts for lowest values of integer types are
>> produce "Out of range" errors:
>>
>> SELECT -32768::smallint
>> ^ERROR: smallint out of range
> The parser will represents this as the numeric portion, 32768, and a “-“ in an
> expression of type AEXPR_OP.
Right. The point is that :: binds more tightly than unary minus[1],
so this is read as "-(32768::smallint)", and the out-of-range failure
is entirely appropriate. You can avoid the problem with
(-32768)::smallint, or as you mentioned with '-32768'::smallint.
While we hear complaints about this regularly, I do not think that
changing the precedence order would be a good idea. It's at least
conceivable that the behavior of the unary-minus operator would be
different for different datatypes, so enforcing the datatype selection
before applying the operator seems like the right decision from an
abstract-data-type standpoint. (This argument explains why :: has
such a high precedence to begin with.) You can even construct cases,
admittedly lame, where changing the precedence would result in failures
where there had been none before.
regards, tom lane
[1] https://www.postgresql.org/docs/current/static/sql-syntax-lexical.html#SQL-PRECEDENCE