Remove prefix to context's props in order to share it throught all templates avoiding to duplicate passing custom params again and again
This commit is contained in:
@@ -4,14 +4,14 @@ const debug = require('debug')('windshaft:widget:aggregation');
|
||||
const filteredQueryTpl = ctx => `
|
||||
filtered_source AS (
|
||||
SELECT *
|
||||
FROM (${ctx._query}) _cdb_filtered_source
|
||||
${ctx._aggregationColumn && ctx._isFloatColumn ? `
|
||||
FROM (${ctx.query}) _cdb_filtered_source
|
||||
${ctx.aggregationColumn && ctx.isFloatColumn ? `
|
||||
WHERE
|
||||
${ctx._aggregationColumn} != 'infinity'::float
|
||||
${ctx.aggregationColumn} != 'infinity'::float
|
||||
AND
|
||||
${ctx._aggregationColumn} != '-infinity'::float
|
||||
${ctx.aggregationColumn} != '-infinity'::float
|
||||
AND
|
||||
${ctx._aggregationColumn} != 'NaN'::float` :
|
||||
${ctx.aggregationColumn} != 'NaN'::float` :
|
||||
''
|
||||
}
|
||||
)
|
||||
@@ -21,31 +21,31 @@ const summaryQueryTpl = ctx => `
|
||||
summary AS (
|
||||
SELECT
|
||||
count(1) AS count,
|
||||
sum(CASE WHEN ${ctx._column} IS NULL THEN 1 ELSE 0 END) AS nulls_count
|
||||
${ctx._isFloatColumn ? `,
|
||||
sum(CASE WHEN ${ctx.column} IS NULL THEN 1 ELSE 0 END) AS nulls_count
|
||||
${ctx.isFloatColumn ? `,
|
||||
sum(
|
||||
CASE
|
||||
WHEN ${ctx._aggregationColumn} = 'infinity'::float OR ${ctx._aggregationColumn} = '-infinity'::float
|
||||
WHEN ${ctx.aggregationColumn} = 'infinity'::float OR ${ctx.aggregationColumn} = '-infinity'::float
|
||||
THEN 1
|
||||
ELSE 0
|
||||
END
|
||||
) AS infinities_count,
|
||||
sum(CASE WHEN ${ctx._aggregationColumn} = 'NaN'::float THEN 1 ELSE 0 END) AS nans_count` :
|
||||
sum(CASE WHEN ${ctx.aggregationColumn} = 'NaN'::float THEN 1 ELSE 0 END) AS nans_count` :
|
||||
''
|
||||
}
|
||||
FROM (${ctx._query}) _cdb_aggregation_nulls
|
||||
FROM (${ctx.query}) _cdb_aggregation_nulls
|
||||
)
|
||||
`;
|
||||
|
||||
const rankedCategoriesQueryTpl = ctx => `
|
||||
categories AS(
|
||||
SELECT
|
||||
${ctx._column} AS category,
|
||||
${ctx._aggregation} AS value,
|
||||
row_number() OVER (ORDER BY ${ctx._aggregation} desc) as rank
|
||||
${ctx.column} AS category,
|
||||
${ctx.aggregationFn} AS value,
|
||||
row_number() OVER (ORDER BY ${ctx.aggregationFn} desc) as rank
|
||||
FROM filtered_source
|
||||
${ctx._aggregationColumn !== null ? `WHERE ${ctx._aggregationColumn} IS NOT NULL` : ''}
|
||||
GROUP BY ${ctx._column}
|
||||
${ctx.aggregationColumn !== null ? `WHERE ${ctx.aggregationColumn} IS NOT NULL` : ''}
|
||||
GROUP BY ${ctx.column}
|
||||
ORDER BY 2 DESC
|
||||
)
|
||||
`;
|
||||
@@ -63,9 +63,9 @@ const categoriesSummaryCountQueryTpl = ctx => `
|
||||
categories_summary_count AS(
|
||||
SELECT count(1) AS categories_count
|
||||
FROM (
|
||||
SELECT ${ctx._column} AS category
|
||||
FROM (${ctx._query}) _cdb_categories
|
||||
GROUP BY ${ctx._column}
|
||||
SELECT ${ctx.column} AS category
|
||||
FROM (${ctx.query}) _cdb_categories
|
||||
GROUP BY ${ctx.column}
|
||||
) _cdb_categories_count
|
||||
)
|
||||
`;
|
||||
@@ -82,43 +82,43 @@ const rankedAggregationQueryTpl = ctx => `
|
||||
max_val,
|
||||
count,
|
||||
categories_count
|
||||
${ctx._isFloatColumn ? `${specialNumericValuesColumns(ctx)}` : '' }
|
||||
${ctx.isFloatColumn ? `${specialNumericValuesColumns(ctx)}` : '' }
|
||||
FROM categories, summary, categories_summary_min_max, categories_summary_count
|
||||
WHERE rank < ${ctx._limit}
|
||||
WHERE rank < ${ctx.limit}
|
||||
UNION ALL
|
||||
SELECT
|
||||
'Other' category,
|
||||
${ctx._aggregationFn}(value) as value,
|
||||
${ctx.aggregation !== 'count' ? ctx.aggregation : 'sum'}(value) as value,
|
||||
true as agg,
|
||||
nulls_count,
|
||||
min_val,
|
||||
max_val,
|
||||
count,
|
||||
categories_count
|
||||
${ctx._isFloatColumn ? `${specialNumericValuesColumns(ctx)}` : '' }
|
||||
${ctx.isFloatColumn ? `${specialNumericValuesColumns(ctx)}` : '' }
|
||||
FROM categories, summary, categories_summary_min_max, categories_summary_count
|
||||
WHERE rank >= ${ctx._limit}
|
||||
WHERE rank >= ${ctx.limit}
|
||||
GROUP BY
|
||||
nulls_count,
|
||||
min_val,
|
||||
max_val,
|
||||
count,
|
||||
categories_count
|
||||
${ctx._isFloatColumn ? `${specialNumericValuesColumns(ctx)}` : '' }
|
||||
${ctx.isFloatColumn ? `${specialNumericValuesColumns(ctx)}` : '' }
|
||||
`;
|
||||
|
||||
const aggregationQueryTpl = ctx => `
|
||||
SELECT
|
||||
CAST(${ctx._column} AS text) AS category,
|
||||
${ctx._aggregation} AS value,
|
||||
CAST(${ctx.column} AS text) AS category,
|
||||
${ctx.aggregationFn} AS value,
|
||||
false as agg,
|
||||
nulls_count,
|
||||
min_val,
|
||||
max_val,
|
||||
count,
|
||||
categories_count
|
||||
${ctx._isFloatColumn ? `${specialNumericValuesColumns(ctx)}` : '' }
|
||||
FROM (${ctx._query}) _cdb_aggregation_all, summary, categories_summary_min_max, categories_summary_count
|
||||
${ctx.isFloatColumn ? `${specialNumericValuesColumns(ctx)}` : '' }
|
||||
FROM (${ctx.query}) _cdb_aggregation_all, summary, categories_summary_min_max, categories_summary_count
|
||||
GROUP BY
|
||||
category,
|
||||
nulls_count,
|
||||
@@ -126,73 +126,45 @@ const aggregationQueryTpl = ctx => `
|
||||
max_val,
|
||||
count,
|
||||
categories_count
|
||||
${ctx._isFloatColumn ? `${specialNumericValuesColumns(ctx)}` : '' }
|
||||
${ctx.isFloatColumn ? `${specialNumericValuesColumns(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
|
||||
isFloatColumn: ctx.isFloatColumn,
|
||||
query: ctx.query,
|
||||
column: ctx.column,
|
||||
aggregationColumn: ctx.aggregationColumn
|
||||
})},
|
||||
${summaryQueryTpl({
|
||||
_isFloatColumn: ctx.isFloatColumn,
|
||||
_query: ctx.query,
|
||||
_column: ctx.column,
|
||||
_aggregationColumn: ctx.aggregation !== 'count' ? ctx.aggregationColumn : null
|
||||
isFloatColumn: ctx.isFloatColumn,
|
||||
query: ctx.query,
|
||||
column: ctx.column,
|
||||
aggregationColumn: ctx.aggregationColumn
|
||||
})},
|
||||
${rankedCategoriesQueryTpl({
|
||||
_query: ctx.query,
|
||||
_column: ctx.column,
|
||||
_aggregation: aggregationFnQueryTpl({
|
||||
aggregation: ctx.aggregation,
|
||||
aggregationColumn: ctx.aggregationColumn || 1
|
||||
}),
|
||||
_aggregationColumn: ctx.aggregation !== 'count' ? ctx.aggregationColumn : null
|
||||
query: ctx.query,
|
||||
column: ctx.column,
|
||||
aggregationFn: ctx.aggregationFn,
|
||||
aggregationColumn: ctx.aggregationColumn
|
||||
})},
|
||||
${categoriesSummaryMinMaxQueryTpl({
|
||||
_query: ctx.query,
|
||||
_column: ctx.column
|
||||
query: ctx.query,
|
||||
column: ctx.column
|
||||
})},
|
||||
${categoriesSummaryCountQueryTpl({
|
||||
_query: ctx.query,
|
||||
_column: ctx.column
|
||||
query: ctx.query,
|
||||
column: ctx.column
|
||||
})}
|
||||
`;
|
||||
|
||||
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
|
||||
})}
|
||||
`}
|
||||
${categoriesCTESqlTpl(ctx)}
|
||||
${!!ctx.override.ownFilter ? `${aggregationQueryTpl(ctx)}` : `${rankedAggregationQueryTpl(ctx)}`}
|
||||
`;
|
||||
|
||||
const CATEGORIES_LIMIT = 6;
|
||||
@@ -275,8 +247,13 @@ Aggregation.prototype.sql = function(psql, override, callback) {
|
||||
query: this.query,
|
||||
column: this.column,
|
||||
aggregation: this.aggregation,
|
||||
aggregationColumn: this.aggregationColumn,
|
||||
isFloatColumn: this._isFloatColumn
|
||||
aggregationColumn: this.aggregation !== 'count' ? this.aggregationColumn : null,
|
||||
aggregationFn: aggregationFnQueryTpl({
|
||||
aggregation: this.aggregation,
|
||||
aggregationColumn: this.aggregationColumn || 1
|
||||
}),
|
||||
isFloatColumn: this._isFloatColumn,
|
||||
limit: CATEGORIES_LIMIT
|
||||
});
|
||||
|
||||
debug(aggregationSql);
|
||||
|
||||
Reference in New Issue
Block a user