Allocating Intermediate Aggregate Storage | Teradata Vantage - 17.10 - Allocating Storage - Advanced SQL Engine - Teradata Database

Teradata Vantageā„¢ - SQL External Routine Programming

Product
Advanced SQL Engine
Teradata Database
Release Number
17.10
Release Date
July 2021
Content Type
Programming Reference
Publication ID
B035-1147-171K
Language
English (United States)

During the AGR_INIT aggregation phase, a UDF must allocate the intermediate storage pointed to by FNC_Context_t.interim1.

To allocate the required memory, the UDF calls the FNC_DefMem library function, specifying the size of the structure, which has a 64000 byte maximum. The UDF cannot request more memory than the value of interim_size in the CLASS AGGREGATE clause of the CREATE FUNCTION statement. If interim_size is omitted, then the limit is 64 bytes.

For best performance, allocate only enough memory to satisfy the needs of the function for intermediate storage.

In the standard deviation function example, the size of the structure can be computed by using sizeof(AGR_Storage). The following statement allocates the intermediate storage pointed to by FNC_Context_t.interim1:

s1 = FNC_DefMem(sizeof(AGR_Storage));
FNC_DefMem returns a NULL pointer for any of the following conditions:
  • The aggregate function asks for more memory than the value of interim_size in the CLASS AGGREGATE clause of the CREATE FUNCTION statement
  • The CLASS AGGREGATE clause of the CREATE FUNCTION statement does not specify a value for interim_size and the aggregate function asks for more than the default 64 bytes
  • The function asks for more than the 64000 byte maximum

If the UDF cannot complete without the memory, then the function can return an error by setting up the SQLSTATE error results argument.