Implement aggregation functions

This commit is contained in:
Daniel García Aubert 2019-03-01 15:45:38 +01:00
parent 6dadb1bf6f
commit 57a229655c
2 changed files with 151 additions and 6 deletions

View File

@ -78,7 +78,7 @@ module.exports = class ClusterBackend {
Array.isArray(expressions) || Array.isArray(expressions) ||
['string', 'number', 'boolean'].includes(typeof expressions)) { ['string', 'number', 'boolean'].includes(typeof expressions)) {
const error = new Error( 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.http_status = 400;
error.type = 'layer'; error.type = 'layer';
@ -150,12 +150,16 @@ function getClusterFeatures (pg, zoom, clusterId, columns, query, resolution, ag
}); });
if (aggregation !== undefined) { 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({ sql = aggregationQuery({
columns, columns,
query: sql, expressions,
expressions query: sql
}); });
} }

View File

@ -394,6 +394,147 @@ describe('cluster', function () {
{ _cdb_feature_count: 1, type: 'even' }, { _cdb_feature_count: 1, type: 'even' },
{ _cdb_feature_count: 2, type: 'odd' } { _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 = { 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:[ errors_with_context:[
{ {
layer: { layer: {
index: '0', index: '0',
type: 'cartodb' 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', subtype: 'aggregation',
type: 'layer' type: 'layer'
} }