Thread: Strange aggregate

Strange aggregate

From
Teodor Sigaev
Date:
Hi!


Is there possibility to write aggregate returns setof value?

I want to implement some statistic aggregate for tsearch:

....
create type statinfo as (word text, ndoc int4, nentry int4);

CREATE FUNCTION ts_accum(txtstat,txtidx)
RETURNS txtstat
AS 'MODULE_PATHNAME'
LANGUAGE 'C' with (isstrict);

create function ts_accum_finish(txtstat)        returns setof statinfo        as 'MODULE_PATHNAME'        language 'C'
     with (isstrict);
 

CREATE AGGREGATE stat (        BASETYPE=txtidx,        SFUNC=ts_accum,        STYPE=txtstat,        FINALFUNC =
ts_accum_finish,       initcond = ''
 
);


and output like this:

# select * from ts_accum_finish(ts_accum('','qwer:1,2,3,345,65 sd:5,3 qwer:5,6,7')); word | ndoc | nentry
------+------+-------- sd   |    1 |      2 qwer |    1 |      8
(2 rows)

But any my tries gives:
# select stat(a) from test_txtidx;
ERROR:  function called in context that does not accept a set result

I know that is not right way to call setof functions, but how in this case?
Or give more simple way, pls...
Thank you.

Note, I'm working on tsearch V2, which isn't in CVS yet, only on our GiST page:
http://www.sai.msu.su/~megera/postgres/gist/

-- 
Teodor Sigaev                                  E-mail: teodor@sigaev.ru



Re: Strange aggregate

From
Tom Lane
Date:
Teodor Sigaev <teodor@sigaev.ru> writes:
> Is there possibility to write aggregate returns setof value?

Not with the present implementation.

I suppose we could think about postponing the call to the finalfunction
so that it's executed by execQual.c while evaluating an Agg node's
output tlist, and then something like that could work.  But I'm not sure
we really want to go that way.  Set-returning functions in targetlists
are a mess that we should try to get rid of, not enhance.

Can you think of a plausible syntax for an aggregate as a table function
(ie, in a FROM item, instead of a target list)?
        regards, tom lane