From 090a1add43e916395084db1c9e628817ed8b894c Mon Sep 17 00:00:00 2001 From: John Krauss Date: Mon, 3 Apr 2017 19:44:00 +0000 Subject: [PATCH] add suggested_name output to OBS_GetMeta. fixes #279 --- doc/measures_functions.md | 1 + src/pg/sql/41_observatory_augmentation.sql | 9 +++++++++ .../expected/41_observatory_augmentation_test.out | 9 +++++++++ .../test/sql/41_observatory_augmentation_test.sql | 15 +++++++++++++++ 4 files changed, 34 insertions(+) diff --git a/doc/measures_functions.md b/doc/measures_functions.md index 6ee563e..255e7bf 100644 --- a/doc/measures_functions.md +++ b/doc/measures_functions.md @@ -246,6 +246,7 @@ fail. Metadata Output Key | Description --- | ----------- +suggested_name | A suggested column name for adding this to an existing table numer_id | Identifier for desired measurement numer_timespan | Timespan that will be used of the desired measurement numer_name | Human-readable name of desired measure diff --git a/src/pg/sql/41_observatory_augmentation.sql b/src/pg/sql/41_observatory_augmentation.sql index 98ff24f..710da76 100644 --- a/src/pg/sql/41_observatory_augmentation.sql +++ b/src/pg/sql/41_observatory_augmentation.sql @@ -180,6 +180,14 @@ BEGIN ), meta AS (SELECT id, f.numer_id, + LOWER(TRIM(BOTH '_' FROM regexp_replace(CASE WHEN f.numer_id IS NOT NULL + THEN CASE + WHEN normalization ILIKE 'area%%' THEN numer_colname || ' per sq km' + WHEN normalization ILIKE 'denom%%' THEN numer_colname || ' rate' + ELSE numer_colname + END || ' ' || m.numer_timespan + ELSE geom_name || ' ' || m.geom_timespan + END, '[^a-zA-Z0-9]+', '_', 'g'))) suggested_name, CASE WHEN f.numer_id IS NULL THEN NULL ELSE numer_aggregate END numer_aggregate, CASE WHEN f.numer_id IS NULL THEN NULL ELSE numer_colname END numer_colname, CASE WHEN f.numer_id IS NULL THEN NULL ELSE numer_geomref_colname END numer_geomref_colname, @@ -237,6 +245,7 @@ BEGIN 'timespan_rank', dense_rank() OVER (PARTITION BY id ORDER BY numer_timespan DESC), 'score_rank', dense_rank() OVER (PARTITION BY id ORDER BY score DESC), 'score', scores.score, + 'suggested_name', cdb_observatory.FIRST(meta.suggested_name), 'numer_aggregate', cdb_observatory.FIRST(meta.numer_aggregate), 'numer_colname', cdb_observatory.FIRST(meta.numer_colname), 'numer_geomref_colname', cdb_observatory.FIRST(meta.numer_geomref_colname), diff --git a/src/pg/test/expected/41_observatory_augmentation_test.out b/src/pg/test/expected/41_observatory_augmentation_test.out index 5ab5336..c445127 100644 --- a/src/pg/test/expected/41_observatory_augmentation_test.out +++ b/src/pg/test/expected/41_observatory_augmentation_test.out @@ -150,6 +150,15 @@ t|t|t|t|t|t|t|t|t|t|t|t|t|t|t obs_getmeta_conflicting_metadata t (1 row) +obs_getmeta_suggested_name +t +(1 row) +obs_getmeta_suggested_name_area +t +(1 row) +obs_getmeta_suggested_name_denom +t +(1 row) obs_getdata_geomval_empty_null t (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 838f35c..472f141 100644 --- a/src/pg/test/sql/41_observatory_augmentation_test.sql +++ b/src/pg/test/sql/41_observatory_augmentation_test.sql @@ -398,6 +398,21 @@ SELECT cdb_observatory.OBS_GetMeta(cdb_observatory._TestPoint(), '[{"numer_id": "us.census.acs.B01001002", "denom_id": "us.census.acs.B01001002", "geom_id": "us.census.tiger.census_tract"}]') IS NULL AS obs_getmeta_conflicting_metadata; +-- OBS_GetMeta provides suggested name for simple meta request +SELECT cdb_observatory.OBS_GetMeta(cdb_observatory._TestPoint(), + '[{"numer_id": "us.census.acs.B01003001"}]' +)->0->>'suggested_name' = 'total_pop_2011_2015' obs_getmeta_suggested_name; + +-- OBS_GetMeta provides suggested name for simple meta request with area norm +SELECT cdb_observatory.OBS_GetMeta(cdb_observatory._TestPoint(), + '[{"numer_id": "us.census.acs.B01003001", "normalization": "area"}]' +)->0->>'suggested_name' = 'total_pop_per_sq_km_2011_2015' obs_getmeta_suggested_name_area; + +-- OBS_GetMeta provides suggested name for simple meta request with denom +SELECT cdb_observatory.OBS_GetMeta(cdb_observatory._TestPoint(), + '[{"numer_id": "us.census.acs.B01001002", "normalization": "denom"}]' +)->0->>'suggested_name' = 'male_pop_rate_2011_2015' obs_getmeta_suggested_name_denom; + -- OBS_GetData/OBS_GetMeta by id with empty list/null WITH data AS (SELECT * FROM cdb_observatory.OBS_GetData(ARRAY[]::TEXT[], null)) SELECT ARRAY_AGG(data) IS NULL AS obs_getdata_geomval_empty_null FROM data;