drop type jj_state cascade;

CREATE TYPE jj_state AS ( x numeric, y numeric);


CREATE OR REPLACE FUNCTION jj_transition(
  state jj_state,
  val numeric
) RETURNS jj_state AS $$
BEGIN
  RETURN NULL::jj_state;
END;
$$ LANGUAGE plpgsql;

CREATE OR REPLACE FUNCTION jj_final(
  state jj_state
) RETURNS numeric AS $$
BEGIN
  RETURN 5.5;
END;
$$ LANGUAGE plpgsql;

CREATE OR REPLACE FUNCTION jj_inverse(state jj_state, val numeric)
RETURNS jj_state AS $$
BEGIN
  RETURN NULL::jj_state;
END;
$$ LANGUAGE plpgsql;

CREATE AGGREGATE jj(numeric) (
  sfunc      = jj_transition,
  stype      = jj_state,
  finalfunc  = jj_final,
  initcond   = '(0,0)',
  msfunc     = jj_transition,
  minvfunc   = jj_inverse,
  mstype     = jj_state
  -- mfinalfunc = average_final
);
