From 00bd302f017506fc3c87f838e468b61851884107 Mon Sep 17 00:00:00 2001 From: Javier Goizueta Date: Fri, 29 Jan 2016 16:19:35 +0100 Subject: [PATCH] Avoid creating GridCluster overviews for non-point datasets --- scripts-available/CDB_Overviews.sql | 37 ++++++++++++++++++++++++++++- test/CDB_OverviewsTest.sql | 4 ++++ test/CDB_OverviewsTest_expect | 4 ++++ test/overviews/fixtures.sql | 8 +++++++ 4 files changed, 52 insertions(+), 1 deletion(-) diff --git a/scripts-available/CDB_Overviews.sql b/scripts-available/CDB_Overviews.sql index f0c2931..0673045 100644 --- a/scripts-available/CDB_Overviews.sql +++ b/scripts-available/CDB_Overviews.sql @@ -513,6 +513,28 @@ BEGIN END $$ 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. -- It clusters the points using a grid, then aggregates the point in each -- cluster into a point at the centroid of the clustered records. @@ -535,7 +557,17 @@ AS $$ aggr_attributes TEXT; attributes TEXT; columns TEXT; + gtypes TEXT[]; 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); -- 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; -- 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; WHILE overview_z >= 0 LOOP SELECT array_append(overviews_z, overview_z) INTO overviews_z; @@ -642,6 +674,9 @@ BEGIN base_rel := reloid; 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; + IF base_rel IS NULL THEN + EXIT; + END IF; base_z := overview_z; PERFORM _CDB_Register_Overview(reloid, base_rel, base_z); SELECT array_append(overview_tables, base_rel) INTO overview_tables; diff --git a/test/CDB_OverviewsTest.sql b/test/CDB_OverviewsTest.sql index ad8ded4..670dfe8 100644 --- a/test/CDB_OverviewsTest.sql +++ b/test/CDB_OverviewsTest.sql @@ -18,8 +18,11 @@ SELECT _CDB_Aggregated_Attributes_Expression('base_t'::regclass, 'tab'); SELECT CDB_CreateOverviews('base_t'::regclass); SELECT count(*) FROM _vovw_5_base_t; +SELECT CDB_CreateOverviews('polyg_t'::regclass); + SELECT CDB_Overviews('base_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_t'::regclass); @@ -27,3 +30,4 @@ SELECT count(*) FROM _vovw_5_base_t; DROP TABLE base_bare_t; DROP TABLE base_t; +DROP TABLE polyg_t; diff --git a/test/CDB_OverviewsTest_expect b/test/CDB_OverviewsTest_expect index ab94049..d770199 100644 --- a/test/CDB_OverviewsTest_expect +++ b/test/CDB_OverviewsTest_expect @@ -3,6 +3,8 @@ CREATE TABLE INSERT 0 1114 CREATE TABLE 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 {_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 + (base_t,0,_vovw_0_base_t) (base_t,1,_vovw_1_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 diff --git a/test/overviews/fixtures.sql b/test/overviews/fixtures.sql index bfe1ae6..3dc1679 100644 --- a/test/overviews/fixtures.sql +++ b/test/overviews/fixtures.sql @@ -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)), (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)); +-- 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));