Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2a3a528

Browse files
authored
json_unnest_recursive() fix performance problem, test improved
1 parent 87e8826 commit 2a3a528

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

‎functions/json_unnest_recursive.sql‎

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ AS $func$
1515
with recursive r (path, value, member_of) as
1616
(
1717
select
18+
--distinct --[42883] ERROR: could not identify an equality operator for type json
19+
distinct on (array[k.key], v.value::text, t.type) --!!! fix performance problem
1820
array[k.key],
1921
v.value,
2022
t.type
@@ -47,15 +49,31 @@ $func$;
4749
comment on function public.json_unnest_recursive(data json) is 'Recursive parse nested JSON (arrays and objects), returns keys and its values';
4850

4951

52+
------------------------------------------------------------------------------------------------------------------------
53+
--TEST
54+
5055
--TEST AND USING EXAMPLE
5156
select cardinality(path) as level, *
5257
from public.json_unnest_recursive('{"id":123,"g":null,"a":[9,8,4,5],"name":"unknown", "7": 3}'::json)
5358
order by level, member_of, path;
5459

60+
5561
/*
5662
-- Example: find all emails in JSON data
57-
select path, value #>> '{}' as email
58-
from public.json_unnest_recursive('[{"name":"Mike", "age": 45, "emails":[null, "mike.1977@gmail.com"]}]'::json)
59-
where json_typeof(value) = 'string'
60-
and public.is_email(value #>> '{}');
63+
select j.path, v.value as email
64+
from public.json_unnest_recursive('[{"name":"Mike", "age": 45, "emails":[null, "mike.1977@gmail.com", ""]}]'::json) as j
65+
cross join nullif(j.value #>> '{}', '') as v(value) --cast jsonb scalar to text (can be null)
66+
where json_typeof(j.value) = 'string'
67+
and v.value is not null
68+
and public.is_email(v.value);
6169
*/
70+
71+
do $$
72+
begin
73+
assert (select count(*) = 8
74+
from public.json_unnest_recursive(
75+
'{"id":123,"g":null,"a":[9,8,4,5],"name":"unknown", "7": 3}'::json
76+
)
77+
);
78+
end;
79+
$$;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /