pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
BUG #16247: Cast error on integer
3+ messages / 3 participants
[nested] [flat]

* BUG #16247: Cast error on integer
@ 2020-02-06 21:43  PG Bug reporting form <noreply@postgresql.org>
  0 siblings, 2 replies; 3+ messages in thread

From: PG Bug reporting form @ 2020-02-06 21:43 UTC (permalink / raw)
  To: pgsql-bugs@lists.postgresql.org; +Cc: ddtripathy@yahoo.com

The following bug has been logged on the website:

Bug reference:      16247
Logged by:          Dilip Tripathy
Email address:      ddtripathy@yahoo.com
PostgreSQL version: 12.1
Operating system:   Windows 7 professional
Description:        

I have defined a procedure with the following signature:

create or replace procedure eventattributes_getadd(
   _cust varchar(128),
   _site varchar(128),
   _devctrluid int,
   _dateid int,
   _fiveminprdid smallint,
   _metric varchar(128),
   _sd varchar(256),
   _value varchar(32))
language 'plpgsql'


When I execute the following call it succeeds.
   call eventattributes_getadd(
    _cust => 'cust'::varchar(128),
    _site => 'site'::varchar(128),
    _devctrluid => cast (-2147483648 as integer),
    _dateid => 20200206::int,
    _fiveminprdid => 1700::smallint,
    _metric => 'somebogusmetric'::varchar(128),
    _sd =>
'>>>ST<<<=mc4;>>>DLN<<<=scanner_1;>>>DG<<<=scanner_1;>>>DGT<<<=scanner;>>>DT<<<=scanner'::varchar(256),
    _value => '28'::varchar(32));

However, when I execute the following call I get: ERROR:  integer out of
range  SQL state: 22003 
   call eventattributes_getadd(
    _cust => 'cust'::varchar(128),
    _site => 'site'::varchar(128),
    _devctrluid => -2147483648::int,
    _dateid => 20200206::int,
    _fiveminprdid => 1700::smallint,
    _metric => 'somebogusmetric'::varchar(128),
    _sd =>
'>>>ST<<<=mc4;>>>DLN<<<=scanner_1;>>>DG<<<=scanner_1;>>>DGT<<<=scanner;>>>DT<<<=scanner'::varchar(256),
    _value => '28'::varchar(32));

The only difference between the two calls is how I'm casting the integer. 
The second call is choking on "_devctrluid => -2147483648::int".  But, if I
change that value to "-2147483647"  the second call succeeds.



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: BUG #16247: Cast error on integer
@ 2020-02-06 21:53  David G. Johnston <david.g.johnston@gmail.com>
  parent: PG Bug reporting form <noreply@postgresql.org>
  1 sibling, 0 replies; 3+ messages in thread

From: David G. Johnston @ 2020-02-06 21:53 UTC (permalink / raw)
  To: ddtripathy@yahoo.com, PostgreSQL mailing lists <pgsql-bugs@lists.postgresql.org>

On Thu, Feb 6, 2020 at 2:44 PM PG Bug reporting form <noreply@postgresql.org>
wrote:

> The only difference between the two calls is how I'm casting the integer.
>

Not a bug - operator precedence:

https://www.postgresql.org/docs/12/sql-syntax-lexical.html#SQL-PRECEDENCE

David J.

^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: BUG #16247: Cast error on integer
@ 2020-02-06 22:12  Andres Freund <andres@anarazel.de>
  parent: PG Bug reporting form <noreply@postgresql.org>
  1 sibling, 0 replies; 3+ messages in thread

From: Andres Freund @ 2020-02-06 22:12 UTC (permalink / raw)
  To: ddtripathy@yahoo.com; pgsql-bugs@lists.postgresql.org

Hi,

On 2020-02-06 21:43:41 +0000, PG Bug reporting form wrote:
> The only difference between the two calls is how I'm casting the integer. 
> The second call is choking on "_devctrluid => -2147483648::int".  But, if I
> change that value to "-2147483647"  the second call succeeds.

I don't think that's a bug. -2147483648::int is parsed as
-((2147483648)::int). 2147483648 gets parsed as an int8 (due to its
width), but then you're casting the result to an int4. And 2147483648 is
not representable as a signed 32bit integer.  It fails before getting to
negating the result of the cast.

For precedence see:
https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-PRECEDENCE-TABLE

Whereas '-2147483648'::int4 gets read directly as int4, including the
sign. And wheras 2147483648 is not representable as an int4, -2147483648
is.

Greetings,

Andres Freund





^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2020-02-06 22:12 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2020-02-06 21:43 BUG #16247: Cast error on integer PG Bug reporting form <noreply@postgresql.org>
2020-02-06 21:53 ` David G. Johnston <david.g.johnston@gmail.com>
2020-02-06 22:12 ` Andres Freund <andres@anarazel.de>

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