Fix cases that require explicit schema name
This allows using overview functions in situations where the base tables require being qualified with the schema name.
This commit is contained in:
parent
6d9424746c
commit
3c71eecbae
@ -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;
|
||||||
@ -109,7 +147,7 @@ AS $$
|
|||||||
RETURN QUERY SELECT
|
RETURN QUERY SELECT
|
||||||
reloid AS base_table,
|
reloid AS base_table,
|
||||||
_CDB_OverviewTableZ(cdb_usertables) AS z,
|
_CDB_OverviewTableZ(cdb_usertables) AS z,
|
||||||
(schema_name||'.'||cdb_usertables)::regclass AS overview_table
|
('"' || schema_name|| '"."' ||cdb_usertables || '"')::regclass AS overview_table
|
||||||
FROM CDB_UserTables()
|
FROM CDB_UserTables()
|
||||||
WHERE _CDB_IsOverviewTableOf((SELECT relname FROM pg_class WHERE oid=reloid), cdb_usertables)
|
WHERE _CDB_IsOverviewTableOf((SELECT relname FROM pg_class WHERE oid=reloid), cdb_usertables)
|
||||||
ORDER BY z;
|
ORDER BY z;
|
||||||
@ -131,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
|
||||||
@ -283,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;
|
||||||
@ -333,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
|
||||||
@ -563,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
|
||||||
@ -575,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;
|
||||||
|
|
||||||
@ -608,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.
|
||||||
|
Loading…
Reference in New Issue
Block a user