Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add setup notebook #8

Open
duncandewhurst opened this issue Oct 6, 2020 · 0 comments
Open

Add setup notebook #8

duncandewhurst opened this issue Oct 6, 2020 · 0 comments

Comments

@duncandewhurst
Copy link
Contributor

duncandewhurst commented Oct 6, 2020

Setup notebook

The setup notebook is out of sync with the structure of the database.

The most important thing it adds is the flatten function (source).

drop function if exists flatten;

CREATE OR REPLACE FUNCTION flatten(jsonb)
    RETURNS TABLE(path text, object_property integer, array_item integer) 
    LANGUAGE 'sql'
    PARALLEL SAFE
AS
$$
WITH RECURSIVE all_paths(path, "value", "object_property", "array_item") AS (
    select 
		 key "path", 
         value "value", 
         1 "object_property",
         0 "array_item"
    from 
         jsonb_each($1)
    UNION ALL (
        select 
            case when key_value is not null then
                path || '/'::text || (key_value).key::text
            else
                path
            end "path",
            case when key_value is not null then
                (key_value).value
            else
                array_value
            end "value",
            case when key_value is not null then 1 else 0 end,
            case when key_value is null then 1 else 0 end
       from
          (select 
             path,
             jsonb_each(case when jsonb_typeof(value) = 'object' then value else '{}'::jsonb end) key_value,
             jsonb_array_elements(case when jsonb_typeof(value) = 'array' and jsonb_typeof(value -> 0) = 'object' then value else '[]'::jsonb end) "array_value"
             from all_paths
          ) a
     )
  )
  SELECT path, object_property, array_item FROM all_paths;
$$;
@duncandewhurst duncandewhurst changed the title Remove credentials and add setup notebook Add setup notebook Jul 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant