From 474de01757eaf73a8cca326d966b35462ffc091d Mon Sep 17 00:00:00 2001 From: Javier Goizueta Date: Tue, 26 Jan 2016 13:20:28 +0100 Subject: [PATCH] Change the overview table naming scheme The scheme is changed from table_ovN to _vovw_N_table for lower collision probability. Also future naming changes will be easier by using the functions _CDB_OverviewTableDiscriminator, _CDB_OverviewTableName, _CDB_IsOverviewTableOf, etc. --- scripts-available/CDB_Overviews.sql | 92 +++++++++++++++++++++++++---- test/CDB_OverviewsTest.sql | 7 +-- test/CDB_OverviewsTest_expect | 44 +++++++------- 3 files changed, 104 insertions(+), 39 deletions(-) diff --git a/scripts-available/CDB_Overviews.sql b/scripts-available/CDB_Overviews.sql index 9824882..6a17574 100644 --- a/scripts-available/CDB_Overviews.sql +++ b/scripts-available/CDB_Overviews.sql @@ -1,3 +1,72 @@ +-- Pattern that can be used to detect overview tables and Extract +-- the intended zoom level from the table name. +-- Scope: private. +CREATE OR REPLACE FUNCTION _CDB_OverviewTableDiscriminator() +RETURNS TEXT +AS $$ + BEGIN + RETURN '\A_vovw_(\d+)_'; + END; +$$ LANGUAGE PLPGSQL IMMUTABLE; +-- substring(tablename from _CDB_OverviewTableDiscriminator()) + + +-- Pattern matched by the overview tables of a given base table name. +-- Scope: private. +CREATE OR REPLACE FUNCTION _CDB_OverviewTablePattern(base_table TEXT) +RETURNS TEXT +AS $$ + BEGIN + RETURN _CDB_OverviewTableDiscriminator() || base_table; + END; +$$ LANGUAGE PLPGSQL IMMUTABLE; +-- tablename SIMILAR TO _CDB_OverviewTablePattern(base_table) + +-- Name of an overview table, given the base table name and the Z level +-- Scope: private. +CREATE OR REPLACE FUNCTION _CDB_OverviewTableName(base_table TEXT, z INTEGER) +RETURNS TEXT +AS $$ + BEGIN + RETURN '_vovw_' || z::text || '_' || base_table; + END; +$$ LANGUAGE PLPGSQL IMMUTABLE; + +-- Condition to check if a tabla is an overview table of some base table +-- Scope: private. +CREATE OR REPLACE FUNCTION _CDB_IsOverviewTableOf(base_table TEXT, otable TEXT) +RETURNS BOOLEAN +AS $$ + BEGIN + RETURN otable SIMILAR TO _CDB_OverviewTablePattern(base_table); + END; +$$ LANGUAGE PLPGSQL IMMUTABLE; + +-- Extract the Z level from an overview table name +-- Scope: private. +CREATE OR REPLACE FUNCTION _CDB_OverviewTableZ(otable TEXT) +RETURNS INTEGER +AS $$ + BEGIN + RETURN substring(otable from _CDB_OverviewTableDiscriminator())::integer; + END; +$$ LANGUAGE PLPGSQL IMMUTABLE; + +-- Name of the base table corresponding to an overview table +-- Scope: private. +CREATE OR REPLACE FUNCTION _CDB_OverviewBaseTableName(overview_table TEXT) +RETURNS TEXT +AS $$ + BEGIN + IF _CDB_OverviewTableZ(overview_table) IS NULL THEN + RETURN overview_table; + ELSE + RETURN regexp_replace(overview_table, _CDB_OverviewTableDiscriminator(), ''); + END IF; + END; +$$ LANGUAGE PLPGSQL IMMUTABLE; + + -- Remove a dataset's existing overview tables. -- Scope: public -- Parameters: @@ -34,11 +103,11 @@ AS $$ -- possible solutions: return table names as text instead of regclass -- or add schema of reloid before casting to regclass SELECT - reloid as base_table, - substring(cdb_usertables from '\d+$')::integer as z, - cdb_usertables::regclass as overview_table + reloid AS base_table, + _CDB_OverviewTableZ(cdb_usertables) AS z, + cdb_usertables::regclass AS overview_table FROM CDB_UserTables() - WHERE cdb_usertables SIMILAR TO (SELECT relname FROM pg_class WHERE oid=reloid) || '_ov[\d]+' + WHERE _CDB_IsOverviewTableOf((SELECT relname FROM pg_class WHERE oid=reloid), cdb_usertables) ORDER BY z; $$ LANGUAGE SQL; @@ -55,11 +124,12 @@ CREATE OR REPLACE FUNCTION CDB_Overviews(tables regclass[]) RETURNS TABLE(base_table REGCLASS, z integer, overview_table REGCLASS) AS $$ SELECT - base_table::regclass AS base_table, substring(cdb_usertables from '\d+$')::integer as z, - cdb_usertables::regclass as overview_table + base_table::regclass AS base_table, + _CDB_OverviewTableZ(cdb_usertables) AS z, + cdb_usertables::regclass AS overview_table FROM CDB_UserTables(), unnest(tables) base_table - WHERE cdb_usertables SIMILAR TO (SELECT relname FROM pg_class WHERE oid=base_table) || '_ov[\d]+' + WHERE _CDB_IsOverviewTableOf((SELECT relname FROM pg_class WHERE oid=base_table), cdb_usertables) ORDER BY base_table, z; $$ LANGUAGE SQL; @@ -236,12 +306,8 @@ AS $$ is_overview BOOLEAN; BEGIN SELECT * FROM _cdb_split_table_name(ref) INTO schema_name, base; - suffix := Format('_ov%s', ref_z); - SELECT base LIKE Format('%%%s', suffix) INTO is_overview; - IF is_overview THEN - SELECT substring(base FROM 1 FOR length(base)-length(suffix)) INTO base; - END IF; - RETURN Format('%s_ov%s', base, overview_z); + SELECT _CDB_OverviewBaseTableName(base) INTO base; + RETURN _CDB_OverviewTableName(base, overview_z); END $$ LANGUAGE PLPGSQL IMMUTABLE; diff --git a/test/CDB_OverviewsTest.sql b/test/CDB_OverviewsTest.sql index 8cea584..ad8ded4 100644 --- a/test/CDB_OverviewsTest.sql +++ b/test/CDB_OverviewsTest.sql @@ -8,7 +8,7 @@ SELECT _CDB_Aggregated_Attributes_Expression('base_bare_t'::regclass); SELECT _CDB_Aggregated_Attributes_Expression('base_bare_t'::regclass, 'tab'); SELECT CDB_CreateOverviews('base_bare_t'::regclass); -SELECT count(*) FROM base_bare_t_ov5; +SELECT count(*) FROM _vovw_5_base_bare_t; SELECT _CDB_Aggregable_Attributes_Expression('base_t'::regclass); @@ -16,15 +16,14 @@ SELECT _CDB_Aggregated_Attributes_Expression('base_t'::regclass); SELECT _CDB_Aggregated_Attributes_Expression('base_t'::regclass, 'tab'); SELECT CDB_CreateOverviews('base_t'::regclass); -SELECT count(*) FROM base_t_ov5; +SELECT count(*) FROM _vovw_5_base_t; SELECT CDB_Overviews('base_t'::regclass); SELECT CDB_Overviews(ARRAY['base_t'::regclass, 'base_bare_t'::regclass]); - SELECT CDB_DropOverviews('base_bare_t'::regclass); SELECT CDB_DropOverviews('base_t'::regclass); -SELECT count(*) FROM base_t_ov5; +SELECT count(*) FROM _vovw_5_base_t; DROP TABLE base_bare_t; DROP TABLE base_t; diff --git a/test/CDB_OverviewsTest_expect b/test/CDB_OverviewsTest_expect index 468f05c..8673b7e 100644 --- a/test/CDB_OverviewsTest_expect +++ b/test/CDB_OverviewsTest_expect @@ -6,35 +6,35 @@ INSERT 0 1114 -{base_bare_t_ov5,base_bare_t_ov4,base_bare_t_ov3,base_bare_t_ov2,base_bare_t_ov1,base_bare_t_ov0} +{_vovw_5_base_bare_t,_vovw_4_base_bare_t,_vovw_3_base_bare_t,_vovw_2_base_bare_t,_vovw_1_base_bare_t,_vovw_0_base_bare_t} 125 number,int_number,name,start AVG(number)::double precision AS number,AVG(int_number)::integer AS int_number,CASE count(*) WHEN 1 THEN string_agg(name,'/') ELSE '*' END::text AS name,CASE count(*) WHEN 1 THEN MIN(start) ELSE NULL END::date AS start AVG(tab.number)::double precision AS number,AVG(tab.int_number)::integer AS int_number,CASE count(*) WHEN 1 THEN string_agg(tab.name,'/') ELSE '*' END::text AS name,CASE count(*) WHEN 1 THEN MIN(tab.start) ELSE NULL END::date AS start -{base_t_ov5,base_t_ov4,base_t_ov3,base_t_ov2,base_t_ov1,base_t_ov0} +{_vovw_5_base_t,_vovw_4_base_t,_vovw_3_base_t,_vovw_2_base_t,_vovw_1_base_t,_vovw_0_base_t} 125 -(base_t,0,base_t_ov0) -(base_t,1,base_t_ov1) -(base_t,2,base_t_ov2) -(base_t,3,base_t_ov3) -(base_t,4,base_t_ov4) -(base_t,5,base_t_ov5) -(base_bare_t,0,base_bare_t_ov0) -(base_bare_t,1,base_bare_t_ov1) -(base_bare_t,2,base_bare_t_ov2) -(base_bare_t,3,base_bare_t_ov3) -(base_bare_t,4,base_bare_t_ov4) -(base_bare_t,5,base_bare_t_ov5) -(base_t,0,base_t_ov0) -(base_t,1,base_t_ov1) -(base_t,2,base_t_ov2) -(base_t,3,base_t_ov3) -(base_t,4,base_t_ov4) -(base_t,5,base_t_ov5) +(base_t,0,_vovw_0_base_t) +(base_t,1,_vovw_1_base_t) +(base_t,2,_vovw_2_base_t) +(base_t,3,_vovw_3_base_t) +(base_t,4,_vovw_4_base_t) +(base_t,5,_vovw_5_base_t) +(base_bare_t,0,_vovw_0_base_bare_t) +(base_bare_t,1,_vovw_1_base_bare_t) +(base_bare_t,2,_vovw_2_base_bare_t) +(base_bare_t,3,_vovw_3_base_bare_t) +(base_bare_t,4,_vovw_4_base_bare_t) +(base_bare_t,5,_vovw_5_base_bare_t) +(base_t,0,_vovw_0_base_t) +(base_t,1,_vovw_1_base_t) +(base_t,2,_vovw_2_base_t) +(base_t,3,_vovw_3_base_t) +(base_t,4,_vovw_4_base_t) +(base_t,5,_vovw_5_base_t) -ERROR: relation "base_t_ov5" does not exist -LINE 1: SELECT count(*) FROM base_t_ov5; +ERROR: relation "_vovw_5_base_t" does not exist +LINE 1: SELECT count(*) FROM _vovw_5_base_t; ^ DROP TABLE DROP TABLE