Merge pull request #220 from CartoDB/218-org-users-overviews
Add support for explicit schema names in overview functions
This commit is contained in:
@@ -68,6 +68,41 @@ AS $$
|
|||||||
END;
|
END;
|
||||||
$$ LANGUAGE PLPGSQL IMMUTABLE;
|
$$ LANGUAGE PLPGSQL IMMUTABLE;
|
||||||
|
|
||||||
|
-- Schema and relation names of a table given its reloid
|
||||||
|
-- Scope: private.
|
||||||
|
-- Parameters
|
||||||
|
-- reloid: oid of the table.
|
||||||
|
-- Return (schema_name, table_name)
|
||||||
|
-- note that returned names will be quoted if necessary
|
||||||
|
CREATE OR REPLACE FUNCTION _cdb_split_table_name(reloid REGCLASS, OUT schema_name TEXT, OUT table_name TEXT)
|
||||||
|
AS $$
|
||||||
|
BEGIN
|
||||||
|
SELECT n.nspname, c.relname
|
||||||
|
INTO STRICT schema_name, table_name
|
||||||
|
FROM pg_class c JOIN pg_namespace n ON c.relnamespace = n.oid
|
||||||
|
WHERE c.oid = reloid;
|
||||||
|
END
|
||||||
|
$$ LANGUAGE PLPGSQL IMMUTABLE;
|
||||||
|
|
||||||
|
-- Schema and relation names of a table given its reloid
|
||||||
|
-- Scope: private.
|
||||||
|
-- Parameters
|
||||||
|
-- reloid: oid of the table.
|
||||||
|
-- Return (schema_name, table_name)
|
||||||
|
-- note that returned names will be quoted if necessary
|
||||||
|
CREATE OR REPLACE FUNCTION _cdb_schema_name(reloid REGCLASS)
|
||||||
|
RETURNS TEXT
|
||||||
|
AS $$
|
||||||
|
DECLARE
|
||||||
|
schema_name TEXT;
|
||||||
|
BEGIN
|
||||||
|
SELECT n.nspname
|
||||||
|
INTO STRICT schema_name
|
||||||
|
FROM pg_class c JOIN pg_namespace n ON c.relnamespace = n.oid
|
||||||
|
WHERE c.oid = reloid;
|
||||||
|
RETURN schema_name;
|
||||||
|
END
|
||||||
|
$$ LANGUAGE PLPGSQL IMMUTABLE;
|
||||||
|
|
||||||
-- Remove a dataset's existing overview tables.
|
-- Remove a dataset's existing overview tables.
|
||||||
-- Scope: public
|
-- Scope: public
|
||||||
@@ -77,12 +112,15 @@ CREATE OR REPLACE FUNCTION CDB_DropOverviews(reloid REGCLASS)
|
|||||||
RETURNS void
|
RETURNS void
|
||||||
AS $$
|
AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
row record;
|
row record;
|
||||||
|
schema_name TEXT;
|
||||||
|
table_name TEXT;
|
||||||
BEGIN
|
BEGIN
|
||||||
|
SELECT * FROM _cdb_split_table_name(reloid) INTO schema_name, table_name;
|
||||||
FOR row IN
|
FOR row IN
|
||||||
SELECT * FROM CDB_Overviews(reloid)
|
SELECT * FROM CDB_Overviews(reloid)
|
||||||
LOOP
|
LOOP
|
||||||
EXECUTE Format('DROP TABLE %s;', row.overview_table);
|
EXECUTE Format('DROP TABLE %I.%I;', schema_name, row.overview_table);
|
||||||
RAISE NOTICE 'Dropped overview for level %: %', row.z, row.overview_table;
|
RAISE NOTICE 'Dropped overview for level %: %', row.z, row.overview_table;
|
||||||
END LOOP;
|
END LOOP;
|
||||||
END;
|
END;
|
||||||
@@ -100,18 +138,21 @@ $$ LANGUAGE PLPGSQL VOLATILE;
|
|||||||
CREATE OR REPLACE FUNCTION CDB_Overviews(reloid REGCLASS)
|
CREATE OR REPLACE FUNCTION CDB_Overviews(reloid REGCLASS)
|
||||||
RETURNS TABLE(base_table REGCLASS, 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
|
DECLARE
|
||||||
-- require a explicit schema name
|
schema_name TEXT;
|
||||||
-- possible solutions: return table names as text instead of regclass
|
table_name TEXT;
|
||||||
-- or add schema of reloid before casting to regclass
|
BEGIN
|
||||||
SELECT
|
-- TODO: review implementation of CDB_UserTables an suitability for this
|
||||||
reloid AS base_table,
|
SELECT * FROM _cdb_split_table_name(reloid) INTO schema_name, table_name;
|
||||||
_CDB_OverviewTableZ(cdb_usertables) AS z,
|
RETURN QUERY SELECT
|
||||||
cdb_usertables::regclass AS overview_table
|
reloid AS base_table,
|
||||||
FROM CDB_UserTables()
|
_CDB_OverviewTableZ(cdb_usertables) AS z,
|
||||||
WHERE _CDB_IsOverviewTableOf((SELECT relname FROM pg_class WHERE oid=reloid), cdb_usertables)
|
('"' || schema_name|| '"."' ||cdb_usertables || '"')::regclass AS overview_table
|
||||||
ORDER BY z;
|
FROM CDB_UserTables()
|
||||||
$$ LANGUAGE SQL;
|
WHERE _CDB_IsOverviewTableOf((SELECT relname FROM pg_class WHERE oid=reloid), cdb_usertables)
|
||||||
|
ORDER BY z;
|
||||||
|
END
|
||||||
|
$$ LANGUAGE PLPGSQL;
|
||||||
|
|
||||||
-- Return existing overviews (if any) for multiple dataset tables.
|
-- Return existing overviews (if any) for multiple dataset tables.
|
||||||
-- Scope: public
|
-- Scope: public
|
||||||
@@ -128,29 +169,13 @@ AS $$
|
|||||||
SELECT
|
SELECT
|
||||||
base_table::regclass AS base_table,
|
base_table::regclass AS base_table,
|
||||||
_CDB_OverviewTableZ(cdb_usertables) AS z,
|
_CDB_OverviewTableZ(cdb_usertables) AS z,
|
||||||
cdb_usertables::regclass AS overview_table
|
('"' || _cdb_schema_name(base_table::regclass) || '"."' || cdb_usertables || '"')::regclass AS overview_table
|
||||||
FROM
|
FROM
|
||||||
CDB_UserTables(), unnest(tables) base_table
|
CDB_UserTables(), unnest(tables) base_table
|
||||||
WHERE _CDB_IsOverviewTableOf((SELECT relname FROM pg_class WHERE oid=base_table), cdb_usertables)
|
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;
|
||||||
|
|
||||||
-- Schema and relation names of a table given its reloid
|
|
||||||
-- Scope: private.
|
|
||||||
-- Parameters
|
|
||||||
-- reloid: oid of the table.
|
|
||||||
-- Return (schema_name, table_name)
|
|
||||||
-- note that returned names will be quoted if necessary
|
|
||||||
CREATE OR REPLACE FUNCTION _cdb_split_table_name(reloid REGCLASS, OUT schema_name TEXT, OUT table_name TEXT)
|
|
||||||
AS $$
|
|
||||||
BEGIN
|
|
||||||
SELECT n.nspname, c.relname
|
|
||||||
INTO STRICT schema_name, table_name
|
|
||||||
FROM pg_class c JOIN pg_namespace n ON c.relnamespace = n.oid
|
|
||||||
WHERE c.oid = reloid;
|
|
||||||
END
|
|
||||||
$$ LANGUAGE PLPGSQL IMMUTABLE;
|
|
||||||
|
|
||||||
-- Calculate the estimated extent of a cartodbfy'ed table.
|
-- Calculate the estimated extent of a cartodbfy'ed table.
|
||||||
-- Scope: private.
|
-- Scope: private.
|
||||||
-- Parameters
|
-- Parameters
|
||||||
@@ -280,8 +305,8 @@ AS $$
|
|||||||
SELECT _CDB_Feature_Density(reloid, nz) INTO fd;
|
SELECT _CDB_Feature_Density(reloid, nz) INTO fd;
|
||||||
-- lim maximum number of (desiderable) features per tile
|
-- lim maximum number of (desiderable) features per tile
|
||||||
-- we have c = 2*Pi*R = CDB_XYZ_Resolution(-8) (earth circumference)
|
-- we have c = 2*Pi*R = CDB_XYZ_Resolution(-8) (earth circumference)
|
||||||
-- ta(z): tile area = power(c*power(2,z), 2) = c*c*power(2,2*z)
|
-- ta(z): tile area = power(c*power(2,-z), 2) = c*c*power(2,-2*z)
|
||||||
-- => fd*ta(z) if the average number of features per tile at level z
|
-- => fd*ta(z) is the average number of features per tile at level z
|
||||||
-- find minimum z so that fd*ta(z) <= lim
|
-- find minimum z so that fd*ta(z) <= lim
|
||||||
-- compute a rough 'feature density' value
|
-- compute a rough 'feature density' value
|
||||||
SELECT CDB_XYZ_Resolution(-8) INTO c;
|
SELECT CDB_XYZ_Resolution(-8) INTO c;
|
||||||
@@ -330,12 +355,15 @@ AS $$
|
|||||||
base_name TEXT;
|
base_name TEXT;
|
||||||
class_info RECORD;
|
class_info RECORD;
|
||||||
num_samples INTEGER;
|
num_samples INTEGER;
|
||||||
|
schema_name TEXT;
|
||||||
|
table_name TEXT;
|
||||||
BEGIN
|
BEGIN
|
||||||
overview_rel := _CDB_Overview_Name(reloid, ref_z, overview_z);
|
overview_rel := _CDB_Overview_Name(reloid, ref_z, overview_z);
|
||||||
fraction := power(2, 2*(overview_z - ref_z));
|
fraction := power(2, 2*(overview_z - ref_z));
|
||||||
|
|
||||||
-- FIXME: handle schema name for overview_rel if reloid requires it
|
SELECT * FROM _cdb_split_table_name(reloid) INTO schema_name, table_name;
|
||||||
EXECUTE Format('DROP TABLE IF EXISTS %I CASCADE;', overview_rel);
|
|
||||||
|
EXECUTE Format('DROP TABLE IF EXISTS %I.%I CASCADE;', schema_name.overview_rel);
|
||||||
|
|
||||||
-- Estimate number of rows
|
-- Estimate number of rows
|
||||||
SELECT reltuples, relpages FROM pg_class INTO STRICT class_info
|
SELECT reltuples, relpages FROM pg_class INTO STRICT class_info
|
||||||
@@ -560,6 +588,8 @@ AS $$
|
|||||||
attributes TEXT;
|
attributes TEXT;
|
||||||
columns TEXT;
|
columns TEXT;
|
||||||
gtypes TEXT[];
|
gtypes TEXT[];
|
||||||
|
schema_name TEXT;
|
||||||
|
table_name TEXT;
|
||||||
BEGIN
|
BEGIN
|
||||||
SELECT _CDB_GeometryTypes(reloid) INTO gtypes;
|
SELECT _CDB_GeometryTypes(reloid) INTO gtypes;
|
||||||
IF array_upper(gtypes, 1) <> 1 OR gtypes[1] <> 'ST_Point' THEN
|
IF array_upper(gtypes, 1) <> 1 OR gtypes[1] <> 'ST_Point' THEN
|
||||||
@@ -572,6 +602,8 @@ AS $$
|
|||||||
|
|
||||||
overview_rel := _CDB_Overview_Name(reloid, ref_z, overview_z);
|
overview_rel := _CDB_Overview_Name(reloid, ref_z, overview_z);
|
||||||
|
|
||||||
|
SELECT * FROM _cdb_split_table_name(reloid) INTO schema_name, table_name;
|
||||||
|
|
||||||
-- compute grid cell size using the overview_z dimension...
|
-- compute grid cell size using the overview_z dimension...
|
||||||
SELECT CDB_XYZ_Resolution(overview_z)*grid_px INTO grid_m;
|
SELECT CDB_XYZ_Resolution(overview_z)*grid_px INTO grid_m;
|
||||||
|
|
||||||
@@ -605,8 +637,7 @@ AS $$
|
|||||||
SELECT * FROM cols
|
SELECT * FROM cols
|
||||||
) AS s INTO columns;
|
) AS s INTO columns;
|
||||||
|
|
||||||
-- FIXME: handle schema name for overview_rel if reloid requires it
|
EXECUTE Format('DROP TABLE IF EXISTS %I.%I CASCADE;', schema_name, overview_rel);
|
||||||
EXECUTE Format('DROP TABLE IF EXISTS %I CASCADE;', overview_rel);
|
|
||||||
|
|
||||||
-- Now we cluster the data using a grid of size grid_m
|
-- Now we cluster the data using a grid of size grid_m
|
||||||
-- and selecte the centroid (average coordinates) of each cluster.
|
-- and selecte the centroid (average coordinates) of each cluster.
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ SELECT CDB_CreateOverviews('polyg_t'::regclass);
|
|||||||
SELECT CDB_CreateOverviews('column_types_t'::regclass);
|
SELECT CDB_CreateOverviews('column_types_t'::regclass);
|
||||||
|
|
||||||
SELECT CDB_Overviews('base_t'::regclass);
|
SELECT CDB_Overviews('base_t'::regclass);
|
||||||
|
SELECT CDB_Overviews('"public"."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_Overviews('polyg_t'::regclass);
|
SELECT CDB_Overviews('polyg_t'::regclass);
|
||||||
SELECT CDB_Overviews('column_types_t'::regclass);
|
SELECT CDB_Overviews('column_types_t'::regclass);
|
||||||
|
|||||||
@@ -24,6 +24,12 @@ AVG(tab.number)::double precision AS number,AVG(tab.int_number)::integer AS int_
|
|||||||
(base_t,3,_vovw_3_base_t)
|
(base_t,3,_vovw_3_base_t)
|
||||||
(base_t,4,_vovw_4_base_t)
|
(base_t,4,_vovw_4_base_t)
|
||||||
(base_t,5,_vovw_5_base_t)
|
(base_t,5,_vovw_5_base_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)
|
||||||
(base_bare_t,0,_vovw_0_base_bare_t)
|
(base_bare_t,0,_vovw_0_base_bare_t)
|
||||||
(base_bare_t,1,_vovw_1_base_bare_t)
|
(base_bare_t,1,_vovw_1_base_bare_t)
|
||||||
(base_bare_t,2,_vovw_2_base_bare_t)
|
(base_bare_t,2,_vovw_2_base_bare_t)
|
||||||
|
|||||||
Reference in New Issue
Block a user