diff --git a/doc/CDB_Overviews.md b/doc/CDB_Overviews.md index 2f2a006..3516b00 100644 --- a/doc/CDB_Overviews.md +++ b/doc/CDB_Overviews.md @@ -53,16 +53,42 @@ CDB_CreateOverviews(table_name, ref_z_strategy, reduction_strategy) Obtain overview metadata for a given table (existing overviews). The returned relation will be empty if the table has no overviews. +The function can be applied to a single table: + ```sql SELECT CDB_Overviews('table_name'); --- Return existing overview Z levels and corresponding tables ``` +Or to multiple tables passed as an array; this can be used +to obtain the overviews that can be applied to a query by +combining it with `CDB_QueryTablesText`: + +```sql +SELECT CDB_Overviews(CDB_QueryTablesText('SELECT * FROM table1, table2')); +--- Return existing overview Z levels and corresponding tables +``` + +The result of `CDB_Overviews` has three columns: + +| base_table | z | overview_table | +|------------+---+----------------| +| table1 | 1 | table1_ov1 | +| table1 | 2 | table1_ov2 | +| table1 | 4 | table1_ov4 | +| table2 | 1 | table1_ov1 | +| table2 | 2 | table1_ov2 | + #### Arguments CDB_Overviews(table_name) -* **table_name** regclass, table to obtain existing overviews for +* **table_name** regclass, oid of table to obtain existing overviews for + +CDB_Overviews(table_names) + +* **table_names** regclass[], array of table oids + ### CDB_DropOverviews diff --git a/scripts-available/CDB_Overviews.sql b/scripts-available/CDB_Overviews.sql index 06ed19b..9824882 100644 --- a/scripts-available/CDB_Overviews.sql +++ b/scripts-available/CDB_Overviews.sql @@ -17,20 +17,24 @@ BEGIN END; $$ LANGUAGE PLPGSQL VOLATILE; + + -- Return existing overviews (if any) for a given dataset table -- Scope: public -- Parameters -- reloid: oid of the input table. -- Return relation of overviews for the table with --- z level of the overview and overview table, ordered by z. +-- the base table oid, +-- z level of the overview and overview table oid, ordered by z. CREATE OR REPLACE FUNCTION CDB_Overviews(reloid REGCLASS) -RETURNS TABLE(z integer, overview_table REGCLASS) +RETURNS TABLE(base_table REGCLASS, z integer, overview_table REGCLASS) AS $$ -- FIXME: this will fail if the overview tables -- require a explicit schema name -- 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 FROM CDB_UserTables() @@ -38,6 +42,27 @@ AS $$ ORDER BY z; $$ LANGUAGE SQL; +-- Return existing overviews (if any) for multiple dataset tables. +-- Scope: public +-- Parameters +-- tables: Array of input tables oids +-- Return relation of overviews for the table with +-- the base table oid, +-- z level of the overview and overview table oid, ordered by z. +-- Note: CDB_Overviews can be applied to the result of CDB_QueryTablesText +-- to obtain the overviews applicable to a query. +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 + FROM + CDB_UserTables(), unnest(tables) base_table + WHERE cdb_usertables SIMILAR TO (SELECT relname FROM pg_class WHERE oid=base_table) || '_ov[\d]+' + ORDER BY base_table, z; +$$ LANGUAGE SQL; + -- Schema and relation names of a table given its reloid -- Scope: private. -- Parameters diff --git a/test/CDB_OverviewsTest.sql b/test/CDB_OverviewsTest.sql index 2ef99ae..8cea584 100644 --- a/test/CDB_OverviewsTest.sql +++ b/test/CDB_OverviewsTest.sql @@ -18,6 +18,9 @@ SELECT _CDB_Aggregated_Attributes_Expression('base_t'::regclass, 'tab'); SELECT CDB_CreateOverviews('base_t'::regclass); SELECT count(*) FROM base_t_ov5; +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); diff --git a/test/CDB_OverviewsTest_expect b/test/CDB_OverviewsTest_expect index 29d79a7..468f05c 100644 --- a/test/CDB_OverviewsTest_expect +++ b/test/CDB_OverviewsTest_expect @@ -13,6 +13,24 @@ AVG(number)::double precision AS number,AVG(int_number)::integer AS int_number,C 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} 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) ERROR: relation "base_t_ov5" does not exist