Overload CDB_Overviews to accept either a single table or an array

The result of CDB_Overviews has been expandend with a base_table column
This commit is contained in:
Javier Goizueta 2016-01-26 10:39:26 +01:00
parent 7033a8d9ac
commit 1ebaeb76ac
4 changed files with 75 additions and 3 deletions

View File

@ -53,16 +53,42 @@ CDB_CreateOverviews(table_name, ref_z_strategy, reduction_strategy)
Obtain overview metadata for a given table (existing overviews). Obtain overview metadata for a given table (existing overviews).
The returned relation will be empty if the table has no overviews. The returned relation will be empty if the table has no overviews.
The function can be applied to a single table:
```sql ```sql
SELECT CDB_Overviews('table_name'); SELECT CDB_Overviews('table_name');
--- Return existing overview Z levels and corresponding tables --- 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 #### Arguments
CDB_Overviews(table_name) 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 ### CDB_DropOverviews

View File

@ -17,20 +17,24 @@ BEGIN
END; END;
$$ LANGUAGE PLPGSQL VOLATILE; $$ LANGUAGE PLPGSQL VOLATILE;
-- Return existing overviews (if any) for a given dataset table -- Return existing overviews (if any) for a given dataset table
-- Scope: public -- Scope: public
-- Parameters -- Parameters
-- reloid: oid of the input table. -- reloid: oid of the input table.
-- Return relation of overviews for the table with -- 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) 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 $$ AS $$
-- FIXME: this will fail if the overview tables -- FIXME: this will fail if the overview tables
-- require a explicit schema name -- require a explicit schema name
-- 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,
substring(cdb_usertables from '\d+$')::integer as z, substring(cdb_usertables from '\d+$')::integer as z,
cdb_usertables::regclass as overview_table cdb_usertables::regclass as overview_table
FROM CDB_UserTables() FROM CDB_UserTables()
@ -38,6 +42,27 @@ AS $$
ORDER BY z; ORDER BY z;
$$ LANGUAGE SQL; $$ 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 -- Schema and relation names of a table given its reloid
-- Scope: private. -- Scope: private.
-- Parameters -- Parameters

View File

@ -18,6 +18,9 @@ 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 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_bare_t'::regclass);
SELECT CDB_DropOverviews('base_t'::regclass); SELECT CDB_DropOverviews('base_t'::regclass);

View File

@ -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 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} {base_t_ov5,base_t_ov4,base_t_ov3,base_t_ov2,base_t_ov1,base_t_ov0}
125 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 ERROR: relation "base_t_ov5" does not exist