From b2fcbdd8a3b6f370c126ff33f1432385545d5460 Mon Sep 17 00:00:00 2001 From: Javier Goizueta Date: Tue, 12 Dec 2017 11:18:18 +0100 Subject: [PATCH 1/2] Implement aggregation columns --- .../aggregation/aggregation-templates.js | 28 ++++++++++++++++--- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/lib/cartodb/models/aggregation/aggregation-templates.js b/lib/cartodb/models/aggregation/aggregation-templates.js index 0597ed43..6b864240 100644 --- a/lib/cartodb/models/aggregation/aggregation-templates.js +++ b/lib/cartodb/models/aggregation/aggregation-templates.js @@ -3,13 +3,33 @@ */ module.exports = (options) => { let templateFn = aggregationQueryTemplates[options.placement]; - console.log(options); if (!templateFn) { throw new Error("Invalid Aggregation placement: '" + options.placement + "'"); } return templateFn; }; +const aggregate_columns = ctx => { + let columns = ctx.columns || {}; + if (Object.keys(columns).length == 0) { + // default aggregation + columns = { + _cdb_feature_count: { + aggregate_function: 'count' + } + } + } + return Object.keys(columns).map(column_name => { + let aggregate_expression = columns[column_name].aggregate_expression; + if (!aggregate_expression) { + const aggregate_function = columns[column_name].aggregate_function || 'count'; + const aggregated_column = columns[column_name].aggregated_column || '*'; + aggregate_expression = `${aggregate_function}(${aggregated_column})`; + } + return `${aggregate_expression} AS ${column_name}`; + }).join(', '); +}; + // Notes: // * ${ctx.res*0.00028/256}*!scale_denominator! is equivalent to ${ctx.res/256}*CDB_XYZ_Resolution(CDB_ZoomFromScale(!scale_denominator!)) // * We need to filter spatially using !bbox! to make the queries efficient because the filter added by Mapnik (wrapping the query) @@ -32,7 +52,7 @@ const aggregationQueryTemplates = { AVG(ST_Y(_cdb_query.the_geom_webmercator)) ), 3857 ) AS the_geom_webmercator, - count(*) AS _cdb_feature_count + ${aggregate_columns(ctx)} FROM (${ctx.sourceQuery}) _cdb_query, _cdb_params WHERE _cdb_query.the_geom_webmercator && _cdb_params.bbox GROUP BY Floor(ST_X(_cdb_query.the_geom_webmercator)/_cdb_params.res), Floor(ST_Y(_cdb_query.the_geom_webmercator)/_cdb_params.res) @@ -49,7 +69,7 @@ const aggregationQueryTemplates = { ST_SetSRID(ST_MakePoint(AVG(ST_X(_cdb_query.the_geom_webmercator)), AVG(ST_Y(_cdb_query.the_geom_webmercator))), 3857) AS the_geom_webmercator, Floor(ST_X(_cdb_query.the_geom_webmercator)/_cdb_params.res)::int AS _cdb_gx, Floor(ST_Y(_cdb_query.the_geom_webmercator)/_cdb_params.res)::int AS _cdb_gy, - count(*) AS _cdb_feature_count + ${aggregate_columns(ctx)} FROM (${ctx.sourceQuery}) _cdb_query, _cdb_params WHERE the_geom_webmercator && _cdb_params.bbox GROUP BY _cdb_gx, _cdb_gy @@ -68,7 +88,7 @@ const aggregationQueryTemplates = { ), _cdb_clusters AS ( SELECT MIN(cartodb_id) AS cartodb_id, - count(*) AS _cdb_feature_count + ${aggregate_columns(ctx)} FROM (${ctx.sourceQuery}) _cdb_query, _cdb_params WHERE _cdb_query.the_geom_webmercator && _cdb_params.bbox GROUP BY Floor(ST_X(_cdb_query.the_geom_webmercator)/_cdb_params.res), Floor(ST_Y(_cdb_query.the_geom_webmercator)/_cdb_params.res) From e37682403c5347cedc513e037e228265be18d229 Mon Sep 17 00:00:00 2001 From: Javier Goizueta Date: Tue, 12 Dec 2017 11:19:05 +0100 Subject: [PATCH 2/2] Fix test Note that the CartoCSS should reference columns of the aggregated table --- test/acceptance/aggregation.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/acceptance/aggregation.js b/test/acceptance/aggregation.js index 2576a09c..804610ba 100644 --- a/test/acceptance/aggregation.js +++ b/test/acceptance/aggregation.js @@ -143,7 +143,7 @@ describe('aggregation', function () { }, threshold: 1 }, - cartocss: '#layer { marker-width: [value]; }', + cartocss: '#layer { marker-width: [total]; }', cartocss_version: '2.3.0' } }