Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

improve support for int arrays and float arrays #501

Merged
merged 7 commits into from

3 participants

@lalitkapoor

addresses #452

@brianc
Owner

Cool! Thanks! This actually changes the return type from big integers from their old, incorrect type of JavaScript ints into a new, correct type of JavaScript strings, right? Since int64 can technically overflow? I wanna make sure I bump the major version if it is indeed a non-backwards compatible change. I think there are a few more weirdnesses in #452 I'll try to address as well before I bump the major version component.

@lalitkapoor

Yes that is what it does. I agree too, it's a breaking change requiring a bump in the major. Just some more details:

If a user has been selecting int8 values within the number range that javascript supports they wouldn't have been experiencing any problems. Shifting parsing int8s over to strings as the default makes sense, but would be a breaking change for anyone selecting int8 values and working with them as numbers in their code.

Currently we parse int8 datatypes as javascript numbers. int8 can go up to (2^63)-1 (9223372036854775807) but javascript's numbers can only go up to 2^53 (9007199254740992) without issues. Any int8 value above what javascript supports may not be the correct value due to an overflow.

References
http://en.wikipedia.org/wiki/Integer_(computer_science)
http://stackoverflow.com/questions/307179/what-is-javascripts-max-int-whats-the-highest-integer-value-a-number-can-go-t

@brianc
Owner

Just as a note to myself - maybe I'll try to get a patch for this in before bumping the major version too?

#508

@brianc
Owner

@lalitkapoor thanks for this so much! :+1: sorry it took so long for me to merge it - needed to have some time & the mental fortitude to commit to doing a little refactoring in prep for pg@3.x.

:heart:

@brianc brianc merged commit ff8fb61 into brianc:master

1 check passed

Details default The Travis CI build passed
@lalitkapoor lalitkapoor deleted the lalitkapoor:GH-452 branch
@brianc brianc added this to the v3.0 milestone
@mariusa

Hello,

What's the recommended way to handle bigint postgres type?

Right now pg.defaults.parseInt8 is inconsistent:

CREATE TABLE testbigints(
    x bigint,
    a bigint[] DEFAULT array[]::bigint[]
);

When pg.defaults.parseInt8 = true:

  • for bigint column types, typeof x == "number" (correct)
  • for bigint[] column types, the values resulting in nodejs still have typeof a[0] == "string" instead of "number".

Looks like both should be strings by default, and both should be numbers when pg.defaults.parseInt8 = true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 8, 2014
  1. @lalitkapoor

    big integer array support

    lalitkapoor authored
  2. @lalitkapoor

    parse values in float arrays as floats not strings

    lalitkapoor authored
    javascript numbers are 64-bit floating numbers
  3. @lalitkapoor

    test array/int2

    lalitkapoor authored
  4. @lalitkapoor

    test array/int4

    lalitkapoor authored
  5. @lalitkapoor

    test array/int8

    lalitkapoor authored
  6. @lalitkapoor

    test array/float4

    lalitkapoor authored
  7. @lalitkapoor

    test array/float8

    lalitkapoor authored
This page is out of date. Refresh to see the latest.
View
17 lib/types/textParsers.js
@@ -89,9 +89,24 @@ var parseIntegerArray = function(val) {
return p.parse();
};
+var parseBigIntegerArray = function(val) {
+ if(!val) { return null; }
+ var p = arrayParser.create(val, function(entry){
+ if(entry !== null) {
+ entry = parseBigInteger(entry).trim();
+ }
+ return entry;
+ });
+
+ return p.parse();
+};
+
var parseFloatArray = function(val) {
if(!val) { return null; }
var p = arrayParser.create(val, function(entry) {
+ if(entry !== null) {
+ entry = parseFloat(entry);
+ }
return entry;
});
@@ -202,7 +217,7 @@ var init = function(register) {
register(1184, parseDate); // timestamp
register(1005, parseIntegerArray); // _int2
register(1007, parseIntegerArray); // _int4
- register(1016, parseIntegerArray); // _int8
+ register(1016, parseBigIntegerArray); // _int8
register(1021, parseFloatArray); // _float4
register(1022, parseFloatArray); // _float8
register(1231, parseFloatArray); // _numeric
View
45 test/unit/client/typed-query-results-tests.js
@@ -197,6 +197,51 @@ test('typed results', function() {
expected :function(val){
assert.deepEqual(val, [1.2,3.4]);
}
+ },{
+ name : 'array/int2',
+ format : 'text',
+ dataTypeID: 1005,
+ actual: '{-32768, -32767, 32766, 32767}',
+ expected :function(val){
+ assert.deepEqual(val, [-32768, -32767, 32766, 32767]);
+ }
+ },{
+ name : 'array/int4',
+ format : 'text',
+ dataTypeID: 1007,
+ actual: '{-2147483648, -2147483647, 2147483646, 2147483647}',
+ expected :function(val){
+ assert.deepEqual(val, [-2147483648, -2147483647, 2147483646, 2147483647]);
+ }
+ },{
+ name : 'array/int8',
+ format : 'text',
+ dataTypeID: 1016,
+ actual: '{-9223372036854775808, -9223372036854775807, 9223372036854775806, 9223372036854775807}',
+ expected :function(val){
+ assert.deepEqual(val, [
+ '-9223372036854775808',
+ '-9223372036854775807',
+ '9223372036854775806',
+ '9223372036854775807'
+ ]);
+ }
+ },{
+ name : 'array/float4',
+ format : 'text',
+ dataTypeID: 1021,
+ actual: '{1.2, 3.4}',
+ expected :function(val){
+ assert.deepEqual(val, [1.2, 3.4]);
+ }
+ },{
+ name : 'array/float8',
+ format : 'text',
+ dataTypeID: 1022,
+ actual: '{-12345678.1234567, 12345678.12345678}',
+ expected :function(val){
+ assert.deepEqual(val, [-12345678.1234567, 12345678.12345678]);
+ }
},
{
Something went wrong with that request. Please try again.