Bug with int2

Started by Feng Tianalmost 10 years ago5 messages
#1Feng Tian
ftian@vitessedata.com

I run into the following. Seems this is a bug for -32768, which should be
a valid smallint value.

Test was run on 9.4.5.

Thanks,
Feng

ftian=# select 32767::int2;

int2
-------
32767
(1 row)

ftian=# select -32767::int2;
?column?
----------
-32767
(1 row)

ftian=# select 32768::int2;

ERROR: smallint out of range
ftian=# select -32768::int2;
ERROR: smallint out of range
ftian=#

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Feng Tian (#1)
Re: Bug with int2

Feng Tian <ftian@vitessedata.com> writes:

I run into the following. Seems this is a bug for -32768, which should be
a valid smallint value.

ftian=# select -32768::int2;
ERROR: smallint out of range

You have the wrong idea about the precedence of those operators.
"select (-32768)::int2" works.

regards, tom lane

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#3Michael Paquier
michael.paquier@gmail.com
In reply to: Feng Tian (#1)
Re: Bug with int2

On Wed, Feb 17, 2016 at 12:27 PM, Feng Tian <ftian@vitessedata.com> wrote:

ftian=# select -32768::int2;
ERROR: smallint out of range

But 32768 is not. You should just use parenthesis, a cast does not
take into account the minus sign here:
=# select (-32768)::int2;
int2
--------
-32768
(1 row)
--
Michael

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#4Peter Geoghegan
pg@heroku.com
In reply to: Feng Tian (#1)
Re: Bug with int2

On Tue, Feb 16, 2016 at 7:27 PM, Feng Tian <ftian@vitessedata.com> wrote:

I run into the following. Seems this is a bug for -32768, which should be
a valid smallint value.

This isn't a bug. You see the error only due to operator precedence:

postgres=# select (-32768)::int2;
int2
─────────
-32,768
(1 row)

--
Peter Geoghegan

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#5Feng Tian
ftian@vitessedata.com
In reply to: Peter Geoghegan (#4)
Re: Bug with int2

Ah, thanks!

On Tue, Feb 16, 2016 at 7:54 PM, Peter Geoghegan <pg@heroku.com> wrote:

Show quoted text

On Tue, Feb 16, 2016 at 7:27 PM, Feng Tian <ftian@vitessedata.com> wrote:

I run into the following. Seems this is a bug for -32768, which should

be

a valid smallint value.

This isn't a bug. You see the error only due to operator precedence:

postgres=# select (-32768)::int2;
int2
─────────
-32,768
(1 row)

--
Peter Geoghegan