i'd like to create a user defined aggregation function. My own type:
CREATE TYPE state AS(
reservoir integer[5],
skipcnt int,
reservoir_size int
);
SFUNC:
create function res_trans (currentstate state,newsample int)
returns state
as $$
DECLARE
pos integer := 0;
BEGIN
IF currentstate.skipcnt = -1 THEN
currentstate.skipcnt := 5;
ELSIF currentstate.skipcnt = 0 THEN
pos := floor(random()*currentstate.reservoir_size+1);
currentstate.reservoir := res_array_replace(currentstate.reservoir,pos,newsample);
currentstate.skipcnt := 5;
ELSE
currentstate.skipcnt := currentstate.skipcnt - 1;
END IF;
RETURN currentstate;
END;
$$LANGUAGE plpgsql;
finalfunc:
CREATE FUNCTION finalize_trans(finalstate state) RETURNS integer[]
AS $$
BEGIN
RETURN finalstate.reservoir;
END;
$$LANGUAGE plpgsql;
All the functions above can be created successfully. However, when i create the aggregation. It shows some error.
CREATE AGGREGATE reservoir_sampling(int)
(
INITCOND = ROW('{1,2,3,4,5}',-1,5),
STYPE = state,
SFUNC = res_trans,
FINALFUNC = finalize_trans
);
enter image description here Is there a problem of the initcond? Please help me, Thanks!
I tried to let the initcond = null and deal with the null case in SFUNC, but i got enter image description here