------------------------------------------------------------------------------------
-- this file defines the "or_aggregate" operation, which computes 1-(1-x1)*(1-x2)*...
------------------------------------------------------------------------------------
--
-- define a new type for the state variable of the aggregate function
drop type if exists state_type cascade;
create type state_type as (f1 float);
--
-- define the incremental aggregate function
--
drop function if exists or_udf (state_type, float) cascade;
create or replace function or_udf (state_type, float) returns state_type as
'select $1.f1 * (1.0 - $2)'
language SQL;
--
-- example usage of the incremental aggregate function
--
-- (doesn't work for me...)
-- select or_udf((0.5), 0.5);
--
-- define the final return function
--
drop function if exists final_udf (state_type);
create or replace function final_udf (state_type) returns float as
'select 1.0 - ($1.f1)'
language SQL;
--
-- define the actual user-defined aggregate
--
drop aggregate if exists or_aggregate (float);
create aggregate or_aggregate (float)
(
   sfunc = or_udf,
   stype = state_type,
   finalfunc = final_udf,
   initcond = '(1.0)'
);

