Apama 10.15.0 | Developing Apama Applications | Developing Apama Applications in EPL | Working with Streams and Stream Queries | Defining custom aggregate functions
 
Defining custom aggregate functions
 
Example of defining a custom aggregate function
Defining actions in custom aggregate functions
Overloading in custom aggregate functions
Distinguishing duplicate values in custom aggregate functions
EPL provides a number of commonly used aggregate functions that you can specify in the select clause of a query. See Aggregating items in projections. If none of these functions perform the operation you need, you can define a custom aggregate function. The format for defining a custom aggregate function is as follows:
aggregate [bounded|unbounded] aggregateName ([arglist])
   returns retType { aggregateBody }
Element
Description
bounded | unbounded
Specify bounded when you are defining a custom aggregate function that will work with only a bounded window. That is, the query cannot specify retain all.
Specify unbounded when you are defining a custom aggregate function that will work with only an unbounded window. That is, the query must specify retain all.
Do not specify either bounded or unbounded when you are defining a custom aggregate function that will work with either a bounded or an unbounded window.
If you do not specify bounded, you must define the custom aggregate function so that it can handle a window that never removes items. The function should not consume memory per item in the window.
aggregateName
Specify a name for your aggregate function. This is the name you will specify when you call the function in a select clause.
For details about the characters you can specify, see Identifiers.
arglist
Optionally, specify one or more comma-separated type/name pairs. Each pair indicates the type and the name of an argument that you are passing to the function. For example, (float price, integer quantity).
retType
Specify any EPL type. This is the type of the value that your function returns.
aggregateBody
The body of a custom aggregate function is similar to an event body. It can contain fields that are specific to one instance of the custom aggregate function and actions to operate on the state. The init(), add(), remove() and value() actions are special. They define how stream queries interact with custom aggregate functions.
You define custom aggregate functions outside of an event or a monitor, and the function's scope is the package in which you declare it. To use custom aggregate functions in other packages, specify the function's fully-qualified name, for example:
from a in all A() select com.myCorporation.custom.myCustomAggregate(a)
Alternatively, you can specify a using statement. For example, suppose you define the myCustomAggregate() function in the com.myCorporation.custom package. To use that function inside another package, insert a statement such as the following in the file that contains the monitor in which you want to use the function:
using com.myCorporation.custom.myCustomAggregate;
Insert the using statement after the optional package declaration, but before any other declarations. You can then simply specify the function name. For example:
from a in all A() select myCustomAggregate(a)
Be sure to inject the file that contains the function definition before you inject the files that contain monitors that use the function.
See also Names.