Skip to content

Array parser drops decimal section of double precision values #131

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
makkeu opened this issue May 30, 2012 · 2 comments
Closed

Array parser drops decimal section of double precision values #131

makkeu opened this issue May 30, 2012 · 2 comments

Comments

@makkeu
Copy link

makkeu commented May 30, 2012

Currently, parseIntegerArray function is used for both integer and float value parsing.
This isn't working for actual decimal numbers, since parseInt is used:

var parseIntegerArray = function(val) {
  if(!val) return null;
  var p = arrayParser.create(val, function(entry){
    if(entry != null)
      entry = parseInt(entry, 10);

    return entry;
  });

  return p.parse();
};

This is related to the pull request #96.

One possible fix could be checking, if the string contains "." and using parseFloat instead:

      if(entry.indexOf(".") > -1) {
        entry = parseFloat(entry);
      } else {
        entry = parseInt(entry, 10);
      }

Tho, more elegant way would be to write separate parseFloatArray function for types 1021, 1022 and 1231.

@brianc
Copy link
Owner

brianc commented May 30, 2012

Yeah that is an ugly bug. Could you possibly include a sample of failing code? I'll try to get this fixed asap.

@makkeu
Copy link
Author

makkeu commented May 30, 2012

Here is a simple test for the situation (modified CREATE TEMP/INSERT INTO and added test in array-tests.js):

var helper = require(__dirname + "/test-helper");
var pg = helper.pg;

test('parsing array results', function() {
  pg.connect(helper.config, assert.calls(function(err, client) {
    assert.isNull(err);
    client.query("CREATE TEMP TABLE why(names text[], numbors integer[], decimals double precision[])");
    client.query('INSERT INTO why(names, numbors, decimals) VALUES(\'{"aaron", "brian","a b c" }\', \'{1, 2, 3}\', \'{.1, 0.05, 3.654}\')').on('error', console.log);
    ...
    test('decimals', function() {
      //      client.connection.on('message', console.log)
      client.query('SELECT decimals FROM why', assert.success(function(result) {
        assert.lengthIs(result.rows[0].decimals, 3);
        assert.equal(result.rows[0].decimals[0], 0.1);
        assert.equal(result.rows[0].decimals[1], 0.05);
        assert.equal(result.rows[0].decimals[2], 3.654);
      }))
    })
    ...
  }))
})

brianc added a commit that referenced this issue May 31, 2012
@brianc brianc closed this as completed in 7f00b3e May 31, 2012
brianc pushed a commit that referenced this issue Dec 27, 2019
If an error not related to the query occurs, the client is emitting an
error event.

Forward this event to the callback.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants