Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1emhx8-0003Qb-Ca for pgsql-bugs@arkaria.postgresql.org; Fri, 16 Feb 2018 15:29:34 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1emhx7-0000fQ-1O for pgsql-bugs@arkaria.postgresql.org; Fri, 16 Feb 2018 15:29:33 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1emhx6-0000aS-OJ for pgsql-bugs@lists.postgresql.org; Fri, 16 Feb 2018 15:29:32 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1emhwz-0003wz-J6 for pgsql-bugs@lists.postgresql.org; Fri, 16 Feb 2018 15:29:31 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id w1GFTNr7014546; Fri, 16 Feb 2018 10:29:23 -0500 From: Tom Lane To: Daniel Gustafsson cc: whiteman.kr@gmail.com, pgsql-bugs@lists.postgresql.org Subject: Re: BUG #15071: Error in PostgreSQL-specific :: type cast In-reply-to: <315DAB2D-4D2A-4556-8941-036784005F98@yesql.se> References: <151878227914.1391.18108972186680794975@wrigleys.postgresql.org> <315DAB2D-4D2A-4556-8941-036784005F98@yesql.se> Comments: In-reply-to Daniel Gustafsson message dated "Fri, 16 Feb 2018 14:01:10 +0100" MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-ID: <14544.1518794963.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Fri, 16 Feb 2018 10:29:23 -0500 Message-ID: <14545.1518794963@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Daniel Gustafsson writes: >> On 16 Feb 2018, at 12:57, PG Bug reporting form wrote: >> PostgreSQL-specific :: type casts for lowest values of integer types ar= e >> 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 =E2= =80=9C-=E2=80=9C 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