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.
This commit is contained in:
Mario de Frutos 2018-02-14 14:24:08 +01:00
parent 5df9e5b850
commit 32ce9c1bba
2 changed files with 14 additions and 5 deletions

View File

@ -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']);

View File

@ -260,8 +260,17 @@ 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
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;