Avoid creating GridCluster overviews for non-point datasets

This commit is contained in:
Javier Goizueta 2016-01-29 16:19:35 +01:00
parent 93d4a6ead0
commit 00bd302f01
4 changed files with 52 additions and 1 deletions

View File

@ -513,6 +513,28 @@ BEGIN
END END
$$ LANGUAGE PLPGSQL STABLE; $$ LANGUAGE PLPGSQL STABLE;
-- Array of geometry types detected in a cartodbfied table
-- For effciency only look at a limited number of rwos.
-- Parameters
-- reloid: oid of the input table. It must be a cartodbfy'ed table.
-- Return value: array of geometry type names
CREATE OR REPLACE FUNCTION _CDB_GeometryTypes(reloid REGCLASS)
RETURNS TEXT[]
AS $$
DECLARE
gtypes TEXT[];
BEGIN
EXECUTE Format('
SELECT array_agg(DISTINCT ST_GeometryType(the_geom)) FROM (
SELECT the_geom FROM %s
WHERE (the_geom is not null) LIMIT 10
) as geom_types
', reloid)
INTO gtypes;
RETURN gtypes;
END
$$ LANGUAGE PLPGSQL STABLE;
-- Experimental Overview reduction method for point datasets. -- Experimental Overview reduction method for point datasets.
-- It clusters the points using a grid, then aggregates the point in each -- It clusters the points using a grid, then aggregates the point in each
-- cluster into a point at the centroid of the clustered records. -- cluster into a point at the centroid of the clustered records.
@ -535,7 +557,17 @@ AS $$
aggr_attributes TEXT; aggr_attributes TEXT;
attributes TEXT; attributes TEXT;
columns TEXT; columns TEXT;
gtypes TEXT[];
BEGIN BEGIN
SELECT _CDB_GeometryTypes(reloid) INTO gtypes;
IF array_upper(gtypes, 1) <> 1 OR gtypes[1] <> 'ST_Point' THEN
-- This strategy only supports datasets with point geomety
RETURN NULL;
RETURN 'x';
END IF;
--TODO: check applicability: geometry type, minimum number of points...
overview_rel := _CDB_Overview_Name(reloid, ref_z, overview_z); overview_rel := _CDB_Overview_Name(reloid, ref_z, overview_z);
-- compute grid cell size using the overview_z dimension... -- compute grid cell size using the overview_z dimension...
@ -630,7 +662,7 @@ BEGIN
EXECUTE 'SELECT ' || quote_ident(refscale_strategy::text) || Format('(''%s'');', reloid) INTO ref_z; EXECUTE 'SELECT ' || quote_ident(refscale_strategy::text) || Format('(''%s'');', reloid) INTO ref_z;
-- Determine overlay zoom levels -- Determine overlay zoom levels
-- TODO: should be handled by the refscale_stragegy? -- TODO: should be handled by the refscale_strategy?
overview_z := ref_z - 1; overview_z := ref_z - 1;
WHILE overview_z >= 0 LOOP WHILE overview_z >= 0 LOOP
SELECT array_append(overviews_z, overview_z) INTO overviews_z; SELECT array_append(overviews_z, overview_z) INTO overviews_z;
@ -642,6 +674,9 @@ BEGIN
base_rel := reloid; base_rel := reloid;
FOREACH overview_z IN ARRAY overviews_z LOOP FOREACH overview_z IN ARRAY overviews_z LOOP
EXECUTE 'SELECT ' || quote_ident(reduce_strategy::text) || Format('(''%s'', %s, %s);', base_rel, base_z, overview_z) INTO base_rel; EXECUTE 'SELECT ' || quote_ident(reduce_strategy::text) || Format('(''%s'', %s, %s);', base_rel, base_z, overview_z) INTO base_rel;
IF base_rel IS NULL THEN
EXIT;
END IF;
base_z := overview_z; base_z := overview_z;
PERFORM _CDB_Register_Overview(reloid, base_rel, base_z); PERFORM _CDB_Register_Overview(reloid, base_rel, base_z);
SELECT array_append(overview_tables, base_rel) INTO overview_tables; SELECT array_append(overview_tables, base_rel) INTO overview_tables;

View File

@ -18,8 +18,11 @@ SELECT _CDB_Aggregated_Attributes_Expression('base_t'::regclass, 'tab');
SELECT CDB_CreateOverviews('base_t'::regclass); SELECT CDB_CreateOverviews('base_t'::regclass);
SELECT count(*) FROM _vovw_5_base_t; SELECT count(*) FROM _vovw_5_base_t;
SELECT CDB_CreateOverviews('polyg_t'::regclass);
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_Overviews('polyg_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);
@ -27,3 +30,4 @@ 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;
DROP TABLE polyg_t;

View File

@ -3,6 +3,8 @@ CREATE TABLE
INSERT 0 1114 INSERT 0 1114
CREATE TABLE CREATE TABLE
INSERT 0 1114 INSERT 0 1114
CREATE TABLE
INSERT 0 5
@ -13,6 +15,7 @@ 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 MIN(tab.name) ELSE NULL 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 MIN(tab.name) ELSE NULL END::text AS name,CASE count(*) WHEN 1 THEN MIN(tab.start) ELSE NULL END::date AS start
{_vovw_5_base_t,_vovw_4_base_t,_vovw_3_base_t,_vovw_2_base_t,_vovw_1_base_t,_vovw_0_base_t} {_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,_vovw_0_base_t) (base_t,0,_vovw_0_base_t)
(base_t,1,_vovw_1_base_t) (base_t,1,_vovw_1_base_t)
(base_t,2,_vovw_2_base_t) (base_t,2,_vovw_2_base_t)
@ -38,3 +41,4 @@ LINE 1: SELECT count(*) FROM _vovw_5_base_t;
^ ^
DROP TABLE DROP TABLE
DROP TABLE DROP TABLE
DROP TABLE

View File

@ -2232,3 +2232,11 @@ INSERT INTO base_t VALUES
(1112, 'SRID=4326;POINT(-1.544993 33.333714)'::geometry, ST_Transform('SRID=4326;POINT(-1.544993 33.333714)'::geometry, 3857)), (1112, 'SRID=4326;POINT(-1.544993 33.333714)'::geometry, ST_Transform('SRID=4326;POINT(-1.544993 33.333714)'::geometry, 3857)),
(1113, 'SRID=4326;POINT(-1.544859 33.333711)'::geometry, ST_Transform('SRID=4326;POINT(-1.544859 33.333711)'::geometry, 3857)), (1113, 'SRID=4326;POINT(-1.544859 33.333711)'::geometry, ST_Transform('SRID=4326;POINT(-1.544859 33.333711)'::geometry, 3857)),
(1114, 'SRID=4326;POINT(-1.544863 33.334479)'::geometry, ST_Transform('SRID=4326;POINT(-1.544863 33.334479)'::geometry, 3857)); (1114, 'SRID=4326;POINT(-1.544863 33.334479)'::geometry, ST_Transform('SRID=4326;POINT(-1.544863 33.334479)'::geometry, 3857));
-- polygons table
CREATE TABLE polyg_t (cartodb_id integer, name text, the_geom geometry, the_geom_webmercator geometry);
INSERT INTO polyg_t VALUES
(1, 'A', 'SRID=4326;POLYGON((9 40,8 39,8.5 40,9 41,9 40))'::geometry, ST_Transform('SRID=4326;POLYGON((9 40,8 39,8.5 40,9 41,9 40))'::geometry, 3857)),
(2, 'B', 'SRID=4326;POLYGON((9 40,8 39,8.5 40,9 41,9 40))'::geometry, ST_Transform('SRID=4326;POLYGON((9 40,8 39,8.5 40,9 41,9 40))'::geometry, 3857)),
(3, 'C', 'SRID=4326;POLYGON((9 40,8 39,8.5 40,9 41,9 40))'::geometry, ST_Transform('SRID=4326;POLYGON((9 40,8 39,8.5 40,9 41,9 40))'::geometry, 3857)),
(4, 'D', 'SRID=4326;POLYGON((9 40,8 39,8.5 40,9 41,9 40))'::geometry, ST_Transform('SRID=4326;POLYGON((9 40,8 39,8.5 40,9 41,9 40))'::geometry, 3857)),
(5, 'E', 'SRID=4326;POLYGON((9 40,8 39,8.5 40,9 41,9 40))'::geometry, ST_Transform('SRID=4326;POLYGON((9 40,8 39,8.5 40,9 41,9 40))'::geometry, 3857));