Use template strings to build histogram query

This commit is contained in:
Daniel García Aubert 2017-09-06 18:13:34 +02:00
parent 9f52e58be8
commit 1ac8455dc2

View File

@ -366,103 +366,45 @@ module.exports = class Histogram extends BaseDataview {
}
_buildQuery (psql, override, callback) {
var filteredQuery, basicsQuery, binsQuery;
var _column = this.column;
var _query = this.query;
if (this.isDateHistogram(override)) {
return this._buildDateHistogramQuery(psql, override, callback);
}
if (this._columnType === 'date') {
_column = columnCastTpl({column: _column});
}
filteredQuery = filteredQueryTpl({
const histogramSql = this.buildNumericHistogramQueryTpl({
_override: override,
_column: this._columnType === 'date' ? columnCastTpl({ column: this.column }) : this.column,
_isFloatColumn: this._columnType === 'float',
_query: _query,
_column: _column
});
if (this._shouldOverride(override)) {
debug('overriding with %j', override);
basicsQuery = overrideBasicsQueryTpl({
_query: _query,
_column: _column,
_query: this.query,
_start: this.getBinStart(override),
_end: this.getBinEnd(override)
});
binsQuery = [
overrideBinsQueryTpl({
_bins: override.bins
})
].join(',\n');
} else {
basicsQuery = basicsQueryTpl({
_query: _query,
_column: _column
});
if (this._shouldOverrideBins(override)) {
binsQuery = [
overrideBinsQueryTpl({
_bins: override.bins
})
].join(',\n');
} else {
binsQuery = [
iqrQueryTpl({
_query: _query,
_column: _column
}),
binsQueryTpl({
_query: _query,
_end: this.getBinEnd(override),
_minBins: BIN_MIN_NUMBER,
_maxBins: BIN_MAX_NUMBER
})
].join(',\n');
}
}
var cteSql = [
filteredQuery,
basicsQuery,
binsQuery,
nullsQueryTpl({
_query: _query,
_column: _column
})
];
if (this._columnType === 'float') {
cteSql.push(
infinitiesQueryTpl({
_query: _query,
_column: _column
}),
nansQueryTpl({
_query: _query,
_column: _column
})
);
}
var histogramSql = [
"WITH",
cteSql.join(',\n'),
histogramQueryTpl({
_isFloatColumn: this._columnType === 'float',
_query: _query,
_column: _column
})
].join('\n');
_maxBins: BIN_MAX_NUMBER,
_bins: override.bins
});
debug(histogramSql);
return callback(null, histogramSql);
}
buildNumericHistogramQueryTpl (ctx) {
return `
WITH
${filteredQueryTpl(ctx)},
${this._shouldOverride(ctx._override) ? overrideBasicsQueryTpl(ctx) : basicsQueryTpl(ctx)},
${this._shouldOverride(ctx._override) ?
overrideBinsQueryTpl(ctx) :
this._shouldOverrideBins(ctx._override) ?
overrideBinsQueryTpl(ctx) :
`${iqrQueryTpl(ctx)}, ${binsQueryTpl(ctx)}`
},
${nullsQueryTpl(ctx)}
${ctx._isFloatColumn ? `,${infinitiesQueryTpl(ctx)}, ${nansQueryTpl(ctx)}` : ''}
${histogramQueryTpl(ctx)}
`;
}
_shouldOverride (override) {
return override &&
override.hasOwnProperty('start') &&