Created
December 6, 2016 18:06
-
-
Save phillip-haydon/02e1cda346b4900a3e0090cefe85b176 to your computer and use it in GitHub Desktop.
starting of a postgresql jsonb aggregate function to merge documents together
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
create or replace function jsonb_or(state jsonb[], current_val jsonb) returns jsonb[] as $$ | |
if (!state) { | |
state = []; | |
} | |
state.push(current_val); | |
return state; | |
$$ language plv8; | |
create or replace function jsonb_or_final(state jsonb[]) returns int as $$ | |
-- todo or the docs together... | |
return state.length; | |
$$ language plv8; | |
create aggregate jsonb_or_agg(jsonb) | |
( | |
sfunc = jsonb_or, | |
stype = jsonb[], | |
finalfunc = jsonb_or_final | |
); | |
select jsonb_or_agg(data) from test_actions group by action_set; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment