pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Daniel Gustafsson <daniel@yesql.se>
Cc: whiteman.kr@gmail.com
Cc: pgsql-bugs@lists.postgresql.org
Subject: Re: BUG #15071: Error in PostgreSQL-specific :: type cast
Date: Fri, 16 Feb 2018 10:29:23 -0500
Message-ID: <14545.1518794963@sss.pgh.pa.us> (raw)
In-Reply-To: <315DAB2D-4D2A-4556-8941-036784005F98@yesql.se>
References: <151878227914.1391.18108972186680794975@wrigleys.postgresql.org>
	<315DAB2D-4D2A-4556-8941-036784005F98@yesql.se>

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




view thread (4+ messages)  latest in thread

Message-ID: <14545.1518794963@sss.pgh.pa.us>
Permalink:  ../14545.1518794963@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/14545.1518794963@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: tgl@sss.pgh.pa.us, daniel@yesql.se, whiteman.kr@gmail.com
  Subject: Re: BUG #15071: Error in PostgreSQL-specific :: type cast
  In-Reply-To: <14545.1518794963@sss.pgh.pa.us>
  References: <151878227914.1391.18108972186680794975@wrigleys.postgresql.org> <315DAB2D-4D2A-4556-8941-036784005F98@yesql.se> <14545.1518794963@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox