informix
Extending Informix Dynamic Server 2000
Creating User-Defined Aggregates

Extending Existing Aggregates

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.

Overloading Operators for Built-In Aggregates

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.

Aggregate Required Operators Return Type
AVG plus(udt, udt),
divide(udt, integer)
Return type of divide()
DISTINCT
(or UNIQUE)
compare(udt, udt) Boolean
COUNT -- (no new operators required) Integer
MAX greaterthanorequal(udt, udt) Boolean
MIN lesthanorequal(udt, udt) Boolean
RANGE lessthanorequal(udt, udt),
greaterthanorequal(udt, udt),
minus(udt, udt)
Return type of minus()
SUM plus(udt, udt) Return type of plus()
STDEV times(udt, udt),
divide(udt, integer),
plus(udt, udt),
minus(udt, udt),
sqrt(udt)
Return type of divide()
VARIANCE times(udt, udt),
divide(udt, integer),
plus(udt, udt),
minus(udt, udt)
Return type of divide()

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.

Extending an Aggregate

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.

To extend a built-in aggregate

  1. Develop functions to overload the required operators.
  2. Register each function with a CREATE FUNCTION statement.
  3. For more information, refer to Registering a User-Defined Routine.

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.

Example of Extending a Built-In Aggregate

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:


Extending Informix Dynamic Server 2000, Version 9.2
Copyright © 1999, Informix Software, Inc. All rights reserved