Thread: aggregate function

aggregate function

From
Viktor Bojović
Date:
I am trying to make aggregate function of existing function which looks like this.
CREATE OR REPLACE FUNCTION "grafika"."pov_sphere" (x numeric, y numeric, z numeric, rad numeric, pigment varchar) RETURNS varchar AS
$body$
DECLARE
_pov varchar;
BEGIN
_pov:='sphere {<'||x||','||y||','||z||'>,'||rad||' '||pigment ||'}';
return _pov;
END;
$body$
LANGUAGE 'plpgsql'
IMMUTABLE
RETURNS NULL ON NULL INPUT
SECURITY INVOKER
COST 100;


Aggregate should concatenate results of pov_sphere using this function below.
CREATE OR REPLACE FUNCTION "public"."concat" (varchar, varchar) RETURNS varchar AS
$body$
DECLARE
t varchar;
BEGIN
IF character_length($1) > 0 THEN
t = $1 || $2;
ELSE
t = $2;
END IF;
RETURN t;
END;
$body$
LANGUAGE 'plpgsql'
VOLATILE
CALLED ON NULL INPUT
SECURITY INVOKER
COST 100;


I tried to write this part below, but something is wrong (ERROR: function grafika.pov_sphere(character varying, numeric, numeric, numeric, numeric, character varying) does not exist) so I wanted to ask if someone knows how to solve this problem.

CREATE AGGREGATE "grafika"."agg_pov_sphere" (NUMERIC, NUMERIC, NUMERIC, NUMERIC, VARCHAR) (
SFUNC = "grafika"."pov_sphere",
STYPE = "varchar",
FINALFUNC = "public"."grp_concat"
);

Thanx in advance.
--
---------------------------------------
Viktor Bojović
---------------------------------------
Wherever I go, Murphy goes with me

Re: aggregate function

From
Tom Lane
Date:
Viktor Bojović <viktor.bojovic@gmail.com> writes:
> I tried to write this part below, but something is wrong (ERROR: function
> grafika.pov_sphere(character varying, numeric, numeric, numeric, numeric,
> character varying) does not exist) so I wanted to ask if someone knows how
> to solve this problem.

Well, it's right: you didn't create such a function.  The aggregate
transition function has to take the state datatype as its first
argument.  You can't just plug these two randomly-chosen functions
into an aggregate definition and expect the system to intuit what you
want done.  I think what you actually need is a transition function
that works something like
if ($1 is null)    return agg_pov_sphere($2,$3,...);else    return concat($1, agg_pov_sphere($2,$3,...));
        regards, tom lane