Support unneeded schema names in overviews queries

Fixes #420
Keep table schema of overviews base tables and use it
to support queries that use the schema name when not
strictly needed.
This commit is contained in:
Javier Goizueta 2016-04-19 22:50:05 +02:00
parent 6b942ecf0b
commit 7902b276ad
7 changed files with 91 additions and 2 deletions

View File

@ -22,7 +22,10 @@ function prepareSql(sql) {
} }
OverviewsMetadataApi.prototype.getOverviewsMetadata = function (username, sql, callback) { OverviewsMetadataApi.prototype.getOverviewsMetadata = function (username, sql, callback) {
var query = 'SELECT * FROM CDB_Overviews(CDB_QueryTablesText($windshaft$' + prepareSql(sql) + '$windshaft$))'; // FIXME: Currently using internal function parsed_table
// CDB_Overviews should provide the schema information directly.
var query = 'SELECT *, _cdb_schema_name(base_table)' +
' FROM CDB_Overviews(CDB_QueryTablesText($windshaft$' + prepareSql(sql) + '$windshaft$))';
this.pgQueryRunner.run(username, query, function handleOverviewsRows(err, rows) { this.pgQueryRunner.run(username, query, function handleOverviewsRows(err, rows) {
if (err){ if (err){
callback(err); callback(err);
@ -31,11 +34,13 @@ OverviewsMetadataApi.prototype.getOverviewsMetadata = function (username, sql, c
var metadata = {}; var metadata = {};
rows.forEach(function(row) { rows.forEach(function(row) {
var table = row.base_table; var table = row.base_table;
var schema = row._cdb_schema_name;
var table_metadata = metadata[table]; var table_metadata = metadata[table];
if ( !table_metadata ) { if ( !table_metadata ) {
table_metadata = metadata[table] = {}; table_metadata = metadata[table] = {};
} }
table_metadata[row.z] = { table: row.overview_table }; table_metadata[row.z] = { table: row.overview_table };
table_metadata.schema = schema;
}); });
return callback(null, metadata); return callback(null, metadata);
}); });

View File

@ -22,7 +22,7 @@ function overviews_view_for_table(table, overviews_metadata, indent) {
indent = indent || ' '; indent = indent || ' ';
for (var z in overviews_metadata) { for (var z in overviews_metadata) {
if (overviews_metadata.hasOwnProperty(z)) { if (overviews_metadata.hasOwnProperty(z) && z !== 'schema') {
sorted_overviews.push([z, overviews_metadata[z].table]); sorted_overviews.push([z, overviews_metadata[z].table]);
} }
} }
@ -127,8 +127,16 @@ function overviews_query(query, overviews, zoom_level_expression) {
if (overviews.hasOwnProperty(table)) { if (overviews.hasOwnProperty(table)) {
var table_overviews = overviews[table]; var table_overviews = overviews[table];
var table_view = overviews_view_name(table); var table_view = overviews_view_name(table);
var schema = table_overviews.schema;
replacement = "(\n" + overviews_view_for_table(table, table_overviews) + "\n ) AS " + table_view; replacement = "(\n" + overviews_view_for_table(table, table_overviews) + "\n ) AS " + table_view;
replaced_query = replace_table_in_query(replaced_query, table, replacement); replaced_query = replace_table_in_query(replaced_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);
replaced_query = replace_table_in_query(replaced_query, table, replacement);
}
} }
} }
if ( replaced_query !== query ) { if ( replaced_query !== query ) {

View File

@ -90,6 +90,7 @@ describe('overviews metadata', function() {
var expected_data = { var expected_data = {
overviews: { overviews: {
test_table_overviews: { test_table_overviews: {
schema: 'public',
1: { table: '_vovw_1_test_table_overviews' }, 1: { table: '_vovw_1_test_table_overviews' },
2: { table: '_vovw_2_test_table_overviews' } 2: { table: '_vovw_2_test_table_overviews' }
} }

View File

@ -136,6 +136,7 @@ describe('overviews metadata for named maps', function() {
var expected_data = { var expected_data = {
overviews: { overviews: {
test_table_overviews: { test_table_overviews: {
schema: 'public',
1: { table: '_vovw_1_test_table_overviews' }, 1: { table: '_vovw_1_test_table_overviews' },
2: { table: '_vovw_2_test_table_overviews' } 2: { table: '_vovw_2_test_table_overviews' }
} }

View File

@ -75,6 +75,7 @@ describe('MapConfigOverviewsAdapter', function() {
var expected_data = { var expected_data = {
overviews: { overviews: {
test_table_overviews: { test_table_overviews: {
schema: 'public',
1: { table: '_vovw_1_test_table_overviews' }, 1: { table: '_vovw_1_test_table_overviews' },
2: { table: '_vovw_2_test_table_overviews' } 2: { table: '_vovw_2_test_table_overviews' }
} }

View File

@ -40,6 +40,7 @@ describe('OverviewsMetadataApi', function() {
assert.deepEqual(result, { assert.deepEqual(result, {
'test_table_overviews': { 'test_table_overviews': {
schema: 'public',
1: { table: '_vovw_1_test_table_overviews' }, 1: { table: '_vovw_1_test_table_overviews' },
2: { table: '_vovw_2_test_table_overviews' } 2: { table: '_vovw_2_test_table_overviews' }
} }

View File

@ -202,6 +202,78 @@ describe('Overviews query rewriter', function() {
done(); done();
}); });
it('uses schema name from overviews', function(done){
var sql = "SELECT * FROM public.table1";
var data = {
overviews: {
'table1': {
schema: 'public',
2: { table: 'table1_ov2' }
}
}
};
var overviews_sql = overviewsQueryRewriter.query(sql, data);
var expected_sql = "\
WITH\
_vovw_scale AS ( SELECT ZoomLevel() AS _vovw_z )\
SELECT * FROM (\
SELECT * FROM table1_ov2, _vovw_scale WHERE _vovw_z <= 2\
UNION ALL\
SELECT * FROM table1, _vovw_scale WHERE _vovw_z > 2\
) AS _vovw_table1\
";
assertSameSql(overviews_sql, expected_sql);
done();
});
it('ignores schema name from overviews if not necessary', function(done){
var sql = "SELECT * FROM table1";
var data = {
overviews: {
'table1': {
schema: 'public',
2: { table: 'table1_ov2' }
}
}
};
var overviews_sql = overviewsQueryRewriter.query(sql, data);
var expected_sql = "\
WITH\
_vovw_scale AS ( SELECT ZoomLevel() AS _vovw_z )\
SELECT * FROM (\
SELECT * FROM table1_ov2, _vovw_scale WHERE _vovw_z <= 2\
UNION ALL\
SELECT * FROM table1, _vovw_scale WHERE _vovw_z > 2\
) AS _vovw_table1\
";
assertSameSql(overviews_sql, expected_sql);
done();
});
it('uses redundant schema information', function(done){
var sql = "SELECT * FROM public.table1";
var data = {
overviews: {
'public.table1': {
schema: 'public',
2: { table: 'table1_ov2' }
}
}
};
var overviews_sql = overviewsQueryRewriter.query(sql, data);
var expected_sql = "\
WITH\
_vovw_scale AS ( SELECT ZoomLevel() AS _vovw_z )\
SELECT * FROM (\
SELECT * FROM public.table1_ov2, _vovw_scale WHERE _vovw_z <= 2\
UNION ALL\
SELECT * FROM public.table1, _vovw_scale WHERE _vovw_z > 2\
) AS _vovw_table1\
";
assertSameSql(overviews_sql, expected_sql);
done();
});
it('generates query for a table that needs quoting with explicit schema', function(done){ it('generates query for a table that needs quoting with explicit schema', function(done){
var sql = "SELECT * FROM public.\"table 1\""; var sql = "SELECT * FROM public.\"table 1\"";
var data = { var data = {