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.
This commit is contained in:
@@ -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.
|
-- Remove a dataset's existing overview tables.
|
||||||
-- Scope: public
|
-- Scope: public
|
||||||
-- Parameters:
|
-- Parameters:
|
||||||
@@ -34,11 +103,11 @@ AS $$
|
|||||||
-- possible solutions: return table names as text instead of regclass
|
-- possible solutions: return table names as text instead of regclass
|
||||||
-- or add schema of reloid before casting to regclass
|
-- or add schema of reloid before casting to regclass
|
||||||
SELECT
|
SELECT
|
||||||
reloid as base_table,
|
reloid AS base_table,
|
||||||
substring(cdb_usertables from '\d+$')::integer as z,
|
_CDB_OverviewTableZ(cdb_usertables) AS z,
|
||||||
cdb_usertables::regclass as overview_table
|
cdb_usertables::regclass AS overview_table
|
||||||
FROM CDB_UserTables()
|
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;
|
ORDER BY z;
|
||||||
$$ LANGUAGE SQL;
|
$$ LANGUAGE SQL;
|
||||||
|
|
||||||
@@ -55,11 +124,12 @@ CREATE OR REPLACE FUNCTION CDB_Overviews(tables regclass[])
|
|||||||
RETURNS TABLE(base_table REGCLASS, z integer, overview_table REGCLASS)
|
RETURNS TABLE(base_table REGCLASS, z integer, overview_table REGCLASS)
|
||||||
AS $$
|
AS $$
|
||||||
SELECT
|
SELECT
|
||||||
base_table::regclass AS base_table, substring(cdb_usertables from '\d+$')::integer as z,
|
base_table::regclass AS base_table,
|
||||||
cdb_usertables::regclass as overview_table
|
_CDB_OverviewTableZ(cdb_usertables) AS z,
|
||||||
|
cdb_usertables::regclass AS overview_table
|
||||||
FROM
|
FROM
|
||||||
CDB_UserTables(), unnest(tables) base_table
|
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;
|
ORDER BY base_table, z;
|
||||||
$$ LANGUAGE SQL;
|
$$ LANGUAGE SQL;
|
||||||
|
|
||||||
@@ -236,12 +306,8 @@ AS $$
|
|||||||
is_overview BOOLEAN;
|
is_overview BOOLEAN;
|
||||||
BEGIN
|
BEGIN
|
||||||
SELECT * FROM _cdb_split_table_name(ref) INTO schema_name, base;
|
SELECT * FROM _cdb_split_table_name(ref) INTO schema_name, base;
|
||||||
suffix := Format('_ov%s', ref_z);
|
SELECT _CDB_OverviewBaseTableName(base) INTO base;
|
||||||
SELECT base LIKE Format('%%%s', suffix) INTO is_overview;
|
RETURN _CDB_OverviewTableName(base, overview_z);
|
||||||
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);
|
|
||||||
END
|
END
|
||||||
$$ LANGUAGE PLPGSQL IMMUTABLE;
|
$$ LANGUAGE PLPGSQL IMMUTABLE;
|
||||||
|
|
||||||
|
|||||||
@@ -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_Aggregated_Attributes_Expression('base_bare_t'::regclass, 'tab');
|
||||||
|
|
||||||
SELECT CDB_CreateOverviews('base_bare_t'::regclass);
|
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);
|
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_Aggregated_Attributes_Expression('base_t'::regclass, 'tab');
|
||||||
|
|
||||||
SELECT CDB_CreateOverviews('base_t'::regclass);
|
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('base_t'::regclass);
|
||||||
SELECT CDB_Overviews(ARRAY['base_t'::regclass, 'base_bare_t'::regclass]);
|
SELECT CDB_Overviews(ARRAY['base_t'::regclass, 'base_bare_t'::regclass]);
|
||||||
|
|
||||||
|
|
||||||
SELECT CDB_DropOverviews('base_bare_t'::regclass);
|
SELECT CDB_DropOverviews('base_bare_t'::regclass);
|
||||||
SELECT CDB_DropOverviews('base_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_bare_t;
|
||||||
DROP TABLE base_t;
|
DROP TABLE base_t;
|
||||||
|
|||||||
@@ -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
|
125
|
||||||
number,int_number,name,start
|
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(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
|
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
|
125
|
||||||
(base_t,0,base_t_ov0)
|
(base_t,0,_vovw_0_base_t)
|
||||||
(base_t,1,base_t_ov1)
|
(base_t,1,_vovw_1_base_t)
|
||||||
(base_t,2,base_t_ov2)
|
(base_t,2,_vovw_2_base_t)
|
||||||
(base_t,3,base_t_ov3)
|
(base_t,3,_vovw_3_base_t)
|
||||||
(base_t,4,base_t_ov4)
|
(base_t,4,_vovw_4_base_t)
|
||||||
(base_t,5,base_t_ov5)
|
(base_t,5,_vovw_5_base_t)
|
||||||
(base_bare_t,0,base_bare_t_ov0)
|
(base_bare_t,0,_vovw_0_base_bare_t)
|
||||||
(base_bare_t,1,base_bare_t_ov1)
|
(base_bare_t,1,_vovw_1_base_bare_t)
|
||||||
(base_bare_t,2,base_bare_t_ov2)
|
(base_bare_t,2,_vovw_2_base_bare_t)
|
||||||
(base_bare_t,3,base_bare_t_ov3)
|
(base_bare_t,3,_vovw_3_base_bare_t)
|
||||||
(base_bare_t,4,base_bare_t_ov4)
|
(base_bare_t,4,_vovw_4_base_bare_t)
|
||||||
(base_bare_t,5,base_bare_t_ov5)
|
(base_bare_t,5,_vovw_5_base_bare_t)
|
||||||
(base_t,0,base_t_ov0)
|
(base_t,0,_vovw_0_base_t)
|
||||||
(base_t,1,base_t_ov1)
|
(base_t,1,_vovw_1_base_t)
|
||||||
(base_t,2,base_t_ov2)
|
(base_t,2,_vovw_2_base_t)
|
||||||
(base_t,3,base_t_ov3)
|
(base_t,3,_vovw_3_base_t)
|
||||||
(base_t,4,base_t_ov4)
|
(base_t,4,_vovw_4_base_t)
|
||||||
(base_t,5,base_t_ov5)
|
(base_t,5,_vovw_5_base_t)
|
||||||
|
|
||||||
|
|
||||||
ERROR: relation "base_t_ov5" does not exist
|
ERROR: relation "_vovw_5_base_t" does not exist
|
||||||
LINE 1: SELECT count(*) FROM base_t_ov5;
|
LINE 1: SELECT count(*) FROM _vovw_5_base_t;
|
||||||
^
|
^
|
||||||
DROP TABLE
|
DROP TABLE
|
||||||
DROP TABLE
|
DROP TABLE
|
||||||
|
|||||||
Reference in New Issue
Block a user