Re: BUG #15071: Error in PostgreSQL-specific :: type cast

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Daniel Gustafsson <daniel(at)yesql(dot)se>
Cc: whiteman(dot)kr(at)gmail(dot)com, pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: BUG #15071: Error in PostgreSQL-specific :: type cast
Date: 2018-02-16 15:29:23
Message-ID: 14545.1518794963@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Daniel Gustafsson <daniel(at)yesql(dot)se> writes:
>> On 16 Feb 2018, at 12:57, PG Bug reporting form <noreply(at)postgresql(dot)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

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Francisco Olarte 2018-02-16 15:31:44 Re: BUG #15071: Error in PostgreSQL-specific :: type cast
Previous Message Daniel Gustafsson 2018-02-16 13:01:10 Re: BUG #15071: Error in PostgreSQL-specific :: type cast