Aggregate
Executes an external function for all tuples of a relation.
Usage
aggregate (indata, func-expr, indata-attr, new-attr : outdata)
Details
All tuples of the relation indata are passed to the
function specified in func-expr. The function's return value
is then joined back to the packed relation indata as
new-attr to produce outdata (i.e., a dependent join).
The packed relation indata is contained in the new attribute
indata-attr.
A func-expr is a call to the remote function that specifies how
attributes in the indata relation are to be passed.
Notes
- Functions should be written in Java and adhere to the following
constraints:
- Functions must be statically declared.
- Functions must consist of only one
java.util.ArrayList
input parameter and must return either
java.lang.String, java.util.Date,
int, or double types. The values
returned are automatically converted into CHAR, DATE, NUMBER,
and NUMBER (respectively). Any other types are automatically
coverted into strings (CHAR).
- Function calls can accept multiple parameters (attributes) from
the input relation indata - use commas to separate each parameter.
Known Bugs
- Functions cannot accept literals.
Example
Using the input:
RELATION books: title char, author char, pub_date date, pages number
Title1|Author1|09-01-1991|34
Title2|Author2|12-23-1954|479
Title3|Author2|05-09-2002|733
Title4|Author3|01-01-1968|32
Title5|Author2|07-03-2001|1152
and the externally compiled MyFunctions.java that contains:
import java.text.*;
import java.util.*;
public class MyFunctions
{
public static double average (ArrayList a_data)
{
double sum = 0;
for (int i=0; i<a_data.size(); i++) {
sum += Double.parseDouble(a_data.get(i).toString());
}
return sum/((double)a_data.size());
}
}
when executing the plan:
PLAN test
{
INPUT: stream books
OUTPUT: stream result
BODY
{
aggregate (books, "MyFunctions.average(pages)", "old_rel", "avg_pages" : post-agg)
project (post-agg, "avg_pages" : result)
}
}
generates the following output:
----------------------------------------------
RELATION: uaggregate1_result
attrs: avg_pages
----------------------------------------------
486.0
----------------------------------------------