2016-05-31 21:30:38 +08:00
|
|
|
var BaseOverviewsDataview = require('./base');
|
2016-05-23 23:06:52 +08:00
|
|
|
var BaseDataview = require('../aggregation');
|
2017-06-14 01:01:59 +08:00
|
|
|
var debug = require('debug')('windshaft:widget:aggregation:overview');
|
2016-05-23 23:06:52 +08:00
|
|
|
|
2016-05-23 23:42:26 +08:00
|
|
|
var dot = require('dot');
|
|
|
|
dot.templateSettings.strip = false;
|
|
|
|
|
2017-06-14 01:01:59 +08:00
|
|
|
var filteredQueryTpl = dot.template([
|
|
|
|
'filtered_source AS (',
|
|
|
|
' SELECT *',
|
|
|
|
' FROM ({{=it._query}}) _cdb_filtered_source',
|
2017-06-16 01:22:26 +08:00
|
|
|
' {{?it._aggregationColumn && it._isFloatColumn}}WHERE',
|
2017-06-14 01:01:59 +08:00
|
|
|
' {{=it._aggregationColumn}} != \'infinity\'::float',
|
|
|
|
' AND',
|
|
|
|
' {{=it._aggregationColumn}} != \'-infinity\'::float',
|
|
|
|
' AND',
|
2017-06-16 01:22:26 +08:00
|
|
|
' {{=it._aggregationColumn}} != \'NaN\'::float{{?}}',
|
2017-06-14 01:01:59 +08:00
|
|
|
')'
|
|
|
|
].join(' \n'));
|
|
|
|
|
2016-05-23 23:42:26 +08:00
|
|
|
var summaryQueryTpl = dot.template([
|
|
|
|
'summary AS (',
|
|
|
|
' SELECT',
|
|
|
|
' sum(_feature_count) AS count,',
|
2017-06-16 01:22:26 +08:00
|
|
|
' sum(CASE WHEN {{=it._column}} IS NULL THEN 1 ELSE 0 END) AS nulls_count',
|
|
|
|
' {{?it._isFloatColumn}},sum(',
|
2017-06-14 01:01:59 +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:22:26 +08:00
|
|
|
' sum(CASE WHEN {{=it._aggregationColumn}} = \'NaN\'::float THEN 1 ELSE 0 END) AS nans_count{{?}}',
|
2016-05-23 23:42:26 +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-14 01:01:59 +08:00
|
|
|
' FROM filtered_source',
|
2017-01-18 00:10:08 +08:00
|
|
|
' {{?it._aggregationColumn!==null}}WHERE {{=it._aggregationColumn}} IS NOT NULL{{?}}',
|
2016-05-23 23:42:26 +08:00
|
|
|
' GROUP BY {{=it._column}}',
|
|
|
|
' ORDER BY 2 DESC',
|
|
|
|
')'
|
|
|
|
].join('\n'));
|
|
|
|
|
2017-01-18 00:10:08 +08:00
|
|
|
var categoriesSummaryMinMaxQueryTpl = dot.template([
|
|
|
|
'categories_summary_min_max AS(',
|
|
|
|
' SELECT max(value) max_val, min(value) min_val',
|
2016-05-23 23:42:26 +08:00
|
|
|
' FROM categories',
|
|
|
|
')'
|
|
|
|
].join('\n'));
|
|
|
|
|
2017-01-18 00:10:08 +08:00
|
|
|
var categoriesSummaryCountQueryTpl = dot.template([
|
|
|
|
'categories_summary_count AS(',
|
|
|
|
' SELECT count(1) AS categories_count',
|
|
|
|
' FROM (',
|
|
|
|
' SELECT {{=it._column}} AS category',
|
2017-06-14 21:05:46 +08:00
|
|
|
' FROM filtered_source',
|
2017-01-18 00:10:08 +08:00
|
|
|
' GROUP BY {{=it._column}}',
|
|
|
|
' ) _cdb_categories_count',
|
|
|
|
')'
|
|
|
|
].join('\n'));
|
|
|
|
|
2016-05-23 23:42:26 +08:00
|
|
|
var rankedAggregationQueryTpl = dot.template([
|
2017-06-14 01:01:59 +08:00
|
|
|
'SELECT CAST(category AS text), value, false as agg, nulls_count, min_val, max_val,',
|
2017-06-16 01:22:26 +08:00
|
|
|
' count, categories_count{{?it._isFloatColumn}}, nans_count, infinities_count{{?}}',
|
2017-01-18 00:10:08 +08:00
|
|
|
' FROM categories, summary, categories_summary_min_max, categories_summary_count',
|
2016-05-23 23:42:26 +08:00
|
|
|
' WHERE rank < {{=it._limit}}',
|
|
|
|
'UNION ALL',
|
2017-06-14 01:01:59 +08:00
|
|
|
'SELECT \'Other\' category, sum(value), true as agg, nulls_count, min_val, max_val,',
|
2017-06-16 01:22:26 +08:00
|
|
|
' count, categories_count{{?it._isFloatColumn}}, nans_count, infinities_count{{?}}',
|
2017-01-18 00:10:08 +08:00
|
|
|
' FROM categories, summary, categories_summary_min_max, categories_summary_count',
|
2016-05-23 23:42:26 +08:00
|
|
|
' WHERE rank >= {{=it._limit}}',
|
2017-06-16 01:22:26 +08:00
|
|
|
'GROUP BY nulls_count, min_val, max_val, count,',
|
|
|
|
' categories_count{{?it._isFloatColumn}}, nans_count, infinities_count{{?}}'
|
2016-05-23 23:42:26 +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:22:26 +08:00
|
|
|
' nulls_count, min_val, max_val, count, categories_count{{?it._isFloatColumn}}, nans_count, infinities_count{{?}}',
|
2017-06-14 21:05:46 +08:00
|
|
|
'FROM filtered_source, summary, categories_summary_min_max, categories_summary_count',
|
2017-06-16 01:22:26 +08:00
|
|
|
'GROUP BY category, nulls_count, min_val, max_val, count,',
|
|
|
|
' categories_count{{?it._isFloatColumn}}, nans_count, infinities_count{{?}}',
|
2016-05-23 23:42:26 +08:00
|
|
|
'ORDER BY value DESC'
|
|
|
|
].join('\n'));
|
|
|
|
|
|
|
|
var CATEGORIES_LIMIT = 6;
|
|
|
|
|
2017-06-16 01:22:26 +08:00
|
|
|
function Aggregation(query, options, queryRewriter, queryRewriteData, params, queries) {
|
2016-05-31 21:30:38 +08:00
|
|
|
BaseOverviewsDataview.call(this, query, options, BaseDataview, queryRewriter, queryRewriteData, params);
|
2016-05-23 23:42:26 +08:00
|
|
|
|
|
|
|
this.query = query;
|
2017-06-16 01:22:26 +08:00
|
|
|
this.queries = queries;
|
2016-05-23 23:42:26 +08:00
|
|
|
this.column = options.column;
|
|
|
|
this.aggregation = options.aggregation;
|
|
|
|
this.aggregationColumn = options.aggregationColumn;
|
2017-06-16 01:22:26 +08:00
|
|
|
this._isFloatColumn = null;
|
2016-05-23 23:06:52 +08:00
|
|
|
}
|
|
|
|
|
2016-05-31 21:30:38 +08:00
|
|
|
Aggregation.prototype = Object.create(BaseOverviewsDataview.prototype);
|
2016-05-23 23:06:52 +08:00
|
|
|
Aggregation.prototype.constructor = Aggregation;
|
|
|
|
|
|
|
|
module.exports = Aggregation;
|
2016-05-23 23:42:26 +08:00
|
|
|
|
2016-12-17 00:37:05 +08:00
|
|
|
Aggregation.prototype.sql = function(psql, override, callback) {
|
2017-06-16 01:22:26 +08:00
|
|
|
var self = this;
|
|
|
|
|
2016-05-23 23:42:26 +08:00
|
|
|
if (!callback) {
|
|
|
|
callback = override;
|
|
|
|
override = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
var _query = this.rewrittenQuery(this.query);
|
2017-06-14 01:01:59 +08:00
|
|
|
var _aggregationColumn = this.aggregation !== 'count' ? this.aggregationColumn : null;
|
2016-05-23 23:42:26 +08:00
|
|
|
|
2017-06-16 01:22:26 +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-05-23 23:42:26 +08:00
|
|
|
var aggregationSql;
|
|
|
|
if (!!override.ownFilter) {
|
|
|
|
aggregationSql = [
|
|
|
|
"WITH",
|
|
|
|
[
|
2017-06-14 01:01:59 +08:00
|
|
|
filteredQueryTpl({
|
2017-06-16 01:22:26 +08:00
|
|
|
_isFloatColumn: this._isFloatColumn,
|
2017-06-14 01:01:59 +08:00
|
|
|
_query: _query,
|
|
|
|
_column: this.column,
|
|
|
|
_aggregationColumn: _aggregationColumn
|
|
|
|
}),
|
2016-05-23 23:42:26 +08:00
|
|
|
summaryQueryTpl({
|
2017-06-16 01:22:26 +08:00
|
|
|
_isFloatColumn: this._isFloatColumn,
|
2016-05-23 23:42:26 +08:00
|
|
|
_query: _query,
|
2017-06-14 01:01:59 +08:00
|
|
|
_column: this.column,
|
|
|
|
_aggregationColumn: _aggregationColumn
|
2016-05-23 23:42:26 +08:00
|
|
|
}),
|
|
|
|
rankedCategoriesQueryTpl({
|
|
|
|
_query: _query,
|
|
|
|
_column: this.column,
|
2017-01-18 00:10:08 +08:00
|
|
|
_aggregation: this.getAggregationSql(),
|
2017-06-14 01:01:59 +08:00
|
|
|
_aggregationColumn: _aggregationColumn
|
2016-05-23 23:42:26 +08:00
|
|
|
}),
|
2017-01-18 00:10:08 +08:00
|
|
|
categoriesSummaryMinMaxQueryTpl({
|
|
|
|
_query: _query,
|
|
|
|
_column: this.column
|
|
|
|
}),
|
|
|
|
categoriesSummaryCountQueryTpl({
|
2016-05-23 23:42:26 +08:00
|
|
|
_query: _query,
|
|
|
|
_column: this.column
|
|
|
|
})
|
|
|
|
].join(',\n'),
|
|
|
|
aggregationQueryTpl({
|
2017-06-16 01:22:26 +08:00
|
|
|
_isFloatColumn: this._isFloatColumn,
|
2016-05-23 23:42:26 +08:00
|
|
|
_query: _query,
|
|
|
|
_column: this.column,
|
|
|
|
_aggregation: this.getAggregationSql(),
|
|
|
|
_limit: CATEGORIES_LIMIT
|
|
|
|
})
|
|
|
|
].join('\n');
|
|
|
|
} else {
|
|
|
|
aggregationSql = [
|
|
|
|
"WITH",
|
|
|
|
[
|
2017-06-14 01:01:59 +08:00
|
|
|
filteredQueryTpl({
|
2017-06-16 01:22:26 +08:00
|
|
|
_isFloatColumn: this._isFloatColumn,
|
2017-06-14 01:01:59 +08:00
|
|
|
_query: _query,
|
|
|
|
_column: this.column,
|
|
|
|
_aggregationColumn: _aggregationColumn
|
|
|
|
}),
|
2016-05-23 23:42:26 +08:00
|
|
|
summaryQueryTpl({
|
2017-06-16 01:22:26 +08:00
|
|
|
_isFloatColumn: this._isFloatColumn,
|
2016-05-23 23:42:26 +08:00
|
|
|
_query: _query,
|
2017-06-14 01:01:59 +08:00
|
|
|
_column: this.column,
|
|
|
|
_aggregationColumn: _aggregationColumn
|
2016-05-23 23:42:26 +08:00
|
|
|
}),
|
|
|
|
rankedCategoriesQueryTpl({
|
|
|
|
_query: _query,
|
|
|
|
_column: this.column,
|
2017-01-18 00:10:08 +08:00
|
|
|
_aggregation: this.getAggregationSql(),
|
2017-06-14 01:01:59 +08:00
|
|
|
_aggregationColumn: _aggregationColumn
|
2017-01-18 00:10:08 +08:00
|
|
|
}),
|
|
|
|
categoriesSummaryMinMaxQueryTpl({
|
|
|
|
_query: _query,
|
|
|
|
_column: this.column
|
2016-05-23 23:42:26 +08:00
|
|
|
}),
|
2017-01-18 00:10:08 +08:00
|
|
|
categoriesSummaryCountQueryTpl({
|
2016-05-23 23:42:26 +08:00
|
|
|
_query: _query,
|
|
|
|
_column: this.column
|
|
|
|
})
|
|
|
|
].join(',\n'),
|
|
|
|
rankedAggregationQueryTpl({
|
2017-06-16 01:22:26 +08:00
|
|
|
_isFloatColumn: this._isFloatColumn,
|
2016-05-23 23:42:26 +08:00
|
|
|
_query: _query,
|
|
|
|
_column: this.column,
|
|
|
|
_limit: CATEGORIES_LIMIT
|
|
|
|
})
|
|
|
|
].join('\n');
|
|
|
|
}
|
|
|
|
|
2017-06-14 01:01:59 +08:00
|
|
|
debug(aggregationSql);
|
|
|
|
|
2016-05-23 23:42:26 +08:00
|
|
|
return callback(null, aggregationSql);
|
|
|
|
};
|
|
|
|
|
|
|
|
var aggregationFnQueryTpl = {
|
|
|
|
count: dot.template('sum(_feature_count)'),
|
|
|
|
sum: dot.template('sum({{=it._aggregationColumn}}*_feature_count)')
|
|
|
|
};
|
|
|
|
|
|
|
|
Aggregation.prototype.getAggregationSql = function() {
|
|
|
|
return aggregationFnQueryTpl[this.aggregation]({
|
|
|
|
_aggregationFn: this.aggregation,
|
|
|
|
_aggregationColumn: this.aggregationColumn || 1
|
|
|
|
});
|
|
|
|
};
|