Обсуждение: Bug with int2

Поиск
Список
Период
Сортировка

Bug with int2

От
Feng Tian
Дата:
<div dir="ltr"><div style="font-size:12.8px">I run into the following.   Seems this is a bug for -32768, which should
bea valid smallint value.</div><div style="font-size:12.8px"><br /></div><div style="font-size:12.8px">Test was run on
9.4.5.</div><divstyle="font-size:12.8px"><br /></div><div style="font-size:12.8px">Thanks,</div><div
style="font-size:12.8px">Feng</div><divstyle="font-size:12.8px"><br /></div><div style="font-size:12.8px">ftian=#
select32767::int2;                                                                                                    
                                                             </div><div style="font-size:12.8px"> int2  </div><div
style="font-size:12.8px">-------</div><divstyle="font-size:12.8px"> 32767</div><div style="font-size:12.8px">(1
row)</div><divstyle="font-size:12.8px"><br /></div><div style="font-size:12.8px">ftian=# select -32767::int2;</div><div
style="font-size:12.8px"> ?column? </div><divstyle="font-size:12.8px">----------</div><div style="font-size:12.8px"> 
 -32767</div><divstyle="font-size:12.8px">(1 row)</div><div style="font-size:12.8px"><br /></div><div
style="font-size:12.8px">ftian=#select 32768::int2;                                                                    
                                                                                             </div><div
style="font-size:12.8px">ERROR: smallint out of range</div><div style="font-size:12.8px">ftian=# select
-32768::int2;</div><divstyle="font-size:12.8px">ERROR:  smallint out of range</div><div
style="font-size:12.8px">ftian=# </div></div>

Re: Bug with int2

От
Tom Lane
Дата:
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



Re: Bug with int2

От
Michael Paquier
Дата:
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



Re: Bug with int2

От
Peter Geoghegan
Дата:
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



Re: Bug with int2

От
Feng Tian
Дата:
<div dir="ltr">Ah, thanks!</div><div class="gmail_extra"><br /><div class="gmail_quote">On Tue, Feb 16, 2016 at 7:54
PM,Peter Geoghegan <span dir="ltr"><<a href="mailto:pg@heroku.com" target="_blank">pg@heroku.com</a>></span>
wrote:<br/><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span
class="">OnTue, Feb 16, 2016 at 7:27 PM, Feng Tian <<a
href="mailto:ftian@vitessedata.com">ftian@vitessedata.com</a>>wrote:<br /> > I run into the following.   Seems
thisis a bug for -32768, which should be<br /> > a valid smallint value.<br /><br /></span>This isn't a bug. You see
theerror only due to operator precedence:<br /><br /> postgres=# select (-32768)::int2;<br />   int2<br /> ─────────<br
/> -32,768<br /> (1 row)<br /><span class="HOEnZb"><font color="#888888"><br /> --<br /> Peter Geoghegan<br
/></font></span></blockquote></div><br/></div>