diff --git a/lib/cartodb/models/dataview/aggregation.js b/lib/cartodb/models/dataview/aggregation.js index 2ba36ede..b22d2345 100644 --- a/lib/cartodb/models/dataview/aggregation.js +++ b/lib/cartodb/models/dataview/aggregation.js @@ -165,6 +165,36 @@ const categoriesCTESqlTpl = ctx => ` const aggregationFnQueryTpl = ctx => `${ctx.aggregation}(${ctx.aggregationColumn})`; +const aggregationSqlTpl = ctx => ` + ${categoriesCTESqlTpl({ + query: ctx.query, + column: ctx.column, + aggregation: ctx.aggregation, + aggregationColumn: ctx.aggregationColumn, + isFloatColumn: ctx.isFloatColumn + })} + ${!!ctx.override.ownFilter ? + `${aggregationQueryTpl({ + _isFloatColumn: ctx.isFloatColumn, + _query: ctx.query, + _column: ctx.column, + _aggregation: aggregationFnQueryTpl({ + aggregation: ctx.aggregation, + aggregationColumn: ctx.aggregationColumn || 1 + }), + _limit: CATEGORIES_LIMIT + }) + }` : + `${rankedAggregationQueryTpl({ + _isFloatColumn: ctx.isFloatColumn, + _query: ctx.query, + _column: ctx.column, + _aggregationFn: ctx.aggregation !== 'count' ? ctx.aggregation : 'sum', + _limit: CATEGORIES_LIMIT + })} + `} +`; + const CATEGORIES_LIMIT = 6; const VALID_OPERATIONS = { @@ -240,33 +270,14 @@ Aggregation.prototype.sql = function(psql, override, callback) { return null; } - var aggregationSql = ` - ${categoriesCTESqlTpl({ - query: this.query, - column: this.column, - aggregation: this.aggregation, - aggregationColumn: this.aggregationColumn, - isFloatColumn: this._isFloatColumn - })} - ${!!override.ownFilter ? - `${aggregationQueryTpl({ - _isFloatColumn: this._isFloatColumn, - _query: this.query, - _column: this.column, - _aggregation: aggregationFnQueryTpl({ - aggregation: this.aggregation, - aggregationColumn: this.aggregationColumn || 1 - }), - _limit: CATEGORIES_LIMIT - })}` : - `${rankedAggregationQueryTpl({ - _isFloatColumn: this._isFloatColumn, - _query: this.query, - _column: this.column, - _aggregationFn: this.aggregation !== 'count' ? this.aggregation : 'sum', - _limit: CATEGORIES_LIMIT - })}`} - `; + var aggregationSql = aggregationSqlTpl({ + override: override, + query: this.query, + column: this.column, + aggregation: this.aggregation, + aggregationColumn: this.aggregationColumn, + isFloatColumn: this._isFloatColumn + }); debug(aggregationSql);