From: Greg Stark <stark@mit.edu>
To: Magnus Hagander <magnus@hagander.net>
Cc: binoternary@gmail.com, pgsql-bugs <pgsql-bugs@postgresql.org>
Subject: Re: BUG #14986: -2147483648 is minimum value of integer but -2147483648::integer fails (out of range).
Date: Thu, 21 Dec 2017 14:05:07 +0000
Message-ID: <CAM-w4HNudmE0OReuV7YCAkO2=0rgxNGC_43G-R8oKXRnJTUNKw@mail.gmail.com> (raw)
In-Reply-To: <CABUevEz7CgCgz8MVkp89-UVb=GrNfvv3-XRw=dJGJKnq=WV9TQ@mail.gmail.com>
References: <20171220122032.25740.1219@wrigleys.postgresql.org>
<CABUevEz7CgCgz8MVkp89-UVb=GrNfvv3-XRw=dJGJKnq=WV9TQ@mail.gmail.com>
On 20 December 2017 at 12:25, Magnus Hagander <magnus@hagander.net> wrote:
> In the first query, you are casting 2147483648 to integer, and then
> applying the minus. So it overflows the positive integer. You need () around
> it:
>
> postgres=# select (-2147483648)::integer;
> int4
> -------------
> -2147483648
> (1 row)
Note that -2147483648 is already an integer constant so the ::integer
doesn't actually do anything here. It gets removed during query
preparation (parse analysis? Not sure). It doesn't appear in the final
plan at all.
But even if you do arrange for the cast to be called (as it is in the
original query) it happens during constant folding preparing the plan
(again parse analysis?):
=# explain select '-2147483648'::bigint::integer;
LOG: 00000: plan:
DETAIL: {PLANNEDSTMT
...
:targetlist (
{TARGETENTRY
:expr
{CONST
:consttype 23
You can see this by running EXPLAIN on the original query. It
overflows even though the query is never run:
=# explain select -2147483648::integer;
ERROR: 22003: integer out of range
LOCATION: int84, int8.c:1298
Time: 0.393 ms
The LOCATION line even gives a hint what's going on. 2147483648 was
read as an int8 constant and the - operator returned an int8 and then
the cast (being run during explain as part of preparing the plan)
overflowed.
I wonder why the "out of range" error doesn't print the actual value
it's trying to cast. That would help the user here...
--
greg
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: stark@mit.edu, magnus@hagander.net
Subject: Re: BUG #14986: -2147483648 is minimum value of integer but -2147483648::integer fails (out of range).
In-Reply-To: <CAM-w4HNudmE0OReuV7YCAkO2=0rgxNGC_43G-R8oKXRnJTUNKw@mail.gmail.com>
References: <20171220122032.25740.1219@wrigleys.postgresql.org> <CABUevEz7CgCgz8MVkp89-UVb=GrNfvv3-XRw=dJGJKnq=WV9TQ@mail.gmail.com> <CAM-w4HNudmE0OReuV7YCAkO2=0rgxNGC_43G-R8oKXRnJTUNKw@mail.gmail.com>
* 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