2018-10-23 23:45:42 +08:00
|
|
|
'use strict';
|
|
|
|
|
2016-05-13 00:30:10 +08:00
|
|
|
var _ = require('underscore');
|
2019-10-07 16:07:25 +08:00
|
|
|
var TableNameParser = require('./table-name-parser');
|
2016-02-04 17:26:31 +08:00
|
|
|
|
2016-05-17 19:55:00 +08:00
|
|
|
var BBoxFilter = require('../models/filter/bbox');
|
2016-07-07 03:11:39 +08:00
|
|
|
var AnalysisFilter = require('../models/filter/analysis');
|
2016-05-17 19:55:00 +08:00
|
|
|
|
2016-05-17 21:41:31 +08:00
|
|
|
// Minimim number of filtered rows to use overviews
|
|
|
|
var FILTER_MIN_ROWS = 65536;
|
|
|
|
// Maximum filtered fraction to not apply overviews
|
|
|
|
var FILTER_MAX_FRACTION = 0.2;
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function apply_filters_to_query (query, filters, bbox_filter) {
|
|
|
|
if (filters && !_.isEmpty(filters)) {
|
2016-07-07 03:11:39 +08:00
|
|
|
var analysisFilter = new AnalysisFilter(filters);
|
|
|
|
query = analysisFilter.sql(query);
|
2016-05-14 00:46:58 +08:00
|
|
|
}
|
2019-10-22 01:07:24 +08:00
|
|
|
if (bbox_filter) {
|
2016-05-17 19:55:00 +08:00
|
|
|
var bboxFilter = new BBoxFilter(bbox_filter.options, bbox_filter.params);
|
|
|
|
query = bboxFilter.sql(query);
|
2016-05-14 02:47:36 +08:00
|
|
|
}
|
2016-05-14 00:46:58 +08:00
|
|
|
return query;
|
|
|
|
}
|
2016-05-13 00:30:10 +08:00
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function OverviewsQueryRewriter (options) {
|
2016-02-04 17:26:31 +08:00
|
|
|
this.options = options;
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = OverviewsQueryRewriter;
|
|
|
|
|
|
|
|
// TODO: some names are introudced in the queries, and the
|
|
|
|
// '_vovw_' (for vector overviews) is used in them, but no check
|
|
|
|
// is performed for conflicts with existing identifiers in the query.
|
|
|
|
|
|
|
|
// Build UNION expression to replace table, using overviews metadata
|
|
|
|
// overviews metadata: { 1: 'table_ov1', ... }
|
|
|
|
// assume table and overview names include schema if necessary and are quoted as needed
|
2019-10-22 01:07:24 +08:00
|
|
|
function overviews_view_for_table (table, overviews_metadata, indent) {
|
2016-02-04 17:26:31 +08:00
|
|
|
var condition, i, len, ov_table, overview_layers, selects, z_hi, z_lo;
|
|
|
|
var parsed_table = TableNameParser.parse(table);
|
|
|
|
|
|
|
|
var sorted_overviews = []; // [[1, 'table_ov1'], ...]
|
|
|
|
|
|
|
|
indent = indent || ' ';
|
|
|
|
for (var z in overviews_metadata) {
|
2016-04-20 04:50:05 +08:00
|
|
|
if (overviews_metadata.hasOwnProperty(z) && z !== 'schema') {
|
2016-02-04 17:26:31 +08:00
|
|
|
sorted_overviews.push([z, overviews_metadata[z].table]);
|
|
|
|
}
|
|
|
|
}
|
2019-10-22 01:07:24 +08:00
|
|
|
sorted_overviews.sort(function (a, b) { return a[0] - b[0]; });
|
2016-02-04 17:26:31 +08:00
|
|
|
|
|
|
|
overview_layers = [];
|
|
|
|
z_lo = null;
|
|
|
|
for (i = 0, len = sorted_overviews.length; i < len; i++) {
|
|
|
|
z_hi = parseInt(sorted_overviews[i][0]);
|
|
|
|
ov_table = sorted_overviews[i][1];
|
|
|
|
overview_layers.push([overview_z_condition(z_lo, z_hi), ov_table]);
|
|
|
|
z_lo = z_hi;
|
|
|
|
}
|
2019-10-22 01:07:24 +08:00
|
|
|
overview_layers.push(['_vovw_z > ' + z_lo, table]);
|
2016-02-04 17:26:31 +08:00
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
selects = overview_layers.map(function (condition_table) {
|
2016-04-20 23:47:43 +08:00
|
|
|
condition = condition_table[0];
|
|
|
|
ov_table = TableNameParser.parse(condition_table[1]);
|
|
|
|
ov_table.schema = ov_table.schema || parsed_table.schema;
|
|
|
|
var ov_identifier = TableNameParser.table_identifier(ov_table);
|
2019-10-22 01:07:24 +08:00
|
|
|
return indent + 'SELECT * FROM ' + ov_identifier + ', _vovw_scale WHERE ' + condition;
|
2016-02-04 17:26:31 +08:00
|
|
|
});
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
return selects.join('\n' + indent + 'UNION ALL\n');
|
2016-02-04 17:26:31 +08:00
|
|
|
}
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function overview_z_condition (z_lo, z_hi) {
|
2016-02-04 17:26:31 +08:00
|
|
|
if (z_lo !== null) {
|
|
|
|
if (z_lo === z_hi - 1) {
|
2019-10-22 01:07:24 +08:00
|
|
|
return '_vovw_z = ' + z_hi;
|
2016-02-04 17:26:31 +08:00
|
|
|
} else {
|
2019-10-22 01:07:24 +08:00
|
|
|
return '_vovw_z > ' + z_lo + ' AND _vovw_z <= ' + z_hi;
|
2016-02-04 17:26:31 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (z_hi === 0) {
|
2019-10-22 01:07:24 +08:00
|
|
|
return '_vovw_z = ' + z_hi;
|
2016-02-04 17:26:31 +08:00
|
|
|
} else {
|
2019-10-22 01:07:24 +08:00
|
|
|
return '_vovw_z <= ' + z_hi;
|
2016-02-04 17:26:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// name to be used for the view of the table using overviews
|
2019-10-22 01:07:24 +08:00
|
|
|
function overviews_view_name (table) {
|
2016-02-04 17:26:31 +08:00
|
|
|
var parsed_table = TableNameParser.parse(table);
|
|
|
|
parsed_table.table = '_vovw_' + parsed_table.table;
|
|
|
|
parsed_table.schema = null;
|
|
|
|
return TableNameParser.table_identifier(parsed_table);
|
|
|
|
}
|
|
|
|
|
|
|
|
// replace a table name in a query by anoter name
|
2019-10-22 01:07:24 +08:00
|
|
|
function replace_table_in_query (sql, old_table_name, replacement) {
|
2016-02-04 17:26:31 +08:00
|
|
|
var old_table = TableNameParser.parse(old_table_name);
|
|
|
|
var old_table_ident = TableNameParser.table_identifier(old_table);
|
|
|
|
|
|
|
|
// regular expression prefix (beginning) to match a table name
|
2019-10-22 01:07:24 +08:00
|
|
|
function pattern_prefix (schema, identifier) {
|
|
|
|
if (schema) {
|
2016-02-04 17:26:31 +08:00
|
|
|
// to match a table name including schema prefix
|
|
|
|
// name should not be part of another name, so we require
|
|
|
|
// to start a at a word boundary
|
2019-10-22 01:07:24 +08:00
|
|
|
if (identifier[0] !== '"') {
|
2016-02-04 17:26:31 +08:00
|
|
|
return '\\b';
|
|
|
|
} else {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
} else {
|
2016-04-20 23:47:43 +08:00
|
|
|
// to match a table name without schema
|
|
|
|
// name should not begin right after a dot (i.e. have a explicit schema)
|
|
|
|
// nor be part of another name
|
|
|
|
// since the pattern matches the first character of the table
|
|
|
|
// it must be put back in the replacement text
|
2019-10-22 01:07:24 +08:00
|
|
|
replacement = '$01' + replacement;
|
2016-04-20 23:47:43 +08:00
|
|
|
return '([^\.a-z0-9_]|^)';
|
2016-02-04 17:26:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// regular expression suffix (ending) to match a table name
|
2019-10-22 01:07:24 +08:00
|
|
|
function pattern_suffix (identifier) {
|
2016-02-04 17:26:31 +08:00
|
|
|
// name shouldn't be the prefix of a longer name
|
2019-10-22 01:07:24 +08:00
|
|
|
if (identifier[identifier.length - 1] !== '"') {
|
2016-02-04 17:26:31 +08:00
|
|
|
return '\\b';
|
|
|
|
} else {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// regular expression to match a table name
|
|
|
|
var regexp = pattern_prefix(old_table.schema, old_table_ident) +
|
|
|
|
old_table_ident +
|
|
|
|
pattern_suffix(old_table_ident);
|
|
|
|
|
|
|
|
// replace all occurrences of the table pattern
|
|
|
|
return sql.replace(new RegExp(regexp, 'g'), replacement);
|
|
|
|
}
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function replace_table_in_query_with_schema (query, table, schema, replacement) {
|
|
|
|
if (replacement) {
|
2016-05-13 00:30:10 +08:00
|
|
|
query = replace_table_in_query(query, table, replacement);
|
|
|
|
var parsed_table = TableNameParser.parse(table);
|
|
|
|
if (!parsed_table.schema && schema) {
|
|
|
|
// replace also the qualified table name, if the table wasn't qualified
|
|
|
|
parsed_table.schema = schema;
|
|
|
|
table = TableNameParser.table_identifier(parsed_table);
|
|
|
|
query = replace_table_in_query(query, table, replacement);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return query;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build query to use overviews for a variant zoom level (given by a expression to
|
|
|
|
// be evaluated by the database server)
|
2019-10-22 01:07:24 +08:00
|
|
|
function overviews_query_with_zoom_expression (query, overviews, zoom_level_expression) {
|
2016-02-04 17:26:31 +08:00
|
|
|
var replaced_query = query;
|
2019-10-22 01:07:24 +08:00
|
|
|
var sql = 'WITH\n _vovw_scale AS ( SELECT ' + zoom_level_expression + ' AS _vovw_z )';
|
2016-03-03 02:25:08 +08:00
|
|
|
var replacement;
|
2019-10-22 01:07:24 +08:00
|
|
|
_.each(Object.keys(overviews), function (table) {
|
2016-05-13 00:30:10 +08:00
|
|
|
var table_overviews = overviews[table];
|
|
|
|
var table_view = overviews_view_name(table);
|
|
|
|
var schema = table_overviews.schema;
|
2019-10-22 01:07:24 +08:00
|
|
|
replacement = '(\n' + overviews_view_for_table(table, table_overviews) + '\n ) AS ' + table_view;
|
2016-05-13 00:30:10 +08:00
|
|
|
replaced_query = replace_table_in_query_with_schema(replaced_query, table, schema, replacement);
|
|
|
|
});
|
2019-10-22 01:07:24 +08:00
|
|
|
if (replaced_query !== query) {
|
|
|
|
sql += '\n';
|
2016-02-04 17:26:31 +08:00
|
|
|
sql += replaced_query;
|
|
|
|
} else {
|
|
|
|
sql = query;
|
|
|
|
}
|
|
|
|
return sql;
|
|
|
|
}
|
|
|
|
|
2016-05-13 00:30:10 +08:00
|
|
|
// Build query to use overviews for a specific zoom level value
|
2019-10-22 01:07:24 +08:00
|
|
|
function overviews_query_with_definite_zoom (query, overviews, zoom_level) {
|
2016-05-13 00:30:10 +08:00
|
|
|
var replaced_query = query;
|
|
|
|
var replacement;
|
2019-10-22 01:07:24 +08:00
|
|
|
_.each(Object.keys(overviews), function (table) {
|
2016-05-13 00:30:10 +08:00
|
|
|
var table_overviews = overviews[table];
|
|
|
|
var schema = table_overviews.schema;
|
|
|
|
replacement = overview_table_for_zoom_level(table_overviews, zoom_level);
|
|
|
|
replaced_query = replace_table_in_query_with_schema(replaced_query, table, schema, replacement);
|
|
|
|
});
|
|
|
|
return replaced_query;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find a suitable overview table for a specific zoom_level
|
2019-10-22 01:07:24 +08:00
|
|
|
function overview_table_for_zoom_level (table_overviews, zoom_level) {
|
2016-05-13 00:30:10 +08:00
|
|
|
var overview_table;
|
2019-10-22 01:07:24 +08:00
|
|
|
if (table_overviews) {
|
2016-05-13 00:30:10 +08:00
|
|
|
overview_table = table_overviews[zoom_level];
|
2019-10-22 01:07:24 +08:00
|
|
|
if (!overview_table) {
|
|
|
|
_.every(Object.keys(table_overviews).sort(function (x, y) { return x - y; }), function (overview_zoom) {
|
|
|
|
if (+overview_zoom > +zoom_level) {
|
|
|
|
overview_table = table_overviews[overview_zoom];
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
2016-05-13 00:30:10 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2019-10-22 01:07:24 +08:00
|
|
|
if (overview_table) {
|
2016-05-13 00:30:10 +08:00
|
|
|
overview_table = overview_table.table;
|
|
|
|
}
|
|
|
|
return overview_table;
|
|
|
|
}
|
|
|
|
|
2016-02-04 17:26:31 +08:00
|
|
|
// Transform an SQL query so that it uses overviews.
|
|
|
|
//
|
|
|
|
// For a given query `SELECT * FROM table`, if any of tables in it
|
|
|
|
// has overviews as defined by the provided metadat, the query will
|
|
|
|
// be transform into something similar to this:
|
|
|
|
//
|
|
|
|
// WITH _vovw_scale AS ( ... ), -- define scale level
|
2016-05-18 14:04:11 +08:00
|
|
|
// SELECT * FROM -- in the query the table is replaced by:
|
|
|
|
// ( ... ) AS _vovw_table -- a union of overviews and base table
|
2016-02-04 17:26:31 +08:00
|
|
|
//
|
2016-05-18 14:04:11 +08:00
|
|
|
// The data argument has the form:
|
|
|
|
// {
|
|
|
|
// overviews: // overview tables metadata
|
|
|
|
// { 'table-name': {1: { table: 'overview-table-1' }, ... }, ... },
|
|
|
|
// zoom_level: ..., // optional zoom level
|
|
|
|
// filters: ..., // filters definition
|
|
|
|
// unfiltered_query: ..., // query without the filters
|
|
|
|
// bbox_filter: ... // bounding-box filter
|
|
|
|
// }
|
2019-10-22 01:07:24 +08:00
|
|
|
OverviewsQueryRewriter.prototype.query = function (query, data, options) {
|
2016-05-13 00:30:10 +08:00
|
|
|
options = options || {};
|
2019-10-22 01:07:24 +08:00
|
|
|
data = data || {};
|
2016-05-13 00:30:10 +08:00
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
var overviews = data.overviews;
|
2016-05-18 21:48:30 +08:00
|
|
|
var unfiltered_query = data.unfiltered_query;
|
2019-10-22 01:07:24 +08:00
|
|
|
var filters = data.filters;
|
|
|
|
var bbox_filter = data.bbox_filter;
|
2016-05-13 00:30:10 +08:00
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
if (!unfiltered_query) {
|
2016-05-13 00:30:10 +08:00
|
|
|
unfiltered_query = query;
|
|
|
|
}
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
if (!should_use_overviews(unfiltered_query, data)) {
|
2016-02-04 17:26:31 +08:00
|
|
|
return query;
|
|
|
|
}
|
2016-05-13 00:30:10 +08:00
|
|
|
|
|
|
|
var rewritten_query;
|
|
|
|
|
|
|
|
var zoom_level_expression = this.options.zoom_level;
|
2016-05-13 00:47:24 +08:00
|
|
|
var zoom_level = zoom_level_for_query(unfiltered_query, zoom_level_expression, options);
|
2016-05-13 00:30:10 +08:00
|
|
|
|
2016-05-13 00:47:24 +08:00
|
|
|
rewritten_query = overviews_query(unfiltered_query, overviews, zoom_level, zoom_level_expression);
|
2016-05-13 00:30:10 +08:00
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
if (rewritten_query === unfiltered_query) {
|
2016-05-13 00:30:10 +08:00
|
|
|
// could not or didn't need to alter the query
|
|
|
|
rewritten_query = query;
|
|
|
|
} else {
|
2016-05-14 02:47:36 +08:00
|
|
|
rewritten_query = apply_filters_to_query(rewritten_query, filters, bbox_filter);
|
2016-05-13 00:30:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return rewritten_query;
|
2016-02-04 17:26:31 +08:00
|
|
|
};
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function zoom_level_for_query (query, zoom_level_expression, options) {
|
2016-05-13 00:47:24 +08:00
|
|
|
var zoom_level = null;
|
2019-10-22 01:07:24 +08:00
|
|
|
if (_.has(options, 'zoom_level')) {
|
2016-05-14 00:46:58 +08:00
|
|
|
zoom_level = options.zoom_level || '0';
|
2016-05-13 00:47:24 +08:00
|
|
|
}
|
2019-10-22 01:07:24 +08:00
|
|
|
if (zoom_level === null && !zoom_level_expression) {
|
2016-05-13 00:47:24 +08:00
|
|
|
zoom_level = '0';
|
|
|
|
}
|
|
|
|
return zoom_level;
|
|
|
|
}
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function overviews_query (query, overviews, zoom_level, zoom_level_expression) {
|
|
|
|
if (zoom_level || zoom_level === '0' || zoom_level === 0) {
|
2016-05-13 00:47:24 +08:00
|
|
|
return overviews_query_with_definite_zoom(query, overviews, zoom_level);
|
|
|
|
} else {
|
|
|
|
return overviews_query_with_zoom_expression(query, overviews, zoom_level_expression);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function should_use_overviews (query, data) {
|
2016-05-17 21:41:31 +08:00
|
|
|
data = data || {};
|
|
|
|
var use_overviews = data.overviews && is_supported_query(query);
|
2019-10-22 01:07:24 +08:00
|
|
|
if (use_overviews && data.filters && data.filter_stats) {
|
2016-05-17 21:46:13 +08:00
|
|
|
var filtered_rows = data.filter_stats.filtered_rows;
|
|
|
|
var unfiltered_rows = data.filter_stats.unfiltered_rows;
|
2019-10-22 01:07:24 +08:00
|
|
|
if (unfiltered_rows && (filtered_rows || filtered_rows === 0)) {
|
2016-05-17 21:46:13 +08:00
|
|
|
use_overviews = filtered_rows >= FILTER_MIN_ROWS ||
|
2019-10-22 01:07:24 +08:00
|
|
|
(filtered_rows / unfiltered_rows) > FILTER_MAX_FRACTION;
|
2016-05-17 21:41:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return use_overviews;
|
|
|
|
}
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function is_supported_query (sql) {
|
2016-04-20 23:47:43 +08:00
|
|
|
var basic_query =
|
|
|
|
/\s*SELECT\s+[\*a-z0-9_,\s]+?\s+FROM\s+((\"[^"]+\"|[a-z0-9_]+)\.)?(\"[^"]+\"|[a-z0-9_]+)\s*;?\s*/i;
|
2019-10-22 01:07:24 +08:00
|
|
|
var unwrapped_query = new RegExp('^' + basic_query.source + '$', 'i');
|
2016-04-20 23:47:43 +08:00
|
|
|
// queries for named maps are wrapped like this:
|
|
|
|
var wrapped_query = new RegExp(
|
2019-10-22 01:07:24 +08:00
|
|
|
'^\\s*SELECT\\s+\\*\\s+FROM\\s+\\(' +
|
2016-04-20 23:47:43 +08:00
|
|
|
basic_query.source +
|
2019-10-22 01:07:24 +08:00
|
|
|
'\\)\\s+AS\\s+wrapped_query\\s+WHERE\\s+\\d+=1\\s*$',
|
2016-04-20 23:47:43 +08:00
|
|
|
'i'
|
|
|
|
);
|
|
|
|
return !!(sql.match(unwrapped_query) || sql.match(wrapped_query));
|
2016-05-17 21:41:31 +08:00
|
|
|
}
|