From cef99c6343ced28359cf942531989a819fcc83b8 Mon Sep 17 00:00:00 2001 From: Mario de Frutos Date: Tue, 8 Aug 2017 19:02:13 +0200 Subject: [PATCH] OBS_MetadataValidation New function to check the metadata in order to search for errors like for example if we have the metadata for a median aggregation and the normalization is by are it'll fail. --- src/pg/sql/41_observatory_augmentation.sql | 43 +++++++++++++++++++ .../41_observatory_augmentation_test.out | 9 ++++ .../sql/41_observatory_augmentation_test.sql | 6 +++ 3 files changed, 58 insertions(+) diff --git a/src/pg/sql/41_observatory_augmentation.sql b/src/pg/sql/41_observatory_augmentation.sql index 971cb82..3cd6866 100644 --- a/src/pg/sql/41_observatory_augmentation.sql +++ b/src/pg/sql/41_observatory_augmentation.sql @@ -1076,3 +1076,46 @@ BEGIN RETURN result; END; $$ LANGUAGE plpgsql STABLE; + +-- MetadataValidation checks the metadata parameters and the geometry type +-- of the data in order to find possible wrong cases +CREATE OR REPLACE FUNCTION cdb_observatory.obs_metadatavalidation( + geometry_extent geometry(Geometry, 4326), + geometry_type text, + params JSON, + target_geoms INTEGER DEFAULT NULL +) +RETURNS TABLE(valid boolean, errors text[]) AS $$ +DECLARE + meta json; + errors text[]; +BEGIN + errors := (ARRAY[])::TEXT[]; + IF geometry_type IN ('ST_Polygon', 'ST_MultiPolygon') THEN + FOR meta IN EXECUTE 'SELECT json_array_elements(cdb_observatory.OBS_GetMeta($1, $2, 1, 1, $3))' USING geometry_extent, params, target_geoms + LOOP + IF (meta->>'normalization' = 'denominated' AND meta->>'denom_id' is NULL) THEN + errors := array_append(errors, 'Normalizated measure should have a numerator and a denominator. Please review the provided options.'); + END IF; + IF (meta->>'numer_aggregate' IS NULL) THEN + errors := array_append(errors, 'For polygon geometries, aggregation is mandatory. Please review the provided options'); + END IF; + IF (meta->>'numer_aggregate' IN ('median', 'average') AND meta->>'denom_id' IS NULL) THEN + errors := array_append(errors, 'Median or average aggregation for polygons requires a denominator to provide weights. Please review the provided options'); + END IF; + IF (meta->>'numer_aggregate' IN ('median', 'average') AND meta->>'normalization' NOT LIKE 'pre%') THEN + errors := array_append(errors, format('Median or average aggregation only supports prenormalized normalization, %s passed. Please review the provided options', meta->>'normalization')); + END IF; + END LOOP; + + IF CARDINALITY(errors) > 0 THEN + RETURN QUERY EXECUTE 'SELECT FALSE, $1' USING errors; + ELSE + RETURN QUERY SELECT TRUE, ARRAY[]::TEXT[]; + END IF; + ELSE + RETURN QUERY SELECT TRUE, ARRAY[]::TEXT[]; + END IF; + RETURN; +END; +$$ LANGUAGE plpgsql STABLE; diff --git a/src/pg/test/expected/41_observatory_augmentation_test.out b/src/pg/test/expected/41_observatory_augmentation_test.out index a08f8ae..33b1265 100644 --- a/src/pg/test/expected/41_observatory_augmentation_test.out +++ b/src/pg/test/expected/41_observatory_augmentation_test.out @@ -304,3 +304,12 @@ tract_sample|tract_max_error|tract_avg_error|tract_min_error no_bg_point_error t (1 row) +valid|errors +t|{} +(1 row) +valid|errors +f|{"Median or average aggregation only supports prenormalized normalization, denominated passed. Please review the provided options"} +(1 row) +valid|errors +f|{"Normalizated measure should have a numerator and a denominator. Please review the provided options."} +(1 row) diff --git a/src/pg/test/sql/41_observatory_augmentation_test.sql b/src/pg/test/sql/41_observatory_augmentation_test.sql index 39ad22e..be5e00f 100644 --- a/src/pg/test/sql/41_observatory_augmentation_test.sql +++ b/src/pg/test/sql/41_observatory_augmentation_test.sql @@ -973,3 +973,9 @@ WITH _geoms AS ( FROM geoms, results WHERE cartodb_id = id ; + +-- OBS_MetadataValidation + +SELECT * FROM cdb_observatory.OBS_MetadataValidation(NULL, 'ST_Polygon', '[{"numer_id": "us.census.acs.B01003001","denom_id": null,"normalization": "prenormalized","geom_id": null,"numer_timespan": "2010 - 2014"}]'::json, 500); +SELECT * FROM cdb_observatory.OBS_MetadataValidation(NULL, 'ST_Polygon', '[{"numer_id": "us.census.acs.B25058001","denom_id": null,"normalization": "denominated","geom_id": null,"numer_timespan": "2010 - 2014"}]'::json, 500); +SELECT * FROM cdb_observatory.OBS_MetadataValidation(NULL, 'ST_Polygon', '[{"numer_id": "us.census.acs.B15003001","denom_id": null,"normalization": "denominated","geom_id": null,"numer_timespan": "2010 - 2014"}]'::json, 500);