diff --git a/lib/cartodb/backends/cluster.js b/lib/cartodb/backends/cluster.js index 671684e4..c9108da9 100644 --- a/lib/cartodb/backends/cluster.js +++ b/lib/cartodb/backends/cluster.js @@ -78,7 +78,7 @@ module.exports = class ClusterBackend { Array.isArray(expressions) || ['string', 'number', 'boolean'].includes(typeof expressions)) { const error = new Error( - `Invalid aggregation input, expressions should be and object with expressions` + `Invalid aggregation input, expressions should be and object with valid functions` ); error.http_status = 400; error.type = 'layer'; @@ -150,12 +150,16 @@ function getClusterFeatures (pg, zoom, clusterId, columns, query, resolution, ag }); if (aggregation !== undefined) { - const { columns = [], expressions = [] } = aggregation; + let { columns = [], expressions = [] } = aggregation; + + if (expressions) { + expressions = Object.entries(expressions).map(entries =>`${entries[1].aggregated_function}(${entries[1].aggregated_column}) AS ${entries[0]}`); + } sql = aggregationQuery({ columns, - query: sql, - expressions + expressions, + query: sql }); } diff --git a/test/acceptance/cluster.js b/test/acceptance/cluster.js index 5e856da2..881dbf8e 100644 --- a/test/acceptance/cluster.js +++ b/test/acceptance/cluster.js @@ -394,6 +394,147 @@ describe('cluster', function () { { _cdb_feature_count: 1, type: 'even' }, { _cdb_feature_count: 2, type: 'odd' } ] + }, + { + zoom: 0, + cartodb_id: 1, + resolution: 1, + aggregation: { + columns: [ 'type' ], + expressions: { + max_value: { + aggregated_function: 'max', + aggregated_column: 'value', + } + } + }, + expected: [ { _cdb_feature_count: 1, type: 'odd', max_value: -3 } ] + }, + { + zoom: 0, + cartodb_id: 2, + resolution: 1, + aggregation: { + columns: [ 'type' ], + expressions: { + max_value: { + aggregated_function: 'max', + aggregated_column: 'value', + } + } + }, + expected: [ { _cdb_feature_count: 1, type: 'even', max_value: -2 } ] + }, + { + zoom: 0, + cartodb_id: 3, + resolution: 1, + aggregation: { + columns: [ 'type' ], + expressions: { + max_value: { + aggregated_function: 'max', + aggregated_column: 'value', + } + } + }, + expected: [ { _cdb_feature_count: 1, type: 'odd', max_value: -1 } ] + }, + { + zoom: 0, + cartodb_id: 4, + resolution: 1, + aggregation: { + columns: [ 'type' ], + expressions: { + max_value: { + aggregated_function: 'max', + aggregated_column: 'value', + } + } + }, + expected: [ { _cdb_feature_count: 1, type: 'even', max_value: 0 } ] + }, + { + zoom: 0, + cartodb_id: 5, + resolution: 1, + aggregation: { + columns: [ 'type' ], + expressions: { + max_value: { + aggregated_function: 'max', + aggregated_column: 'value', + } + } + }, + expected: [ { _cdb_feature_count: 1, type: 'odd', max_value: 1 } ] + }, + { + zoom: 0, + cartodb_id: 6, + resolution: 1, + aggregation: { + columns: [ 'type' ], + expressions: { + max_value: { + aggregated_function: 'max', + aggregated_column: 'value', + } + } + }, + expected: [ { _cdb_feature_count: 1, type: 'even', max_value: 2 } ] + }, + { + zoom: 0, + cartodb_id: 7, + resolution: 1, + aggregation: { + columns: [ 'type' ], + expressions: { + max_value: { + aggregated_function: 'max', + aggregated_column: 'value', + } + } + }, + expected: [ { _cdb_feature_count: 1, type: 'odd', max_value: 3 } ] + }, + { + zoom: 0, + cartodb_id: 1, + resolution: 50, + aggregation: { + columns: [ 'type' ], + expressions: { + max_value: { + aggregated_function: 'max', + aggregated_column: 'value', + } + } + }, + expected: [ + { _cdb_feature_count: 2, type: 'even', max_value: 0 }, + { _cdb_feature_count: 2, type: 'odd', max_value: -1 } + ] + }, + { + zoom: 0, + cartodb_id: 5, + resolution: 50, + aggregation: { + columns: [ 'type' ], + expressions: { + max_value: { + aggregated_function: 'max', + aggregated_column: 'value', + } + } + }, + expected: [ + { _cdb_feature_count: 1, type: 'even', max_value: 2 }, + { _cdb_feature_count: 2, type: 'odd', max_value: 3 } + ] } ]; @@ -443,14 +584,14 @@ describe('cluster', function () { }; const expectedExpressionsError = { - errors:[ 'Invalid aggregation input, expressions should be and object with expressions' ], + errors:[ 'Invalid aggregation input, expressions should be and object with valid functions' ], errors_with_context:[ { layer: { index: '0', type: 'cartodb' }, - message: 'Invalid aggregation input, expressions should be and object with expressions', + message: 'Invalid aggregation input, expressions should be and object with valid functions', subtype: 'aggregation', type: 'layer' }