The INIT function

The INIT aggregate support function performs the initialization for the user-defined aggregate.

The following table summarizes possible initialization tasks.
Table 1. Initialization tasks for the INIT aggregate support function
Initialization task More information
Setup for any additional resources outside the state that the aggregation might need Aggregate support functions that the algorithm requires
Initial calculations that the user-defined aggregate might need Aggregate support functions that the algorithm requires
Allocation and initialization of the aggregate state that the rest of the aggregation computation might need Aggregate support functions for the aggregate state
Handling of an optional setup argument Implement a setup argument

The INIT support function is an optional aggregate support function. If your aggregate algorithm does not require any of the tasks in Initialization tasks for the INIT aggregate support function, you do not need to define an INIT function. When you omit the INIT function from your user-defined aggregate, the database server performs the state management for the aggregate state. For more information, see Handling a simple state.

To declare an INIT support function as a C function, use the following syntax:
agg_state init_func(dummy_arg, set_up_arg)
   agg_arg_type dummy_arg;
   set_up_type set_up_arg; /* optional */
agg_state
The data type of the aggregate state.
dummy_arg
A placeholder parameter that has the same data type as the aggregate argument that this aggregate support function is to handle for the user-defined aggregate.
init_func
The name of the INIT aggregate support function.
set_up_arg
An optional parameter for the setup argument. For more information, see Implement a setup argument.

In the execution of a UDA, the database server calls the INIT function before it begins the actual aggregation computation. It passes in any optional setup argument (set_up_arg) from the user-defined aggregate and copies any initialized aggregate state that INIT returns into the state buffer. For more information about the state buffer, see Aggregate support functions for the aggregate state.

The first argument of the INIT function serves only to identify the data type of the aggregate argument that the user-defined aggregate handles. The routine manager uses this argument in routine resolution to determine the correct version of the overloaded INIT function. At the time of invocation, the routine manager just passes in a NULL value for the first argument of the INIT function, as the following syntax shows:
agg_state init_func(NULL, optional setup argument)
Tip: For more information about how the routine manager resolves the overloaded aggregate support functions, see the section on aggregates in the Informix® User-Defined Routines and Data Types Developer's Guide.
The following code fragment shows the INIT aggregate support function that handles an INTEGER argument for the SQSUM1 user-defined aggregate (which A sample user-defined aggregate describes).
Figure 1: The INIT aggregate support function for SQSUM1 on INTEGER
/* SQSUM1 INIT support function on INTEGER */
mi_integer init_sqsum1(dummy_arg)
   mi_integer dummy_arg;
{
   return (0);
}

For other aggregate support functions of SQSUM1, see The ITER aggregate support function for SQSUM1 on INTEGER, The COMBINE aggregate support function for SQSUM1 on INTEGER, and The FINAL aggregate support function for SQSUM1 on INTEGER.