Using a C Structure to Access the Storage Area - 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
Language
English (United States)
Last Update
2023-07-11
dita:mapPath
iiv1628111441820.ditamap
dita:ditavalPath
qkf1628213546010.ditaval
dita:id
B035-1147
lifecycle
latest
Product Category
Teradata Vantageā„¢

The type of intermediate results that an aggregate function needs to save depends on the type of calculation that the function performs.

Consider a standard deviation function STD_DEV(x) that uses the following equation:



Based on the calculation, the function needs to store the following:
  • N
  • sum(X 2)
  • sum(X)

The function can declare a C structure with components that match the necessary intermediate values:

typedef struct agr_storage {
    FLOAT n;
    FLOAT x_sq;
    FLOAT x_sum;
} AGR_Storage;

The function can then define a pointer to AGR_Storage that points to the FNC_Context_t.interim1 function argument.

Suppose the function declaration uses the following parameters:

void STD_DEV ( FNC_Phase     phase,
               FNC_Context_t *fctx,
               FLOAT         *x,
               FLOAT         *result,
               char          sqlstate[6] )
{
     ...
}

The following statement defines a pointer to AGR_Storage named s1 that points to the FNC_Context_t.interim1 function argument:

AGR_Storage *s1 = fctx->interim1;

The function can then easily access the storage area using the pointer to AGR_Storage. For example:

s1->n     = 0;
s1->x_sq  = 0;
s1->x_sum = 0;