User Defined Aggregates


#1

I am wanting to develop some user Defined Aggregates, and I read in the documentation that it is something that you are planning on implementing.

  1. is it implemented now?
    a) if yes, is there documentation/examples for creating them? or some advice on how to do it?
    b) if no, what advice can be offered if I wanted to add this functionality? What code should I look at?

Thank you,
Alan


#2

Hi Alan,

Glad you asked. I did some work in this area.
In 11.06 there is no notion of an “aggregate” as a separate structure. There are only “operators” which accept an array and return another array. There are, of course, ops like sum() and avg() that take an array and return a single value - so you could call those aggregates.

In the upcoming release (what used to be 11.10 and is now likely going to be 11.12 :smile:) we actually treat aggregates as a separate class of objects and allow them to be user defined. We then create higher-level ops that apply aggregates to arrays in various ways. Here are some examples out of 11.12:

AFL% show(test_arr);
[("test_arr<val:int64 NOT NULL> [y=1:4,4,0,x=1:4,4,0]")]
AFL% scan(test_arr);
[[(1),(1),(1),(1)],
 [(2),(2),(2),(2)],
 [(3),(3),(3),(3)],
 [(4),(4),(4),(4)]]

--These few aggregates come canned with the system:
AFL% list('aggregates');
[("avg"),("count"),("max"),("min"),("stdev"),("sum"),("var")]

-- Here are various ops that apply aggregates:
AFL% aggregate(test_arr, sum(val));
[(40)]

-- group by dimension:
AFL% aggregate(test_arr, sum(val), x);
[(10),(10),(10),(10)]
AFL% aggregate(test_arr, sum(val), y);
[(4),
 (8),
(12),
(16)]

-- regrid:
AFL% regrid(test_arr, 2, 2, sum(val));
[[(6),(6)],
[(14),(14)]]

-- window:
AFL% window(test_arr, 2, 2, sum(val));
[[(1),(2),(2),(2)],
 [(3),(6),(6),(6)],
 [(5),(10),(10),(10)],
[(7),(14),(14),(14)]]

-- redimension (the cool stuff) - this query calculates sum of even and odd numbers in the array:
AFL% redimension( apply( test_arr, even_or_odd, iif(val % 2 = 0, 1, 0)), <vsum:int64 null, empty_tag:indicator> [even_or_odd=0:1,1,0], sum(val) as vsum);
[{0}(16)];
[{1}(24)]

So if you can wait for 11.12 to come out, which should be pretty soon, you can pretty easily add your own aggs (like sum) to the mix. Or you can always define your own operators to do the job as well.

Hope it helps.


#3

Just a follow up on this.
Looks like the new way of making aggregates have been implemented long time ago. Is there any documentation on how to make a new aggregate?

Thanks


#4

Hello,

Doc is currently overloaded and the UDA API is still fluid. The best I can give you at the moment is a toy example - see: viewtopic.php?f=18&t=1122