obs_getdata takes api_method and api_args in both forms, and handles them correctly; cleanup to getdata, added more tests
This commit is contained in:
parent
c2dc4fb8b9
commit
afe4c27dd5
@ -386,12 +386,20 @@ BEGIN
|
|||||||
(unnest($1))->>'geom_type' geom_type,
|
(unnest($1))->>'geom_type' geom_type,
|
||||||
(unnest($1))->>'geom_timespan' geom_timespan,
|
(unnest($1))->>'geom_timespan' geom_timespan,
|
||||||
(unnest($1))->>'numer_timespan' numer_timespan,
|
(unnest($1))->>'numer_timespan' numer_timespan,
|
||||||
(unnest($1))->>'normalization' normalization
|
(unnest($1))->>'normalization' normalization,
|
||||||
|
(unnest($1))->>'api_method' api_method,
|
||||||
|
(unnest($1))->'api_args' api_args
|
||||||
)
|
)
|
||||||
SELECT String_Agg(
|
SELECT String_Agg(
|
||||||
-- numeric
|
-- numeric
|
||||||
'(''{' || CASE WHEN LOWER(numer_type) LIKE 'numeric' THEN
|
'JSON_Build_Object(' || CASE
|
||||||
'"value": '' || ' || CASE
|
WHEN api_method IS NOT NULL THEN
|
||||||
|
'''value'', ' ||
|
||||||
|
'cdb_observatory.FIRST( ' ||
|
||||||
|
api_method || '.' || numer_colname || ')::' || numer_type
|
||||||
|
-- numeric internal values
|
||||||
|
WHEN LOWER(numer_type) LIKE 'numeric' THEN
|
||||||
|
'''value'', ' || CASE
|
||||||
-- denominated
|
-- denominated
|
||||||
WHEN LOWER(normalization) LIKE 'denom%' OR (normalization IS NULL AND denom_id IS NOT NULL)
|
WHEN LOWER(normalization) LIKE 'denom%' OR (normalization IS NULL AND denom_id IS NOT NULL)
|
||||||
THEN 'cdb_observatory.FIRST(' || numer_tablename || '.' || numer_colname ||
|
THEN 'cdb_observatory.FIRST(' || numer_tablename || '.' || numer_colname ||
|
||||||
@ -406,30 +414,34 @@ BEGIN
|
|||||||
|
|
||||||
-- categorical/text
|
-- categorical/text
|
||||||
WHEN LOWER(numer_type) LIKE 'text' THEN
|
WHEN LOWER(numer_type) LIKE 'text' THEN
|
||||||
'"value": "'' || ' || 'cdb_observatory.FIRST(' || numer_tablename || '.' || numer_colname || ') || ''"'''
|
'''value'', ' || 'cdb_observatory.FIRST(' || numer_tablename || '.' || numer_colname || ') '
|
||||||
|
|
||||||
-- geometry
|
-- geometry
|
||||||
WHEN numer_id IS NULL THEN
|
WHEN numer_id IS NULL THEN
|
||||||
'"geomref": "'' || ' || 'cdb_observatory.FIRST(' || geom_tablename ||
|
'''geomref'', ' || 'cdb_observatory.FIRST(' || geom_tablename ||
|
||||||
'.' || geom_geomref_colname || ') || ''", ' ||
|
'.' || geom_geomref_colname || '), ' ||
|
||||||
'"value": "'' || ' || 'cdb_observatory.FIRST(' || geom_tablename ||
|
'''value'', ' || 'cdb_observatory.FIRST(' || geom_tablename ||
|
||||||
'.' || geom_colname || ')::TEXT || ''"'''
|
'.' || geom_colname || ')'
|
||||||
ELSE ''
|
ELSE ''
|
||||||
END || ' || ''}'')::JSON', ', ')
|
END || ')', ', ')
|
||||||
AS colspecs,
|
AS colspecs,
|
||||||
|
|
||||||
(SELECT String_Agg(DISTINCT CASE
|
(SELECT String_Agg(DISTINCT CASE
|
||||||
-- External API
|
-- External API
|
||||||
WHEN tablename LIKE 'cdb_observatory.%' THEN
|
WHEN tablename LIKE 'cdb_observatory.%' THEN
|
||||||
'LATERAL (SELECT * FROM ' || tablename ||
|
'LATERAL (SELECT * FROM ' || tablename || ') ' ||
|
||||||
'(_geoms.geom)) ' || REPLACE(tablename, 'cdb_observatory.', '')
|
REPLACE(split_part(tablename, '(', 1), 'cdb_observatory.', '')
|
||||||
-- Internal obs_ table
|
-- Internal obs_ table
|
||||||
ELSE 'observatory.' || tablename
|
ELSE 'observatory.' || tablename
|
||||||
END, ', ') FROM (
|
END, ', ') FROM (
|
||||||
SELECT DISTINCT UNNEST(tablenames_ary) tablename FROM (
|
SELECT DISTINCT UNNEST(tablenames_ary) tablename FROM (
|
||||||
SELECT ARRAY_AGG(numer_tablename) ||
|
SELECT ARRAY_AGG(numer_tablename) ||
|
||||||
ARRAY_AGG(denom_tablename) ||
|
ARRAY_AGG(denom_tablename) ||
|
||||||
ARRAY_AGG(geom_tablename)
|
ARRAY_AGG(geom_tablename) ||
|
||||||
|
ARRAY_AGG('cdb_observatory.' || api_method || '(_geomrefs.id' || COALESCE(', ' ||
|
||||||
|
(SELECT STRING_AGG(REPLACE(val::text, '"', ''''), ', ')
|
||||||
|
FROM (SELECT json_array_elements(api_args) as val) as vals),
|
||||||
|
'') || ')')
|
||||||
tablenames_ary
|
tablenames_ary
|
||||||
) tablenames_inner
|
) tablenames_inner
|
||||||
) tablenames_outer) tablenames,
|
) tablenames_outer) tablenames,
|
||||||
@ -451,12 +463,13 @@ BEGIN
|
|||||||
|
|
||||||
RETURN QUERY EXECUTE format($query$
|
RETURN QUERY EXECUTE format($query$
|
||||||
WITH _geomrefs AS (SELECT UNNEST($1) as id)
|
WITH _geomrefs AS (SELECT UNNEST($1) as id)
|
||||||
SELECT _geomrefs.id, Array_to_JSON(ARRAY[%s])
|
SELECT _geomrefs.id, Array_to_JSON(ARRAY[%s]::JSON[])
|
||||||
FROM %s, _geomrefs
|
FROM _geomrefs, %s
|
||||||
WHERE %s %s
|
%s
|
||||||
GROUP BY _geomrefs.id
|
GROUP BY _geomrefs.id
|
||||||
ORDER BY _geomrefs.id
|
ORDER BY _geomrefs.id
|
||||||
$query$, colspecs, tables, NULLIF(obs_wheres, '') || ' AND ', user_wheres)
|
$query$, colspecs, tables,
|
||||||
|
'WHERE ' || NULLIF(ARRAY_TO_STRING(ARRAY[obs_wheres, user_wheres], ' AND '), ''))
|
||||||
USING geomrefs;
|
USING geomrefs;
|
||||||
RETURN;
|
RETURN;
|
||||||
END;
|
END;
|
||||||
@ -510,18 +523,20 @@ BEGIN
|
|||||||
(unnest($1))->>'geom_type' geom_type,
|
(unnest($1))->>'geom_type' geom_type,
|
||||||
(unnest($1))->>'numer_timespan' numer_timespan,
|
(unnest($1))->>'numer_timespan' numer_timespan,
|
||||||
(unnest($1))->>'geom_timespan' geom_timespan,
|
(unnest($1))->>'geom_timespan' geom_timespan,
|
||||||
(unnest($1))->>'normalization' normalization
|
(unnest($1))->>'normalization' normalization,
|
||||||
|
(unnest($1))->>'api_method' api_method,
|
||||||
|
(unnest($1))->'api_args' api_args
|
||||||
)
|
)
|
||||||
SELECT String_Agg(
|
SELECT String_Agg(
|
||||||
'(''{' || CASE
|
'JSON_Build_Object(' || CASE
|
||||||
-- api-delivered values
|
-- api-delivered values
|
||||||
WHEN numer_tablename LIKE 'cdb_observatory.%' THEN
|
WHEN api_method IS NOT NULL THEN
|
||||||
'"value": "'' || ' ||
|
'''value'', ' ||
|
||||||
'(cdb_observatory.FIRST( ' ||
|
'cdb_observatory.FIRST( ' ||
|
||||||
REPLACE(numer_tablename, 'cdb_observatory.', '') || '.' || numer_colname || '))::TEXT || ''"'''
|
api_method || '.' || numer_colname || ')::' || numer_type
|
||||||
-- numeric internal values
|
-- numeric internal values
|
||||||
WHEN LOWER(numer_type) LIKE 'numeric' THEN
|
WHEN LOWER(numer_type) LIKE 'numeric' THEN
|
||||||
'"value": '' || ' || CASE
|
'''value'', ' || CASE
|
||||||
-- denominated
|
-- denominated
|
||||||
WHEN LOWER(normalization) LIKE 'denom%' OR (normalization IS NULL AND denom_id IS NOT NULL)
|
WHEN LOWER(normalization) LIKE 'denom%' OR (normalization IS NULL AND denom_id IS NOT NULL)
|
||||||
THEN ' CASE ' ||
|
THEN ' CASE ' ||
|
||||||
@ -592,36 +607,45 @@ BEGIN
|
|||||||
|
|
||||||
-- categorical/text
|
-- categorical/text
|
||||||
WHEN LOWER(numer_type) LIKE 'text' THEN
|
WHEN LOWER(numer_type) LIKE 'text' THEN
|
||||||
'"value": "'' || ' || 'MODE() WITHIN GROUP (ORDER BY ' || numer_tablename || '.' || numer_colname || ') || ''"'''
|
'''value'', ' || 'MODE() WITHIN GROUP (ORDER BY ' || numer_tablename || '.' || numer_colname || ') '
|
||||||
|
|
||||||
-- geometry
|
-- geometry
|
||||||
WHEN numer_id IS NULL THEN
|
WHEN numer_id IS NULL THEN
|
||||||
'"geomref": "'' || ' || geom_tablename || '.' || geom_geomref_colname || ' || ''", ' ||
|
'''geomref'', ' || geom_tablename || '.' || geom_geomref_colname || ', ' ||
|
||||||
'"value": "'' || ' || 'cdb_observatory.FIRST(' || geom_tablename ||
|
'''value'', ' || 'cdb_observatory.FIRST(' || geom_tablename ||
|
||||||
'.' || geom_colname || ')::TEXT || ''"'''
|
'.' || geom_colname || ')::TEXT'
|
||||||
-- code below will return the intersection of the user's geom and the
|
-- code below will return the intersection of the user's geom and the
|
||||||
-- OBS geom
|
-- OBS geom
|
||||||
--'"value": "'' || ' || 'cdb_observatory.FIRST(ST_Intersection(_geoms.geom, ' || geom_tablename ||
|
--'"value": "'' || ' || 'cdb_observatory.FIRST(ST_Intersection(_geoms.geom, ' || geom_tablename ||
|
||||||
-- '.' || geom_colname || '))::TEXT || ''"'''
|
-- '.' || geom_colname || '))::TEXT || ''"'''
|
||||||
ELSE ''
|
ELSE ''
|
||||||
END || ' || ''}'')::JSON', ', ')
|
END || ')', ', ')
|
||||||
AS colspecs,
|
AS colspecs,
|
||||||
|
|
||||||
STRING_AGG(REPLACE(geom_tablename, 'cdb_observatory.', '') ||
|
-- geomrefs, used to separate out rows in case we don't want to merge
|
||||||
|
-- results by user input IDs
|
||||||
|
--
|
||||||
|
-- api_method and geom_tablename are interchangeable since when an
|
||||||
|
-- api_method is passed, geom_tablename is ignored
|
||||||
|
STRING_AGG(COALESCE(geom_tablename, api_method) ||
|
||||||
'.' || geom_geomref_colname, ', ') AS geomrefs,
|
'.' || geom_geomref_colname, ', ') AS geomrefs,
|
||||||
|
|
||||||
(SELECT String_Agg(DISTINCT CASE
|
(SELECT String_Agg(DISTINCT CASE
|
||||||
-- External API
|
-- External API
|
||||||
WHEN tablename LIKE 'cdb_observatory.%' THEN
|
WHEN tablename LIKE 'cdb_observatory.%' THEN
|
||||||
'LATERAL (SELECT * FROM ' || tablename ||
|
'LATERAL (SELECT * FROM ' || tablename || ') ' ||
|
||||||
'(_geoms.geom)) ' || REPLACE(tablename, 'cdb_observatory.', '')
|
REPLACE(split_part(tablename, '(', 1), 'cdb_observatory.', '')
|
||||||
-- Internal obs_ table
|
-- Internal obs_ table
|
||||||
ELSE 'observatory.' || tablename
|
ELSE 'observatory.' || tablename
|
||||||
END, ', ') FROM (
|
END, ', ') FROM (
|
||||||
SELECT DISTINCT UNNEST(tablenames_ary) tablename FROM (
|
SELECT DISTINCT UNNEST(tablenames_ary) tablename FROM (
|
||||||
SELECT ARRAY_AGG(numer_tablename) ||
|
SELECT ARRAY_AGG(numer_tablename) ||
|
||||||
ARRAY_AGG(denom_tablename) ||
|
ARRAY_AGG(denom_tablename) ||
|
||||||
ARRAY_AGG(geom_tablename)
|
ARRAY_AGG(geom_tablename) ||
|
||||||
|
ARRAY_AGG('cdb_observatory.' || api_method || '(_geoms.geom' || COALESCE(', ' ||
|
||||||
|
(SELECT STRING_AGG(REPLACE(val::text, '"', ''''), ', ')
|
||||||
|
FROM (SELECT json_array_elements(api_args) as val) as vals),
|
||||||
|
'') || ')')
|
||||||
tablenames_ary
|
tablenames_ary
|
||||||
) tablenames_inner
|
) tablenames_inner
|
||||||
) tablenames_outer) tablenames,
|
) tablenames_outer) tablenames,
|
||||||
|
@ -105,6 +105,18 @@ t
|
|||||||
obs_getmeasurebyid_null_id
|
obs_getmeasurebyid_null_id
|
||||||
t
|
t
|
||||||
(1 row)
|
(1 row)
|
||||||
obs_getdata_api
|
obs_getdata_api_geomvals_no_args
|
||||||
|
t
|
||||||
|
(1 row)
|
||||||
|
obs_getdata_api_geomvals_args_numer_return
|
||||||
|
t
|
||||||
|
(1 row)
|
||||||
|
obs_getdata_api_geomvals_args_string_return
|
||||||
|
t
|
||||||
|
(1 row)
|
||||||
|
obs_getdata_api_geomrefs_args_numer_return
|
||||||
|
t
|
||||||
|
(1 row)
|
||||||
|
obs_getdata_api_geomrefs_args_string_return
|
||||||
t
|
t
|
||||||
(1 row)
|
(1 row)
|
||||||
|
@ -217,9 +217,28 @@ SELECT cdb_observatory.OBS_GetMeasureById(
|
|||||||
'2010 - 2014'
|
'2010 - 2014'
|
||||||
) IS NULL As OBS_GetMeasureById_null_id;
|
) IS NULL As OBS_GetMeasureById_null_id;
|
||||||
|
|
||||||
-- OBS_GetData with an API
|
-- OBS_GetData with an API + geomvals, no args
|
||||||
SELECT ARRAY['us.census.tiger.census_tract'] <@ array_agg(data->0->>'value') AS OBS_GetData_API
|
SELECT ARRAY['us.census.tiger.census_tract'] <@ array_agg(data->0->>'value') AS OBS_GetData_API_geomvals_no_args
|
||||||
FROM cdb_observatory.obs_getdata(array[(cdb_observatory._testarea(), 1)::geomval],
|
FROM cdb_observatory.obs_getdata(array[(cdb_observatory._testarea(), 1)::geomval],
|
||||||
'[{"numer_coltype": "text", "numer_colname": "boundary_id", "numer_tablename": "cdb_observatory.obs_getavailableboundaries", "geom_tablename": "cdb_observatory.obs_getavailableboundaries", "geom_geomref_colname": "boundary_id"}]',
|
'[{"numer_type": "text", "numer_colname": "boundary_id", "api_method": "obs_getavailableboundaries", "geom_geomref_colname": "boundary_id"}]',
|
||||||
false);
|
false);
|
||||||
|
|
||||||
|
-- OBS_GetData with an API + geomvals, args, numeric
|
||||||
|
SELECT json_typeof(data->0->'value') = 'number' AS OBS_GetData_API_geomvals_args_numer_return
|
||||||
|
FROM cdb_observatory.obs_getdata(array[(cdb_observatory._testarea(), 1)::geomval],
|
||||||
|
'[{"numer_type": "numeric", "numer_colname": "obs_getmeasure", "api_method": "obs_getmeasure", "api_args": ["us.census.acs.B01003001"]}]', false);
|
||||||
|
|
||||||
|
-- OBS_GetData with an API + geomvals, args, text
|
||||||
|
SELECT json_typeof(data->0->'value') = 'string' AS OBS_GetData_API_geomvals_args_string_return
|
||||||
|
FROM cdb_observatory.obs_getdata(array[(cdb_observatory._testarea(), 1)::geomval],
|
||||||
|
'[{"numer_type": "text", "numer_colname": "obs_getcategory", "api_method": "obs_getcategory", "api_args": ["us.census.spielman_singleton_segments.X55"]}]', false);
|
||||||
|
|
||||||
|
-- OBS_GetData with an API + geomrefs, args, numeric
|
||||||
|
SELECT json_typeof(data->0->'value') = 'number' AS OBS_GetData_API_geomrefs_args_numer_return
|
||||||
|
FROM cdb_observatory.obs_getdata(array['36047076200'],
|
||||||
|
'[{"numer_type": "numeric", "numer_colname": "obs_getmeasurebyid", "api_method": "obs_getmeasurebyid", "api_args": ["us.census.acs.B01003001", "us.census.tiger.census_tract"]}]');
|
||||||
|
|
||||||
|
-- OBS_GetData with an API + geomrefs, args, text
|
||||||
|
SELECT json_typeof(data->0->'value') = 'string' AS OBS_GetData_API_geomrefs_args_string_return
|
||||||
|
FROM cdb_observatory.obs_getdata(array['36047'],
|
||||||
|
'[{"numer_type": "text", "numer_colname": "obs_getboundarybyid", "api_method": "obs_getboundarybyid", "api_args": ["us.census.tiger.county"]}]');
|
||||||
|
Loading…
Reference in New Issue
Block a user