Skip to content
This repository has been archived by the owner on Mar 28, 2024. It is now read-only.

Commit

Permalink
Update json functions to more closely match Redshift, fixes HearthSim#3
Browse files Browse the repository at this point in the history
Redshift returns NULL when a value cannot be found.
Redshift strips quotes from strings returned in the JSON.
Redshift checks for valid index when using json_extract_array_element_text.
  • Loading branch information
shawnpyle committed Jan 18, 2022
1 parent ce246e9 commit 8647065
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
22 changes: 13 additions & 9 deletions sql/01_functions.sql
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
-- json_extract_array_element_text()
CREATE FUNCTION json_extract_array_element_text(json_array text, array_index int) RETURNS text immutable as $$
import json
result = json.loads(json_array)[array_index]
return json.dumps(result)
CREATE OR REPLACE FUNCTION json_extract_array_element_text(json_array text, array_index int) RETURNS text immutable as $$
import json
items = json.loads(json_array)
if 0 <= array_index and array_index < len(items):
return json.dumps(items[array_index]).strip('"')
else:
return None
$$ LANGUAGE plpythonu;

-- json_extract_path_text()
CREATE FUNCTION json_extract_path_text(json_string text, VARIADIC path_elems character[]) RETURNS text immutable as $$
import json
result = json.loads(json_string)
if path_elem not in result: return ""
result = result[path_elem]
return json.dumps(result)
import json
result = json.loads(json_string)
for path_elem in path_elems:
if path_elem not in result: return None
result = result[path_elem]
return json.dumps(result).strip('"')
$$ LANGUAGE plpythonu;

-- json_array_length()
Expand Down
18 changes: 18 additions & 0 deletions test/01_functions.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
-- These should work in Redshift and the docker container as of
-- select version(); -- PostgreSQL 8.0.2 on i686-pc-linux-gnu, compiled by GCC gcc (GCC) 3.4.2 20041017 (Red Hat 3.4.2-6.fc3), Redshift 1.0.34691

SELECT json_extract_array_element_text('[1,2,3]', -1); -- NULL
SELECT json_extract_array_element_text('[1,2,3]', 0); -- 1
SELECT json_extract_array_element_text('[1,2,3]', 3); -- NULL
SELECT json_extract_array_element_text('["a","b","c"]', -1); -- NULL
SELECT json_extract_array_element_text('["a","b","c"]', 2); -- c
SELECT json_extract_array_element_text('["a","b","c"]', 3); -- NULL

-- Built in pg_catalog.json_extract_path_text exists so we need to include the schema when calling.
-- See https://www.postgresql.org/docs/10/functions-json.html
SELECT public.json_extract_path_text('{"a":{"b":{"c":1}},"d":[2,3],"e":"f"}', 'a'); -- {"b": {"c": 1}}
SELECT public.json_extract_path_text('{"a":{"b":{"c":1}},"d":[2,3],"e":"f"}','a.b'); -- NULL
SELECT public.json_extract_path_text('{"a":{"b":{"c":1}},"d":[2,3],"e":"f"}','d'); -- [2, 3]
SELECT public.json_extract_path_text('{"a":{"b":{"c":1}},"d":[2,3],"e":"f"}','e'); -- f

SELECT NOW(), getdate(); -- 2022-01-18 13:47:09.566 -0500, 2022-01-18 18:47:10.000

0 comments on commit 8647065

Please sign in to comment.