From b4799124e65c34b8a43af3f6722715bba9bf874f Mon Sep 17 00:00:00 2001 From: Javier Goizueta Date: Wed, 2 Aug 2017 17:28:46 +0200 Subject: [PATCH 1/3] Exclude non-finite values when computing ramps --- lib/cartodb/backends/turbo-carto-postgres-datasource.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/cartodb/backends/turbo-carto-postgres-datasource.js b/lib/cartodb/backends/turbo-carto-postgres-datasource.js index 8c0a93c7..65109325 100644 --- a/lib/cartodb/backends/turbo-carto-postgres-datasource.js +++ b/lib/cartodb/backends/turbo-carto-postgres-datasource.js @@ -10,7 +10,13 @@ function createTemplate(method) { 'max({{=it._column}}) max_val,', 'avg({{=it._column}}) avg_val,', method, - 'FROM ({{=it._sql}}) _table_sql WHERE {{=it._column}} IS NOT NULL' + 'FROM ({{=it._sql}}) _table_sql WHERE {{=it._column}} IS NOT NULL', + 'AND', + ' {{=it._column}} != \'infinity\'::float', + 'AND', + ' {{=it._column}} != \'-infinity\'::float', + 'AND', + ' {{=it._column}} != \'NaN\'::float' ].join('\n')); } From eed33fc76da3035d0441de5df906bc8d5cddc98b Mon Sep 17 00:00:00 2001 From: Javier Goizueta Date: Thu, 3 Aug 2017 19:07:02 +0200 Subject: [PATCH 2/3] Add tests for excluding NaNs, Ininities from ramps --- .../turbo-carto-postgres-datasource.js | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 test/unit/cartodb/backends/turbo-carto-postgres-datasource.js diff --git a/test/unit/cartodb/backends/turbo-carto-postgres-datasource.js b/test/unit/cartodb/backends/turbo-carto-postgres-datasource.js new file mode 100644 index 00000000..8276bf25 --- /dev/null +++ b/test/unit/cartodb/backends/turbo-carto-postgres-datasource.js @@ -0,0 +1,46 @@ +var PostgresDatasource = require('../../../../lib/cartodb/backends/turbo-carto-postgres-datasource'); +var PSQL = require('cartodb-psql'); +var _ = require('underscore'); +var assert = require('assert'); + +describe('turbo-carto-postgres-datasource', function() { + + beforeEach(function () { + const dbname = _.template(global.environment.postgres_auth_user, { user_id: 1 }) + '_db'; + const dbuser = _.template(global.environment.postgres_auth_user, { user_id: 1 }) + const pass = _.template(global.environment.postgres_auth_pass, { user_id: 1 }) + const psql = new PSQL({ + user: 'postgres', + dbname: dbname, + host: global.environment.postgres.host, + port: global.environment.postgres.port + }); + const sql = [ + 'SELECT', + ' null::geometry the_geom_webmercator,', + ' CASE', + ' WHEN x % 4 = 0 THEN \'infinity\'::float', + ' WHEN x % 4 = 1 THEN \'-infinity\'::float', + ' WHEN x % 4 = 2 THEN \'NaN\'::float', + ' ELSE x', + ' END AS values', + 'FROM generate_series(1, 1000) x' + ].join('\n') + this.datasource = new PostgresDatasource(psql, sql); + }); + + it('should ignore NaNs and Infinities when computing ramps', function(done) { + column = 'values'; + buckets = 4; + method = 'equal'; + this.datasource.getRamp(column, buckets, method, function(err, result) { + expected_result = { + ramp: [ 252, 501, 750, 999 ], + stats: { min_val: 3, max_val: 999, avg_val: 501 }, + strategy: undefined + }; + assert.deepEqual(result, expected_result); + done(); + }); + }); +}); From e01730e8e46d2d479e3dca82f0c640963fd0afc4 Mon Sep 17 00:00:00 2001 From: Javier Goizueta Date: Thu, 3 Aug 2017 19:16:08 +0200 Subject: [PATCH 3/3] Syntax fixes --- .../backends/turbo-carto-postgres-datasource.js | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/test/unit/cartodb/backends/turbo-carto-postgres-datasource.js b/test/unit/cartodb/backends/turbo-carto-postgres-datasource.js index 8276bf25..7e774023 100644 --- a/test/unit/cartodb/backends/turbo-carto-postgres-datasource.js +++ b/test/unit/cartodb/backends/turbo-carto-postgres-datasource.js @@ -7,8 +7,6 @@ describe('turbo-carto-postgres-datasource', function() { beforeEach(function () { const dbname = _.template(global.environment.postgres_auth_user, { user_id: 1 }) + '_db'; - const dbuser = _.template(global.environment.postgres_auth_user, { user_id: 1 }) - const pass = _.template(global.environment.postgres_auth_pass, { user_id: 1 }) const psql = new PSQL({ user: 'postgres', dbname: dbname, @@ -25,16 +23,16 @@ describe('turbo-carto-postgres-datasource', function() { ' ELSE x', ' END AS values', 'FROM generate_series(1, 1000) x' - ].join('\n') + ].join('\n'); this.datasource = new PostgresDatasource(psql, sql); }); it('should ignore NaNs and Infinities when computing ramps', function(done) { - column = 'values'; - buckets = 4; - method = 'equal'; + var column = 'values'; + var buckets = 4; + var method = 'equal'; this.datasource.getRamp(column, buckets, method, function(err, result) { - expected_result = { + var expected_result = { ramp: [ 252, 501, 750, 999 ], stats: { min_val: 3, max_val: 999, avg_val: 501 }, strategy: undefined