graphile / graphile/pg-aggregates

Supporting user-defined aggregate functions

Open
#62 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
89
Forks
19
PR merge metrics
No merged PRs in 30d

Description

### Summary

Supporting user-defined aggregate functions

### Additional context

I have a custom type and a user-defined aggregate function that works on that type.

```
CREATE TYPE public.amount AS (
number numeric,
currency text
);

CREATE FUNCTION public.sum(state public.amount[], current public.amount) RETURNS public.amount[]
LANGUAGE plpgsql
AS $$
DECLARE
found boolean = false;
i int = 0;
BEGIN
IF array_length(state, 1) IS NULL THEN
RETURN array[current];
END IF;

FOR i IN 1..array_length(state, 1)
LOOP
IF state[i].currency = current.currency THEN
state[i].number := state[i].number + current.number;
found := true;
EXIT;
END IF;
END LOOP;

IF NOT found THEN
state := array_append(state, current);
END IF;

RETURN state;
END;
$$;

CREATE AGGREGATE public.sum(public.amount) (
SFUNC = public.sum,
STYPE = public.amount[],
INITCOND = '{}'
);
```

Unsurprisingly, columns using the custom `amount` type are not recognized as aggregatable by the plugin. I couldn't find any mention of user-defined aggregate functions in the documentation so I assume they are not supported. Would it be easy to add support? Any simple workarounds here to add support?

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.