2016-03-22 20:10:42 +08:00
|
|
|
var _ = require('underscore');
|
|
|
|
var BaseWidget = require('./base');
|
|
|
|
var debug = require('debug')('windshaft:widget:aggregation');
|
|
|
|
|
|
|
|
var dot = require('dot');
|
|
|
|
dot.templateSettings.strip = false;
|
|
|
|
|
2017-06-13 00:55:33 +08:00
|
|
|
var filteredQueryTpl = dot.template([
|
|
|
|
'filtered_source AS (',
|
|
|
|
' SELECT *',
|
|
|
|
' FROM ({{=it._query}}) _cdb_filtered_source',
|
2017-06-16 01:07:31 +08:00
|
|
|
' {{?it._aggregationColumn && it._isFloatColumn}}WHERE',
|
2017-06-13 00:55:33 +08:00
|
|
|
' {{=it._aggregationColumn}} != \'infinity\'::float',
|
|
|
|
' AND',
|
|
|
|
' {{=it._aggregationColumn}} != \'-infinity\'::float',
|
|
|
|
' AND',
|
2017-06-16 01:07:31 +08:00
|
|
|
' {{=it._aggregationColumn}} != \'NaN\'::float{{?}}',
|
2017-06-13 00:55:33 +08:00
|
|
|
')'
|
|
|
|
].join(' \n'));
|
|
|
|
|
2016-03-22 20:10:42 +08:00
|
|
|
var summaryQueryTpl = dot.template([
|
|
|
|
'summary AS (',
|
|
|
|
' SELECT',
|
|
|
|
' count(1) AS count,',
|
2017-06-16 01:07:31 +08:00
|
|
|
' sum(CASE WHEN {{=it._column}} IS NULL THEN 1 ELSE 0 END) AS nulls_count',
|
|
|
|
' {{?it._isFloatColumn}},sum(',
|
2017-06-13 01:49:58 +08:00
|
|
|
' CASE',
|
|
|
|
' WHEN {{=it._aggregationColumn}} = \'infinity\'::float OR {{=it._aggregationColumn}} = \'-infinity\'::float',
|
|
|
|
' THEN 1',
|
|
|
|
' ELSE 0',
|
|
|
|
' END',
|
|
|
|
' ) AS infinities_count,',
|
2017-06-16 01:07:31 +08:00
|
|
|
' sum(CASE WHEN {{=it._aggregationColumn}} = \'NaN\'::float THEN 1 ELSE 0 END) AS nans_count{{?}}',
|
2016-03-22 20:10:42 +08:00
|
|
|
' FROM ({{=it._query}}) _cdb_aggregation_nulls',
|
|
|
|
')'
|
|
|
|
].join('\n'));
|
|
|
|
|
|
|
|
var rankedCategoriesQueryTpl = dot.template([
|
|
|
|
'categories AS(',
|
|
|
|
' SELECT {{=it._column}} AS category, {{=it._aggregation}} AS value,',
|
|
|
|
' row_number() OVER (ORDER BY {{=it._aggregation}} desc) as rank',
|
2017-06-13 01:45:06 +08:00
|
|
|
' FROM filtered_source',
|
2017-01-18 00:09:17 +08:00
|
|
|
' {{?it._aggregationColumn!==null}}WHERE {{=it._aggregationColumn}} IS NOT NULL{{?}}',
|
2016-03-22 20:10:42 +08:00
|
|
|
' GROUP BY {{=it._column}}',
|
|
|
|
' ORDER BY 2 DESC',
|
|
|
|
')'
|
|
|
|
].join('\n'));
|
|
|
|
|
2017-01-18 00:09:17 +08:00
|
|
|
var categoriesSummaryMinMaxQueryTpl = dot.template([
|
|
|
|
'categories_summary_min_max AS(',
|
|
|
|
' SELECT max(value) max_val, min(value) min_val',
|
2016-03-22 20:10:42 +08:00
|
|
|
' FROM categories',
|
|
|
|
')'
|
|
|
|
].join('\n'));
|
|
|
|
|
2017-01-18 00:09:17 +08:00
|
|
|
var categoriesSummaryCountQueryTpl = dot.template([
|
|
|
|
'categories_summary_count AS(',
|
|
|
|
' SELECT count(1) AS categories_count',
|
|
|
|
' FROM (',
|
|
|
|
' SELECT {{=it._column}} AS category',
|
|
|
|
' FROM ({{=it._query}}) _cdb_categories',
|
|
|
|
' GROUP BY {{=it._column}}',
|
|
|
|
' ) _cdb_categories_count',
|
|
|
|
')'
|
|
|
|
].join('\n'));
|
|
|
|
|
2016-03-22 20:10:42 +08:00
|
|
|
var rankedAggregationQueryTpl = dot.template([
|
2017-06-13 01:49:58 +08:00
|
|
|
'SELECT CAST(category AS text), value, false as agg, nulls_count, min_val, max_val,',
|
2017-06-16 01:07:31 +08:00
|
|
|
' count, categories_count{{?it._isFloatColumn}}, nans_count, infinities_count{{?}}',
|
2017-01-18 00:09:17 +08:00
|
|
|
' FROM categories, summary, categories_summary_min_max, categories_summary_count',
|
2016-03-22 20:10:42 +08:00
|
|
|
' WHERE rank < {{=it._limit}}',
|
|
|
|
'UNION ALL',
|
2017-06-13 01:49:58 +08:00
|
|
|
'SELECT \'Other\' category, {{=it._aggregationFn}}(value) as value, true as agg, nulls_count,',
|
2017-06-16 01:07:31 +08:00
|
|
|
' min_val, max_val, count, categories_count{{?it._isFloatColumn}}, nans_count, infinities_count{{?}}',
|
2017-01-18 00:09:17 +08:00
|
|
|
' FROM categories, summary, categories_summary_min_max, categories_summary_count',
|
2016-03-22 20:10:42 +08:00
|
|
|
' WHERE rank >= {{=it._limit}}',
|
2017-06-16 01:07:31 +08:00
|
|
|
'GROUP BY nulls_count, min_val, max_val, count,',
|
|
|
|
' categories_count{{?it._isFloatColumn}}, nans_count, infinities_count{{?}}'
|
2016-03-22 20:10:42 +08:00
|
|
|
].join('\n'));
|
|
|
|
|
|
|
|
var aggregationQueryTpl = dot.template([
|
|
|
|
'SELECT CAST({{=it._column}} AS text) AS category, {{=it._aggregation}} AS value, false as agg,',
|
2017-06-16 01:07:31 +08:00
|
|
|
' nulls_count, min_val, max_val, count, categories_count{{?it._isFloatColumn}}, nans_count, infinities_count{{?}}',
|
2017-01-18 00:09:17 +08:00
|
|
|
'FROM ({{=it._query}}) _cdb_aggregation_all, summary, categories_summary_min_max, categories_summary_count',
|
2017-06-16 01:07:31 +08:00
|
|
|
'GROUP BY category, nulls_count, min_val, max_val, count,',
|
|
|
|
' categories_count{{?it._isFloatColumn}}, nans_count, infinities_count{{?}}',
|
2016-03-22 20:10:42 +08:00
|
|
|
'ORDER BY value DESC'
|
|
|
|
].join('\n'));
|
|
|
|
|
|
|
|
var CATEGORIES_LIMIT = 6;
|
|
|
|
|
|
|
|
var VALID_OPERATIONS = {
|
|
|
|
count: [],
|
2016-06-20 22:20:48 +08:00
|
|
|
sum: ['aggregationColumn'],
|
|
|
|
avg: ['aggregationColumn'],
|
|
|
|
min: ['aggregationColumn'],
|
|
|
|
max: ['aggregationColumn']
|
2016-03-22 20:10:42 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
var TYPE = 'aggregation';
|
|
|
|
|
|
|
|
/**
|
|
|
|
{
|
|
|
|
type: 'aggregation',
|
|
|
|
options: {
|
|
|
|
column: 'name',
|
|
|
|
aggregation: 'count' // it could be, e.g., sum if column is numeric
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
2017-06-16 01:07:31 +08:00
|
|
|
function Aggregation(query, options, queries) {
|
2016-03-22 20:10:42 +08:00
|
|
|
if (!_.isString(options.column)) {
|
|
|
|
throw new Error('Aggregation expects `column` in widget options');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_.isString(options.aggregation)) {
|
|
|
|
throw new Error('Aggregation expects `aggregation` operation in widget options');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!VALID_OPERATIONS[options.aggregation]) {
|
|
|
|
throw new Error("Aggregation does not support '" + options.aggregation + "' operation");
|
|
|
|
}
|
|
|
|
|
|
|
|
var requiredOptions = VALID_OPERATIONS[options.aggregation];
|
|
|
|
var missingOptions = _.difference(requiredOptions, Object.keys(options));
|
|
|
|
if (missingOptions.length > 0) {
|
|
|
|
throw new Error(
|
|
|
|
"Aggregation '" + options.aggregation + "' is missing some options: " + missingOptions.join(',')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
BaseWidget.apply(this);
|
|
|
|
|
|
|
|
this.query = query;
|
2017-06-16 01:07:31 +08:00
|
|
|
this.queries = queries;
|
2016-03-22 20:10:42 +08:00
|
|
|
this.column = options.column;
|
|
|
|
this.aggregation = options.aggregation;
|
|
|
|
this.aggregationColumn = options.aggregationColumn;
|
2017-06-16 01:07:31 +08:00
|
|
|
this._isFloatColumn = null;
|
2016-03-22 20:10:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Aggregation.prototype = new BaseWidget();
|
|
|
|
Aggregation.prototype.constructor = Aggregation;
|
|
|
|
|
|
|
|
module.exports = Aggregation;
|
|
|
|
|
2016-06-01 17:42:24 +08:00
|
|
|
Aggregation.prototype.sql = function(psql, override, callback) {
|
2017-06-16 01:07:31 +08:00
|
|
|
var self = this;
|
|
|
|
|
2016-03-22 20:10:42 +08:00
|
|
|
if (!callback) {
|
|
|
|
callback = override;
|
|
|
|
override = {};
|
|
|
|
}
|
|
|
|
|
2017-06-16 01:07:31 +08:00
|
|
|
if (this.aggregationColumn && this._isFloatColumn === null) {
|
|
|
|
this._isFloatColumn = false;
|
|
|
|
this.getColumnType(psql, this.aggregationColumn, this.queries.no_filters, function (err, type) {
|
|
|
|
if (!err && !!type) {
|
|
|
|
self._isFloatColumn = type.float;
|
|
|
|
}
|
|
|
|
self.sql(psql, override, callback);
|
|
|
|
});
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-03-22 20:10:42 +08:00
|
|
|
var _query = this.query;
|
|
|
|
|
|
|
|
var aggregationSql;
|
2017-01-18 00:09:17 +08:00
|
|
|
|
2016-03-22 20:10:42 +08:00
|
|
|
if (!!override.ownFilter) {
|
|
|
|
aggregationSql = [
|
2017-06-16 01:07:31 +08:00
|
|
|
this.getCategoriesCTESql(
|
|
|
|
_query,
|
|
|
|
this.column,
|
|
|
|
this.aggregation,
|
|
|
|
this.aggregationColumn,
|
|
|
|
this._isFloatColumn
|
|
|
|
),
|
2016-03-22 20:10:42 +08:00
|
|
|
aggregationQueryTpl({
|
2017-06-16 01:07:31 +08:00
|
|
|
_isFloatColumn: this._isFloatColumn,
|
2016-03-22 20:10:42 +08:00
|
|
|
_query: _query,
|
|
|
|
_column: this.column,
|
|
|
|
_aggregation: this.getAggregationSql(),
|
|
|
|
_limit: CATEGORIES_LIMIT
|
|
|
|
})
|
|
|
|
].join('\n');
|
|
|
|
} else {
|
|
|
|
aggregationSql = [
|
2017-06-16 01:07:31 +08:00
|
|
|
this.getCategoriesCTESql(
|
|
|
|
_query,
|
|
|
|
this.column,
|
|
|
|
this.aggregation,
|
|
|
|
this.aggregationColumn,
|
|
|
|
this._isFloatColumn
|
|
|
|
),
|
2016-03-22 20:10:42 +08:00
|
|
|
rankedAggregationQueryTpl({
|
2017-06-16 01:07:31 +08:00
|
|
|
_isFloatColumn: this._isFloatColumn,
|
2016-03-22 20:10:42 +08:00
|
|
|
_query: _query,
|
|
|
|
_column: this.column,
|
2017-03-02 17:48:20 +08:00
|
|
|
_aggregationFn: this.aggregation !== 'count' ? this.aggregation : 'sum',
|
2016-03-22 20:10:42 +08:00
|
|
|
_limit: CATEGORIES_LIMIT
|
|
|
|
})
|
|
|
|
].join('\n');
|
|
|
|
}
|
|
|
|
|
|
|
|
debug(aggregationSql);
|
|
|
|
|
|
|
|
return callback(null, aggregationSql);
|
|
|
|
};
|
|
|
|
|
2017-06-16 01:07:31 +08:00
|
|
|
Aggregation.prototype.getCategoriesCTESql = function(query, column, aggregation, aggregationColumn, isFloatColumn) {
|
2017-03-02 17:48:20 +08:00
|
|
|
return [
|
2017-06-13 00:55:33 +08:00
|
|
|
"WITH",
|
|
|
|
[
|
|
|
|
filteredQueryTpl({
|
2017-06-16 01:07:31 +08:00
|
|
|
_isFloatColumn: isFloatColumn,
|
2017-06-13 00:55:33 +08:00
|
|
|
_query: this.query,
|
|
|
|
_column: this.column,
|
|
|
|
_aggregationColumn: aggregation !== 'count' ? aggregationColumn : null
|
|
|
|
}),
|
|
|
|
summaryQueryTpl({
|
2017-06-16 01:07:31 +08:00
|
|
|
_isFloatColumn: isFloatColumn,
|
2017-06-13 00:55:33 +08:00
|
|
|
_query: query,
|
|
|
|
_column: column,
|
|
|
|
_aggregationColumn: aggregation !== 'count' ? aggregationColumn : null
|
|
|
|
}),
|
|
|
|
rankedCategoriesQueryTpl({
|
|
|
|
_query: query,
|
|
|
|
_column: column,
|
|
|
|
_aggregation: this.getAggregationSql(),
|
|
|
|
_aggregationColumn: aggregation !== 'count' ? aggregationColumn : null
|
|
|
|
}),
|
|
|
|
categoriesSummaryMinMaxQueryTpl({
|
|
|
|
_query: query,
|
|
|
|
_column: column
|
|
|
|
}),
|
|
|
|
categoriesSummaryCountQueryTpl({
|
|
|
|
_query: query,
|
|
|
|
_column: column
|
|
|
|
})
|
|
|
|
].join(',\n')
|
|
|
|
].join('\n');
|
2017-03-02 17:48:20 +08:00
|
|
|
};
|
|
|
|
|
2016-03-22 20:10:42 +08:00
|
|
|
var aggregationFnQueryTpl = dot.template('{{=it._aggregationFn}}({{=it._aggregationColumn}})');
|
|
|
|
Aggregation.prototype.getAggregationSql = function() {
|
|
|
|
return aggregationFnQueryTpl({
|
|
|
|
_aggregationFn: this.aggregation,
|
|
|
|
_aggregationColumn: this.aggregationColumn || 1
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
Aggregation.prototype.format = function(result) {
|
|
|
|
var categories = [];
|
|
|
|
var count = 0;
|
|
|
|
var nulls = 0;
|
2017-06-13 00:55:33 +08:00
|
|
|
var nans = 0;
|
|
|
|
var infinities = 0;
|
2016-03-22 20:10:42 +08:00
|
|
|
var minValue = 0;
|
|
|
|
var maxValue = 0;
|
|
|
|
var categoriesCount = 0;
|
|
|
|
|
|
|
|
|
|
|
|
if (result.rows.length) {
|
|
|
|
var firstRow = result.rows[0];
|
|
|
|
count = firstRow.count;
|
|
|
|
nulls = firstRow.nulls_count;
|
2017-06-13 00:55:33 +08:00
|
|
|
nans = firstRow.nans_count;
|
|
|
|
infinities = firstRow.infinities_count;
|
2016-03-22 20:10:42 +08:00
|
|
|
minValue = firstRow.min_val;
|
|
|
|
maxValue = firstRow.max_val;
|
|
|
|
categoriesCount = firstRow.categories_count;
|
|
|
|
|
|
|
|
result.rows.forEach(function(row) {
|
2017-06-13 01:49:58 +08:00
|
|
|
categories.push(_.omit(row, 'count', 'nulls_count', 'min_val',
|
|
|
|
'max_val', 'categories_count', 'nans_count', 'infinities_count'));
|
2016-03-22 20:10:42 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2016-06-20 22:20:48 +08:00
|
|
|
aggregation: this.aggregation,
|
2016-03-22 20:10:42 +08:00
|
|
|
count: count,
|
|
|
|
nulls: nulls,
|
2017-06-13 00:55:33 +08:00
|
|
|
nans: nans,
|
|
|
|
infinities: infinities,
|
2016-03-22 20:10:42 +08:00
|
|
|
min: minValue,
|
|
|
|
max: maxValue,
|
|
|
|
categoriesCount: categoriesCount,
|
|
|
|
categories: categories
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
var filterCategoriesQueryTpl = dot.template([
|
|
|
|
'SELECT {{=it._column}} AS category, {{=it._value}} AS value',
|
|
|
|
'FROM ({{=it._query}}) _cdb_aggregation_search',
|
|
|
|
'WHERE CAST({{=it._column}} as text) ILIKE {{=it._userQuery}}',
|
|
|
|
'GROUP BY {{=it._column}}'
|
|
|
|
].join('\n'));
|
|
|
|
|
|
|
|
var searchQueryTpl = dot.template([
|
|
|
|
'WITH',
|
|
|
|
'search_unfiltered AS (',
|
|
|
|
' {{=it._searchUnfiltered}}',
|
|
|
|
'),',
|
|
|
|
'search_filtered AS (',
|
|
|
|
' {{=it._searchFiltered}}',
|
|
|
|
'),',
|
|
|
|
'search_union AS (',
|
|
|
|
' SELECT * FROM search_unfiltered',
|
|
|
|
' UNION ALL',
|
|
|
|
' SELECT * FROM search_filtered',
|
|
|
|
')',
|
|
|
|
'SELECT category, sum(value) AS value',
|
|
|
|
'FROM search_union',
|
|
|
|
'GROUP BY category',
|
|
|
|
'ORDER BY value desc'
|
|
|
|
].join('\n'));
|
|
|
|
|
|
|
|
|
|
|
|
Aggregation.prototype.search = function(psql, userQuery, callback) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
var _userQuery = psql.escapeLiteral('%' + userQuery + '%');
|
2017-07-07 23:44:32 +08:00
|
|
|
var _value = this.aggregation !== 'count' && this.aggregationColumn ?
|
|
|
|
this.aggregation + '(' + this.aggregationColumn + ')' : 'count(1)';
|
2016-03-22 20:10:42 +08:00
|
|
|
|
|
|
|
// TODO unfiltered will be wrong as filters are already applied at this point
|
|
|
|
var query = searchQueryTpl({
|
|
|
|
_searchUnfiltered: filterCategoriesQueryTpl({
|
|
|
|
_query: this.query,
|
|
|
|
_column: this.column,
|
|
|
|
_value: '0',
|
|
|
|
_userQuery: _userQuery
|
|
|
|
}),
|
|
|
|
_searchFiltered: filterCategoriesQueryTpl({
|
|
|
|
_query: this.query,
|
|
|
|
_column: this.column,
|
2017-07-07 23:09:17 +08:00
|
|
|
_value: _value,
|
2016-03-22 20:10:42 +08:00
|
|
|
_userQuery: _userQuery
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
psql.query(query, function(err, result) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
return callback(null, {type: self.getType(), categories: result.rows });
|
|
|
|
}, true); // use read-only transaction
|
|
|
|
};
|
|
|
|
|
|
|
|
Aggregation.prototype.getType = function() {
|
|
|
|
return TYPE;
|
|
|
|
};
|
|
|
|
|
|
|
|
Aggregation.prototype.toString = function() {
|
|
|
|
return JSON.stringify({
|
|
|
|
_type: TYPE,
|
|
|
|
_query: this.query,
|
|
|
|
_column: this.column,
|
|
|
|
_aggregation: this.aggregation
|
|
|
|
});
|
|
|
|
};
|