From 84ea0753d8d7861ff6a47a6eb29f072a1d342879 Mon Sep 17 00:00:00 2001 From: Sarah Hoffmann Date: Wed, 26 Feb 2020 10:04:17 +0100 Subject: [PATCH 1/4] simplify getNearestRoadFeature function The function only ever returns one result of which only the place_id is used. So simplify it to return a single place_id only (or NULL if none is found). Rename funciton to avoid conflicts when updating an existing database. --- sql/functions/aux_property.sql | 4 ++-- sql/functions/placex_triggers.sql | 3 +-- sql/partition-functions.src.sql | 23 +++++++++++------------ sql/tiger_import_start.sql | 5 ++--- 4 files changed, 16 insertions(+), 19 deletions(-) diff --git a/sql/functions/aux_property.sql b/sql/functions/aux_property.sql index cee5cfb2..6dd99eb2 100644 --- a/sql/functions/aux_property.sql +++ b/sql/functions/aux_property.sql @@ -28,8 +28,8 @@ BEGIN END IF; IF out_parent_place_id IS NULL THEN - FOR location IN SELECT place_id FROM getNearestRoadFeature(out_partition, place_centroid) LOOP - out_parent_place_id := location.place_id; + SELECT getNearestRoadPlaceId(out_partition, place_centroid) + INTO out_parent_place_id; END LOOP; END IF; diff --git a/sql/functions/placex_triggers.sql b/sql/functions/placex_triggers.sql index fb3f2dc5..ffc83fa3 100644 --- a/sql/functions/placex_triggers.sql +++ b/sql/functions/placex_triggers.sql @@ -135,8 +135,7 @@ BEGIN IF fallback THEN IF ST_Area(bbox) < 0.01 THEN -- for smaller features get the nearest road - SELECT place_id FROM getNearestRoadFeature(poi_partition, bbox) - INTO parent_place_id; + SELECT getNearestRoadPlaceId(poi_partition, bbox) INTO parent_place_id; --DEBUG: RAISE WARNING 'Checked for nearest way (%)', parent_place_id; ELSE -- for larger features simply find the area with the largest rank that diff --git a/sql/partition-functions.src.sql b/sql/partition-functions.src.sql index 41758c83..4a8f9139 100644 --- a/sql/partition-functions.src.sql +++ b/sql/partition-functions.src.sql @@ -214,29 +214,28 @@ END $$ LANGUAGE plpgsql; -create or replace function getNearestRoadFeature(in_partition INTEGER, point GEOMETRY) RETURNS setof nearfeature AS $$ +CREATE OR REPLACE FUNCTION getNearestRoadPlaceId(in_partition INTEGER, point GEOMETRY) + RETURNS BIGINT + AS $$ DECLARE - r nearfeature%rowtype; - search_diameter FLOAT; + r RECORD; + search_diameter FLOAT; BEGIN -- start IF in_partition = -partition- THEN search_diameter := 0.00005; WHILE search_diameter < 0.1 LOOP - FOR r IN - SELECT place_id, null, null, null, - ST_Distance(geometry, point) as distance, null as isguess - FROM location_road_-partition- - WHERE ST_DWithin(geometry, point, search_diameter) - ORDER BY distance ASC limit 1 + FOR r IN + SELECT place_id FROM location_road_-partition- + WHERE ST_DWithin(geometry, point, search_diameter) + ORDER BY ST_Distance(geometry, point) ASC limit 1 LOOP - RETURN NEXT r; - RETURN; + RETURN r.place_id; END LOOP; search_diameter := search_diameter * 2; END LOOP; - RETURN; + RETURN NULL; END IF; -- end diff --git a/sql/tiger_import_start.sql b/sql/tiger_import_start.sql index b9c4fcfc..5f916501 100644 --- a/sql/tiger_import_start.sql +++ b/sql/tiger_import_start.sql @@ -69,9 +69,8 @@ BEGIN END IF; IF out_parent_place_id IS NULL THEN - FOR location IN SELECT place_id FROM getNearestRoadFeature(out_partition, place_centroid) LOOP - out_parent_place_id := location.place_id; - END LOOP; + SELECT getNearestRoadPlaceId(out_partition, place_centroid) + INTO out_parent_place_id; END IF; --insert street(line) into import table From 8a4c7f6e2b5f46fca1adbfbe1718a273a3d43760 Mon Sep 17 00:00:00 2001 From: Sarah Hoffmann Date: Wed, 26 Feb 2020 10:14:28 +0100 Subject: [PATCH 2/4] simplify getNearestParallelRoadFeature function The function only ever returns one result of which only the place_id is used. So simplify it to return a single place_id only (or NULL if none is found). Also fix typo in function name. --- sql/partition-functions.src.sql | 36 ++++++++++++++++----------------- sql/tiger_import_start.sql | 5 ++--- 2 files changed, 20 insertions(+), 21 deletions(-) diff --git a/sql/partition-functions.src.sql b/sql/partition-functions.src.sql index 4a8f9139..dbe34363 100644 --- a/sql/partition-functions.src.sql +++ b/sql/partition-functions.src.sql @@ -242,19 +242,22 @@ BEGIN RAISE EXCEPTION 'Unknown partition %', in_partition; END $$ -LANGUAGE plpgsql; +LANGUAGE plpgsql IMMUTABLE; -create or replace function getNearestParellelRoadFeature(in_partition INTEGER, line GEOMETRY) RETURNS setof nearfeature AS $$ +CREATE OR REPLACE FUNCTION getNearestParallelRoadFeature(in_partition INTEGER, + line GEOMETRY) + RETURNS BIGINT + AS $$ DECLARE - r nearfeature%rowtype; - search_diameter FLOAT; + r RECORD; + search_diameter FLOAT; p1 GEOMETRY; p2 GEOMETRY; p3 GEOMETRY; BEGIN - IF st_geometrytype(line) not in ('ST_LineString') THEN - RETURN; + IF ST_GeometryType(line) not in ('ST_LineString') THEN + RETURN NULL; END IF; p1 := ST_LineInterpolatePoint(line,0); @@ -265,25 +268,22 @@ BEGIN IF in_partition = -partition- THEN search_diameter := 0.0005; WHILE search_diameter < 0.01 LOOP - FOR r IN - SELECT place_id, null, null, null, - ST_Distance(geometry, line) as distance, null as isguess - FROM location_road_-partition- - WHERE ST_DWithin(line, geometry, search_diameter) - ORDER BY (ST_distance(geometry, p1)+ - ST_distance(geometry, p2)+ - ST_distance(geometry, p3)) ASC limit 1 + FOR r IN + SELECT place_id FROM location_road_-partition- + WHERE ST_DWithin(line, geometry, search_diameter) + ORDER BY (ST_distance(geometry, p1)+ + ST_distance(geometry, p2)+ + ST_distance(geometry, p3)) ASC limit 1 LOOP - RETURN NEXT r; - RETURN; + RETURN r.place_id; END LOOP; search_diameter := search_diameter * 2; END LOOP; - RETURN; + RETURN NULL; END IF; -- end RAISE EXCEPTION 'Unknown partition %', in_partition; END $$ -LANGUAGE plpgsql; +LANGUAGE plpgsql IMMUTABLE; diff --git a/sql/tiger_import_start.sql b/sql/tiger_import_start.sql index 5f916501..4b9c33fc 100644 --- a/sql/tiger_import_start.sql +++ b/sql/tiger_import_start.sql @@ -63,9 +63,8 @@ BEGIN END IF; IF out_parent_place_id IS NULL THEN - FOR location IN SELECT place_id FROM getNearestParellelRoadFeature(out_partition, linegeo) LOOP - out_parent_place_id := location.place_id; - END LOOP; + SELECT getNearestParallelRoadFeature(out_partition, linegeo) + INTO out_parent_place_id; END IF; IF out_parent_place_id IS NULL THEN From bdaa39573ffe032214ec31f763ec0110401d7e4f Mon Sep 17 00:00:00 2001 From: Sarah Hoffmann Date: Wed, 26 Feb 2020 10:42:30 +0100 Subject: [PATCH 3/4] remove unused nearfeature types Also move the remaining nearfeaturecentr type close to the function that is using it. --- sql/partition-functions.src.sql | 12 ++++++++++++ sql/partition-tables.src.sql | 27 --------------------------- 2 files changed, 12 insertions(+), 27 deletions(-) diff --git a/sql/partition-functions.src.sql b/sql/partition-functions.src.sql index dbe34363..07e99c97 100644 --- a/sql/partition-functions.src.sql +++ b/sql/partition-functions.src.sql @@ -1,3 +1,15 @@ +DROP TYPE IF EXISTS nearfeaturecentr CASCADE; +CREATE TYPE nearfeaturecentr AS ( + place_id BIGINT, + keywords int[], + rank_address smallint, + rank_search smallint, + distance float, + isguess boolean, + postcode TEXT, + centroid GEOMETRY +); + create or replace function getNearFeatures(in_partition INTEGER, feature GEOMETRY, maxrank INTEGER, isin_tokens INT[]) RETURNS setof nearfeaturecentr AS $$ DECLARE r nearfeaturecentr%rowtype; diff --git a/sql/partition-tables.src.sql b/sql/partition-tables.src.sql index 57806898..8749243e 100644 --- a/sql/partition-tables.src.sql +++ b/sql/partition-tables.src.sql @@ -1,30 +1,3 @@ -drop type if exists nearplace cascade; -create type nearplace as ( - place_id BIGINT -); - -drop type if exists nearfeature cascade; -create type nearfeature as ( - place_id BIGINT, - keywords int[], - rank_address smallint, - rank_search smallint, - distance float, - isguess boolean -); - -drop type if exists nearfeaturecentr cascade; -create type nearfeaturecentr as ( - place_id BIGINT, - keywords int[], - rank_address smallint, - rank_search smallint, - distance float, - isguess boolean, - postcode TEXT, - centroid GEOMETRY -); - drop table IF EXISTS search_name_blank CASCADE; CREATE TABLE search_name_blank ( place_id BIGINT, From 03c373a4b3e031ab977c5bffb9f09731b28581a6 Mon Sep 17 00:00:00 2001 From: Sarah Hoffmann Date: Wed, 26 Feb 2020 11:41:49 +0100 Subject: [PATCH 4/4] make all query partition functions stable --- sql/partition-functions.src.sql | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/sql/partition-functions.src.sql b/sql/partition-functions.src.sql index 07e99c97..8f78032e 100644 --- a/sql/partition-functions.src.sql +++ b/sql/partition-functions.src.sql @@ -39,7 +39,7 @@ BEGIN RAISE EXCEPTION 'Unknown partition %', in_partition; END $$ -LANGUAGE plpgsql; +LANGUAGE plpgsql STABLE; create or replace function deleteLocationArea(in_partition INTEGER, in_place_id BIGINT, in_rank_search INTEGER) RETURNS BOOLEAN AS $$ DECLARE @@ -145,7 +145,7 @@ BEGIN RAISE EXCEPTION 'Unknown partition %', in_partition; END $$ -LANGUAGE plpgsql; +LANGUAGE plpgsql STABLE; create or replace function insertSearchName( @@ -254,7 +254,7 @@ BEGIN RAISE EXCEPTION 'Unknown partition %', in_partition; END $$ -LANGUAGE plpgsql IMMUTABLE; +LANGUAGE plpgsql STABLE; CREATE OR REPLACE FUNCTION getNearestParallelRoadFeature(in_partition INTEGER, line GEOMETRY) @@ -298,4 +298,4 @@ BEGIN RAISE EXCEPTION 'Unknown partition %', in_partition; END $$ -LANGUAGE plpgsql IMMUTABLE; +LANGUAGE plpgsql STABLE;