Skip to content

Open Infrastructure Map, a view of infrastructure data in OpenStreetMap

Attempt to simplify voltage parsing again

Russ Garrett15a8945c615d
diff --git a/schema/functions.sql b/schema/functions.sql
index 5fea140..4ecd957 100644
--- a/schema/functions.sql
+++ b/schema/functions.sql
@@ -37,29 +37,24 @@ END
$$ LANGUAGE plpgsql;
-- Select the highest voltage from a semicolon-delimited list
--- NOTE: the EXCEPTION clause here shouldn't be needed, and it's
--- annoying because it makes this PARALLEL UNSAFE. However it was
--- causing issues on imposm import with the (apparent) value
--- '138000.69000.69000'. Oddly this worked on the console, but
--- not through imposm.
CREATE OR REPLACE FUNCTION convert_voltage(value TEXT) RETURNS NUMERIC
IMMUTABLE
-PARALLEL UNSAFE -- uses EXCEPTION
+PARALLEL SAFE
RETURNS NULL ON NULL INPUT
AS $$
-DECLARE
- parts TEXT[];
- res NUMERIC;
-BEGIN
- parts := regexp_matches(value, '([0-9][0-9,]*)[;]?.*', '');
- BEGIN
- res := replace(parts[1], ',', '.')::NUMERIC;
- EXCEPTION WHEN OTHERS THEN
- res := NULL;
- END;
- RETURN res;
-END
-$$ LANGUAGE plpgsql;
+ SELECT value FROM convert_integer_list(value) ORDER BY value DESC LIMIT 1;
+$$ LANGUAGE SQL;
+
+-- Convert a semicolon-delimited list of integers into a table of integer voltages.
+CREATE OR REPLACE FUNCTION convert_integer_list(value TEXT) RETURNS TABLE (value INTEGER)
+IMMUTABLE
+PARALLEL SAFE
+RETURNS NULL ON NULL INPUT
+AS $$
+ SELECT convert_integer(v) AS voltage
+ FROM regexp_split_to_table(value, ';') AS v
+ WHERE convert_integer(v) IS NOT NULL;
+$$ LANGUAGE SQL;
-- Convert a text numeric value into a number. Both the period (.) and the comma (,)
-- are accepted as decimal separators.