dataservices-api/client/sql/21_bulk_geocoding_functions.sql

93 lines
3.7 KiB
MySQL
Raw Normal View History

CREATE OR REPLACE FUNCTION cdb_dataservices_client.cdb_bulk_geocode_street_point (query text,
2018-07-11 01:06:49 +08:00
street_column text, city_column text default null, state_column text default null, country_column text default null, batch_size integer DEFAULT NULL)
2018-06-15 15:55:52 +08:00
RETURNS SETOF cdb_dataservices_client.geocoding AS $$
DECLARE
2018-06-22 15:41:29 +08:00
query_row_count integer;
enough_quota boolean;
remaining_quota integer;
2018-07-11 01:06:49 +08:00
max_batch_size integer;
cartodb_id_batch integer;
batches_n integer;
DEFAULT_BATCH_SIZE CONSTANT numeric := 100;
2018-07-11 01:06:49 +08:00
MAX_SAFE_BATCH_SIZE CONSTANT numeric := 5000;
2018-06-22 18:35:29 +08:00
temp_table_name text;
username text;
orgname text;
apikey_permissions json;
2018-06-15 15:55:52 +08:00
BEGIN
IF session_user = 'publicuser' OR session_user ~ 'cartodb_publicuser_*' THEN
RAISE EXCEPTION 'The api_key must be provided';
END IF;
2018-09-10 18:54:37 +08:00
SELECT u, o, p INTO username, orgname, apikey_permissions FROM cdb_dataservices_client._cdb_entity_config() AS (u text, o text, p json);
2018-09-06 21:40:41 +08:00
IF apikey_permissions IS NULL OR NOT apikey_permissions::jsonb ? 'geocoding' THEN
RAISE EXCEPTION 'Geocoding is not allowed';
END IF;
-- JSON value stored "" is taken as literal
IF username IS NULL OR username = '' OR username = '""' THEN
RAISE EXCEPTION 'Username is a mandatory argument, check it out';
END IF;
2018-07-11 01:06:49 +08:00
SELECT csqi.monthly_quota - csqi.used_quota AS remaining_quota, csqi.max_batch_size
INTO remaining_quota, max_batch_size
FROM cdb_dataservices_client.cdb_service_quota_info_batch() csqi
2018-07-11 01:06:49 +08:00
WHERE service = 'hires_geocoder';
RAISE DEBUG 'remaining_quota: %; max_batch_size: %', remaining_quota, max_batch_size;
IF batch_size IS NULL THEN
2018-07-11 01:06:49 +08:00
batch_size := max_batch_size;
ELSIF batch_size > max_batch_size THEN
RAISE EXCEPTION 'batch_size must be lower than %', max_batch_size + 1;
END IF;
IF batch_size > MAX_SAFE_BATCH_SIZE THEN
batch_size := MAX_SAFE_BATCH_SIZE;
END IF;
2018-07-17 19:24:39 +08:00
EXECUTE format('SELECT count(1), ceil(count(1)::float/%s) FROM (%s) _x', batch_size, query)
INTO query_row_count, batches_n;
RAISE DEBUG 'cdb_bulk_geocode_street_point --> query_row_count: %; query: %; country: %; state: %; city: %; street: %',
query_row_count, query, country_column, state_column, city_column, street_column;
2018-06-22 15:41:29 +08:00
SELECT cdb_dataservices_client.cdb_enough_quota('hires_geocoder', query_row_count) INTO enough_quota;
2018-07-11 01:06:49 +08:00
IF remaining_quota < query_row_count THEN
2018-06-22 15:41:29 +08:00
RAISE EXCEPTION 'Remaining quota: %. Estimated cost: %', remaining_quota, query_row_count;
END IF;
RAISE DEBUG 'batches_n: %', batches_n;
2018-06-22 18:35:29 +08:00
temp_table_name := 'bulk_geocode_street_' || md5(random()::text);
EXECUTE format('CREATE TEMPORARY TABLE %s ' ||
'(cartodb_id integer, the_geom geometry(Multipolygon,4326), metadata jsonb)',
temp_table_name);
select
coalesce(street_column, ''''''), coalesce(city_column, ''''''),
coalesce(state_column, ''''''), coalesce(country_column, '''''')
into street_column, city_column, state_column, country_column;
2018-07-11 01:06:49 +08:00
IF batches_n > 0 THEN
FOR cartodb_id_batch in 0..(batches_n - 1)
LOOP
EXECUTE format(
'WITH geocoding_data as (' ||
' SELECT ' ||
' json_build_object(''id'', cartodb_id, ''address'', %s, ''city'', %s, ''state'', %s, ''country'', %s) as data , ' ||
' floor((row_number() over () - 1)::float/$1) as batch' ||
2018-07-11 01:06:49 +08:00
' FROM (%s) _x' ||
') ' ||
'INSERT INTO %s SELECT (cdb_dataservices_client._cdb_bulk_geocode_street_point(jsonb_agg(data))).* ' ||
'FROM geocoding_data ' ||
2018-07-17 19:24:39 +08:00
'WHERE batch = $2', street_column, city_column, state_column, country_column, query, temp_table_name)
USING batch_size, cartodb_id_batch;
2018-07-11 01:06:49 +08:00
END LOOP;
END IF;
2018-06-22 18:35:29 +08:00
RETURN QUERY EXECUTE 'SELECT * FROM ' || quote_ident(temp_table_name);
2018-06-15 15:55:52 +08:00
END;
$$ LANGUAGE 'plpgsql' SECURITY DEFINER VOLATILE PARALLEL UNSAFE;