Merge pull request #420 from CartoDB/support-special-float-values

Support special float values (NaN and Infinity)
This commit is contained in:
Daniel 2017-06-27 16:56:20 +02:00 committed by GitHub
commit 99a849d09e
2 changed files with 85 additions and 1 deletions

View File

@ -66,7 +66,21 @@ JsonFormat.prototype.handleQueryRow = function(row, result) {
this.lastKnownResult = result;
this.buffer += (this.total_rows++ ? ',' : '') + JSON.stringify(row);
this.buffer += (this.total_rows++ ? ',' : '') + JSON.stringify(row, function (key, value) {
if (value !== value) {
return 'NaN';
}
if (value === Infinity) {
return 'Infinity';
}
if (value === -Infinity) {
return '-Infinity';
}
return value;
});
if (this.total_rows % (this.opts.bufferedRows || 1000)) {
this.opts.sink.write(this.buffer);

View File

@ -0,0 +1,70 @@
require('../helper');
var server = require('../../app/server')();
var assert = require('../support/assert');
var querystring = require('querystring');
var step = require('step');
describe('special numeric (float) values', function() {
var RESPONSE_OK = {
statusCode: 200
};
var HEADERS = {
host: 'vizzuality.localhost.lan:8080'
};
var METHOD = 'GET';
var URL = '/api/v1/sql?api_key=1234&';
it('should cast Infinity and NaN values properly', function (done) {
step(
function createTable () {
var next = this;
var opts = {
url: URL + querystring.stringify({
q: 'create table numbers_test(val float)'
}),
headers: HEADERS,
method: METHOD
};
assert.response(server, opts, RESPONSE_OK, next);
},
function insertData (err) {
assert.ifError(err);
var next = this;
var opts = {
url: URL + querystring.stringify({
q: [
'insert into numbers_test',
' values (\'NaN\'::float), (\'infinity\'::float), (\'-infinity\'::float), (1::float)'
].join('')
}),
headers: HEADERS,
method: METHOD
};
assert.response(server, opts, RESPONSE_OK, next);
},
function queryData (err) {
assert.ifError(err);
var next = this;
var opts = {
url: URL + querystring.stringify({
q: 'select * from numbers_test'
}),
headers: HEADERS,
method: METHOD
};
assert.response(server, opts, RESPONSE_OK, next);
},
function assertResult (err, res) {
assert.ifError(err);
var result = JSON.parse(res.body);
assert.ok(Array.isArray(result.rows));
assert.equal(result.rows[0].val, 'NaN');
assert.equal(result.rows[1].val, 'Infinity');
assert.equal(result.rows[2].val, '-Infinity');
assert.equal(result.rows[3].val, 1);
done();
}
);
});
});