From ba6cca46a171f3f5f24526748056381a573d64dc Mon Sep 17 00:00:00 2001 From: Javier Goizueta Date: Thu, 14 Dec 2017 16:37:15 +0100 Subject: [PATCH] Fix aggregation queries --- .../models/aggregation/aggregation-query.js | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/lib/cartodb/models/aggregation/aggregation-query.js b/lib/cartodb/models/aggregation/aggregation-query.js index f088e6b3..b987e24d 100644 --- a/lib/cartodb/models/aggregation/aggregation-query.js +++ b/lib/cartodb/models/aggregation/aggregation-query.js @@ -50,11 +50,20 @@ const SUPPORTED_AGGREGATE_FUNCTIONS = { }; const aggregateColumns = ctx => { - let columns = Object.assign({ + return Object.assign({ _cdb_feature_count: { aggregate_function: 'count' } }, ctx.columns || {}); +}; + +const aggregateColumnNames = ctx => { + let columns = aggregateColumns(ctx); + return Object.keys(columns).join(', '); +}; + +const aggregateColumnDefs = ctx => { + let columns = aggregateColumns(ctx); return Object.keys(columns).map(column_name => { const aggregate_function = columns[column_name].aggregate_function || 'count'; const aggregate_definition = SUPPORTED_AGGREGATE_FUNCTIONS[aggregate_function]; @@ -89,7 +98,7 @@ const aggregationQueryTemplates = { AVG(ST_Y(_cdb_query.the_geom_webmercator)) ), 3857 ) AS the_geom_webmercator, - ${aggregateColumns(ctx)} + ${aggregateColumnDefs(ctx)} FROM (${ctx.sourceQuery}) _cdb_query, _cdb_params WHERE _cdb_query.the_geom_webmercator && _cdb_params.bbox GROUP BY @@ -107,14 +116,14 @@ const aggregationQueryTemplates = { SELECT 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, - ${aggregateColumns(ctx)} + ${aggregateColumnDefs(ctx)} FROM (${ctx.sourceQuery}) _cdb_query, _cdb_params WHERE the_geom_webmercator && _cdb_params.bbox GROUP BY _cdb_gx, _cdb_gy ) SELECT ST_SetSRID(ST_MakePoint(_cdb_gx*(res+0.5), _cdb_gy*(res+0.5)), 3857) AS the_geom_webmercator, - _cdb_feature_count + ${aggregateColumnNames(ctx)} FROM _cdb_clusters, _cdb_params `, @@ -127,7 +136,7 @@ const aggregationQueryTemplates = { ), _cdb_clusters AS ( SELECT MIN(cartodb_id) AS cartodb_id, - ${aggregateColumns(ctx)} + ${aggregateColumnDefs(ctx)} FROM (${ctx.sourceQuery}) _cdb_query, _cdb_params WHERE _cdb_query.the_geom_webmercator && _cdb_params.bbox GROUP BY @@ -136,7 +145,7 @@ const aggregationQueryTemplates = { ) SELECT _cdb_clusters.cartodb_id, the_geom, the_geom_webmercator, - _cdb_feature_count + ${aggregateColumnNames(ctx)} FROM _cdb_clusters INNER JOIN (${ctx.sourceQuery}) _cdb_query ON (_cdb_clusters.cartodb_id = _cdb_query.cartodb_id)