16.20 - Basic Algorithm of an Aggregate UDF - Advanced SQL Engine - Teradata Database

Teradata Vantage™ - SQL External Routine Programming

Advanced SQL Engine
Teradata Database
Release Number
Release Date
April 2020
Content Type
Programming Reference
Publication ID
English (United States)

Teradata Database invokes an aggregate UDF once for each item in an aggregation group, passing the detail values of a group through the input arguments. To accumulate summary information, the method must retain context each time it is called.

You do not need to understand or worry about how to create a group or how to code an aggregate UDF to deal with groups, because Teradata takes care of that. You only need to write the basic algorithm of combining the data passed to it to produce the desired result.

The basic algorithm consists of five possible phases of execution that an aggregate UDF goes through. To determine the phase of execution, and what actions the method must perform during each phase, call the getPhase() method on the phase input argument. The return value is a constant of the Phase class.

The following table describes the flow of an aggregate UDF. To see the flow of a window aggregate UDF, see Window Aggregate UDFs.

Phase Value of phase.getPhase() Description
1 Phase.AGR_INIT This is the first time Teradata Database is invoking the aggregate UDF for an aggregation group.

The method must:

  • Allocate intermediate storage and initialize it.
  • Process the first detail passed into the method.
2 Phase.AGR_DETAIL In this phase, Teradata Database calls the method once for each row to be aggregated for each group.

The method must combine the UDF argument input data with the intermediate storage defined for the group.

3 Phase.AGR_COMBINE This phase combines results from different AMPs for a specific group.

The method must combine the data for the two intermediate storage areas passed in.

4 Phase.AGR_FINAL No more input is expected for the group. The method must produce the final result for the group.
5 Phase.AGR_NODATA This phase is only presented when there is absolutely no data to aggregate.