|
The database server provides built-in aggregate functions, such as SUM and COUNT, that operate on the built-in data types. You can extend a built-in aggregate so that it can operate on user-defined data types. To extend a built-in aggregate, you must create user-defined routines that overload several binary operators.
The following table shows the operators that you must overload for each of the built-in aggregates. For example, if you need only the SUM aggregate for a user-defined data type, you need to overload only the plus() operator.
The database server uses the compare() operator for indexing as well as for DISTINCT and UNIQUE aggregations. You must create the compare() function as an external function. You cannot use the compare() operator with user-defined types that are not hashable. For a description of hashable user-defined types, refer to Hashable Data Types.
When you extend a built-in aggregate to include a user-defined data type, you do not use the CREATE AGGREGATE statement because the aggregate itself already exists.
After you have registered the functions that overload the binary operators, you can use the built-in aggregates in an SQL statement.
For the syntax of the CREATE FUNCTION statement, see the Informix Guide to SQL: Syntax. For more information about writing overloaded functions, refer to Overloading Routines. For information about writing functions in external languages, refer to the DataBlade API Programmer's Manual or Creating UDRs in Java.
The following example uses SPL functions to overload the plus() and divide() operators for a row type, complex, that represents a complex number. After you overload the operators, you can use the SUM, AVG, and COUNT operators with complex.
You can now use the extended aggregates as follows: