Basic Algorithm of a Window Aggregate Function | Teradata Vantage - Basic Algorithm of a Window Aggregate Function - Analytics Database - Teradata Vantage

SQL External Routine Programming

Deployment
VantageCloud
VantageCore
Edition
Enterprise
IntelliFlex
VMware
Product
Analytics Database
Teradata Vantage
Release Number
17.20
Published
June 2022
ft:locale
en-US
ft:lastEdition
2025-03-30
dita:mapPath
iiv1628111441820.ditamap
dita:ditavalPath
qkf1628213546010.ditaval
dita:id
qnu1472247494689
lifecycle
latest
Product Category
Teradata Vantageā„¢

The following table describes the flow of a window aggregate function for a group.

Phase Value of aggregation_phase Argument Description
1 AGR_INIT This phase is triggered once per partition when starting evaluation of a new aggregation group.
The function must:
  • Allocate and initialize intermediate storage for accumulating data passed during aggregation phases.
  • Save the first set of data values passed in through arguments into the intermediate storage area.
2 AGR_DETAIL This phase is triggered every time the forward row progresses.

Vantage calls the function once for each row to be aggregated for each group.

The function must accumulate the argument input data into the intermediate storage defined for the specific group.

3 AGR_COMBINE This phase is not applicable for window aggregate functions, but may be included to generate an error if this phase is reached erroneously.

This phase is used for aggregate UDFs. For more information, see Aggregate Function Parameter List.

6 AGR_MOVINGTRAIL This phase is applicable for the moving window type.

This phase is triggered by the last rows of a moving window when the forward pointer to the window reaches the end of the partition.

The phase does not provide any new values to the function, but indicates to the function that processing has reached the end of the partition.

The function can use this phase to adjust the necessary internal count and offset values to reflect the actual size as the window diminishes towards the end of the partition.

4 AGR_FINAL This phase is invoked at the time the final evaluated result needs to be moved into the result row.

No more input is expected for the group, and the function produces the final aggregate result for the group.

5 AGR_NODATA This phase is only presented when there is absolutely no data to aggregate.

The function provides a result for a null aggregate set.

You must maintain a cache of rows corresponding to the window size. You may want to maintain a counter value that indicates the total rows read so far. For example, if C represents the counter, then the window of rows for evaluation is:

if (C < window_size),
   window size would be C
else
   window size would be as indicated in function context.

The base of the window is C - window_size and the end of the window corresponds to C.

The complexities of the window combinations ( for example, PRECEDING/FOLLOWING/CURRENT) are handled by Vantage. You only need to maintain the window cache of rows and implement the semantics of the function on this cache of rows.