Re: FW: [JDBC] BIGINT vs Java's long - Mailing list pgsql-hackers

From Peter Eisentraut
Subject Re: FW: [JDBC] BIGINT vs Java's long
Date
Msg-id Pine.LNX.4.30.0108072119080.871-100000@peter.localdomain
Whole thread Raw
In response to FW: [JDBC] BIGINT vs Java's long  ("Dave Cramer" <dave@fastcrypt.com>)
List pgsql-hackers
Dave Cramer writes:

> Apparently bigint is not really 8 bytes???

It's sort of 7.999 bytes.

> test=# update testbigint set fp0 = -9223372036854775808 where id = 1;
> ERROR:  int8 value out of range: "-9223372036854775808"

This is a bug in the int8 value parser.  While it reads the string it
always accumulates the value as positive and then tags the sign on.
Since +9223372036854775808 doesn't fit you get this error.

ISTM that this can be fixed by accumulating toward the negative end and
taking some special care around the boundaries, like this patch:

Index: int8.c
===================================================================
RCS file: /home/projects/pgsql/cvsroot/pgsql/src/backend/utils/adt/int8.c,v
retrieving revision 1.30
diff -u -r1.30 int8.c
--- int8.c    2001/06/07 00:09:29    1.30
+++ int8.c    2001/08/07 19:26:35
@@ -77,16 +77,21 @@        elog(ERROR, "Bad int8 external representation \"%s\"", str);    while (*ptr &&
isdigit((unsignedchar) *ptr))        /* process digits */    {
 
-        int64        newtmp = tmp * 10 + (*ptr++ - '0');
+        /* We accumulate the value towards the negative end to allow
+           the minimum value to fit it. */
+        int64        newtmp = tmp * 10 - (*ptr++ - '0');

-        if ((newtmp / 10) != tmp)        /* overflow? */
+        /* overflow? */
+        if ((newtmp / 10) != tmp
+            /* This number only fits with a negative sign. */
+            || (newtmp == -9223372036854775808 && sign > 0))            elog(ERROR, "int8 value out of range: \"%s\"",
str);       tmp = newtmp;    }    if (*ptr)                    /* trailing junk? */        elog(ERROR, "Bad int8
externalrepresentation \"%s\"", str);
 

-    result = (sign < 0) ? -tmp : tmp;
+    result = (sign > 0) ? -tmp : tmp;
    PG_RETURN_INT64(result);}
===end

-- 
Peter Eisentraut   peter_e@gmx.net   http://funkturm.homeip.net/~peter



pgsql-hackers by date:

Previous
From: Neil Tiffin
Date:
Subject: Re: Proposal: OID wraparound: summary and proposal
Next
From: Oleg Bartunov
Date:
Subject: Re: To be 7.1.3 or not to be 7.1.3?