From 32ce9c1bbadcf0510ae631b85b7f5605a06e9d71 Mon Sep 17 00:00:00 2001 From: Mario de Frutos Date: Wed, 14 Feb 2018 14:24:08 +0100 Subject: [PATCH] Fix namedplaces function that changed its ouputs in PG10 Chained unnests have changed its behavior in PG10. In previous versions if the unnested arrays had different cardinality the function automatically fills the gaps duplicating the content of the array with less cardinality but not anymore. --- .../extension/expected/50_namedplaces_test.out | 2 +- geocoder/extension/sql/0.0.1/50_namedplaces.sql | 17 +++++++++++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/geocoder/extension/expected/50_namedplaces_test.out b/geocoder/extension/expected/50_namedplaces_test.out index feabe8a..1fd53f5 100644 --- a/geocoder/extension/expected/50_namedplaces_test.out +++ b/geocoder/extension/expected/50_namedplaces_test.out @@ -42,8 +42,8 @@ SELECT (geocode_namedplace(Array['Portland', 'Portland', 'New York City'], Array q | a1 | c | geom | success ---------------+--------+-----+------+--------- New York City | | USA | | f - Portland | Oregon | USA | | f Portland | Maine | USA | | f + Portland | Oregon | USA | | f (3 rows) SELECT namedplace_guess_country(Array['granada', 'jaen', 'cordoba', 'madrid', 'valladolid']); diff --git a/geocoder/extension/sql/0.0.1/50_namedplaces.sql b/geocoder/extension/sql/0.0.1/50_namedplaces.sql index 893d257..f55abe8 100644 --- a/geocoder/extension/sql/0.0.1/50_namedplaces.sql +++ b/geocoder/extension/sql/0.0.1/50_namedplaces.sql @@ -225,7 +225,7 @@ CREATE OR REPLACE FUNCTION geocode_namedplace(places text[], admin1s text[], inp next AS (SELECT p.s AS q, p.a1 AS a1, (SELECT gp.the_geom FROM global_cities_points_limited gp, global_cities_alternates_limited ga WHERE lower(p.s) = ga.lowername AND ga.admin1 = p.i AND ga.geoname_id = gp.geoname_id ORDER BY preferred DESC LIMIT 1) geom FROM p WHERE p.s NOT IN (SELECT q FROM best WHERE geom IS NOT NULL)) SELECT q, a1, inputcountry as c, geom, TRUE AS success FROM best WHERE geom IS NOT NULL UNION ALL - SELECT q, a1, inputcountry as c, geom, CASE WHEN geom IS NULL THEN FALSE ELSE TRUE END AS success FROM next + SELECT q, a1, inputcountry as c, geom, CASE WHEN geom IS NULL THEN FALSE ELSE TRUE END AS success FROM next LOOP RETURN NEXT ret; END LOOP; @@ -260,9 +260,18 @@ CREATE OR REPLACE FUNCTION geocode_namedplace(places text[], admin1s text[], inp RETURN NEXT ret; END LOOP; ELSE - FOR ret IN WITH clean AS (SELECT array_agg(p) p, array_agg(a) a, c FROM (SELECT p, a, c FROM (SELECT unnest(places) p, unnest(admin1s) a, unnest(inputcountry) c) z GROUP BY p, a, c) y GROUP BY c) - SELECT (geocode_namedplace(p, a, c)).* FROM clean LOOP - RETURN NEXT ret; + FOR ret IN WITH clean AS ( + SELECT array_agg(p) p, array_agg(a) a, c + FROM (SELECT p, a, c + FROM (SELECT p, a, c, nest.ordinality as ord FROM unnest(places, admin1s) with ordinality nest (p, a), LATERAL unnest(inputcountry) with ordinality c) z + GROUP BY p, a, c, z.ord + ORDER BY z.ord + ) y + GROUP BY c + ) + SELECT (geocode_namedplace(p, a, c)).* FROM clean + LOOP + RETURN NEXT ret; END LOOP; END IF; RETURN;