diff --git a/lib/cartodb/models/dataview/aggregation.js b/lib/cartodb/models/dataview/aggregation.js index ed0261ae..627eb73a 100644 --- a/lib/cartodb/models/dataview/aggregation.js +++ b/lib/cartodb/models/dataview/aggregation.js @@ -130,6 +130,41 @@ const aggregationQueryTpl = ctx => ` ORDER BY value DESC `; +const categoriesCTESqlTpl = ctx => ` + WITH + ${filteredQueryTpl({ + _isFloatColumn: ctx.isFloatColumn, + _query: ctx.query, + _column: ctx.column, + _aggregationColumn: ctx.aggregation !== 'count' ? ctx.aggregationColumn : null + })}, + ${summaryQueryTpl({ + _isFloatColumn: ctx.isFloatColumn, + _query: ctx.query, + _column: ctx.column, + _aggregationColumn: ctx.aggregation !== 'count' ? ctx.aggregationColumn : null + })}, + ${rankedCategoriesQueryTpl({ + _query: ctx.query, + _column: ctx.column, + _aggregation: aggregationFnQueryTpl({ + aggregation: ctx.aggregation, + aggregationColumn: ctx.aggregationColumn || 1 + }), + _aggregationColumn: ctx.aggregation !== 'count' ? ctx.aggregationColumn : null + })}, + ${categoriesSummaryMinMaxQueryTpl({ + _query: ctx.query, + _column: ctx.column + })}, + ${categoriesSummaryCountQueryTpl({ + _query: ctx.query, + _column: ctx.column + })} +`; + +const aggregationFnQueryTpl = ctx => `${ctx.aggregation}(${ctx.aggregationColumn})`; + const CATEGORIES_LIMIT = 6; const VALID_OPERATIONS = { @@ -209,32 +244,37 @@ Aggregation.prototype.sql = function(psql, override, callback) { var aggregationSql; + + if (!!override.ownFilter) { aggregationSql = [ - this.getCategoriesCTESql( - _query, - this.column, - this.aggregation, - this.aggregationColumn, - this._isFloatColumn - ), + categoriesCTESqlTpl({ + query: _query, + column: this.column, + aggregation: this.aggregation, + aggregationColumn: this.aggregationColumn, + isFloatColumn: this._isFloatColumn + }), aggregationQueryTpl({ _isFloatColumn: this._isFloatColumn, _query: _query, _column: this.column, - _aggregation: this.getAggregationSql(), + _aggregation: aggregationFnQueryTpl({ + aggregation: this.aggregation, + aggregationColumn: this.aggregationColumn || 1 + }), _limit: CATEGORIES_LIMIT }) ].join('\n'); } else { aggregationSql = [ - this.getCategoriesCTESql( - _query, - this.column, - this.aggregation, - this.aggregationColumn, - this._isFloatColumn - ), + categoriesCTESqlTpl({ + query: _query, + column: this.column, + aggregation: this.aggregation, + aggregationColumn: this.aggregationColumn, + isFloatColumn: this._isFloatColumn + }), rankedAggregationQueryTpl({ _isFloatColumn: this._isFloatColumn, _query: _query, @@ -250,47 +290,6 @@ Aggregation.prototype.sql = function(psql, override, callback) { return callback(null, aggregationSql); }; -Aggregation.prototype.getCategoriesCTESql = function() { - return ` - WITH - ${filteredQueryTpl({ - _isFloatColumn: this._isFloatColumn, - _query: this.query, - _column: this.column, - _aggregationColumn: this.aggregation !== 'count' ? this.aggregationColumn : null - })}, - ${summaryQueryTpl({ - _isFloatColumn: this._isFloatColumn, - _query: this.query, - _column: this.column, - _aggregationColumn: this.aggregation !== 'count' ? this.aggregationColumn : null - })}, - ${rankedCategoriesQueryTpl({ - _query: this.query, - _column: this.column, - _aggregation: this.getAggregationSql(), - _aggregationColumn: this.aggregation !== 'count' ? this.aggregationColumn : null - })}, - ${categoriesSummaryMinMaxQueryTpl({ - _query: this.query, - _column: this.column - })}, - ${categoriesSummaryCountQueryTpl({ - _query: this.query, - _column: this.column - })} - `; -}; - -const aggregationFnQueryTpl = ctx => `${ctx._aggregationFn}(${ctx._aggregationColumn})`; - -Aggregation.prototype.getAggregationSql = function() { - return aggregationFnQueryTpl({ - _aggregationFn: this.aggregation, - _aggregationColumn: this.aggregationColumn || 1 - }); -}; - Aggregation.prototype.format = function(result) { var categories = []; var count = 0;