C Function Definition - 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™
#define SQL_TEXT Latin_Text
#include <sqltypes_td.h>
#include <string.h>

/* Aggregate storage area */
typedef struct arg_storage {
   CHARACTER ConcatOfColumn[20000] ;
   } AgrStorage ;

void udf_agch002002udt(FNC_Phase      phase,
                       FNC_Context_t *fContext,
                       UDT_HANDLE    *ColumnValueUdt,
                       UDT_HANDLE    *resultUdt,
                       int           *ColumnValueUdtIndicator,
                       int           *ResultUdtIndicator,
                       char           sqlstate[6],
                       SQL_TEXT       ExternalName[129],
                       SQL_TEXT       SpecificName[129],
                       SQL_TEXT       ErrorMsg[257])
{
   char ColumnValue[20000]; // Buffer to hold retrieved column value
   int length;              // Length of retrieved value
   char result[20000];
   AgrStorage *s1 = fContext->interim1 ; // Pointers to intermediate
   AgrStorage *s2 = fContext->interim2 ; //   storage areas

   /* Switch to determine aggregation phase */
   switch (phase)
      {
      case AGR_INIT:
         /* Allocate and initialize the intermediate storage */
         if ((s1 = FNC_DefMem(sizeof(AgrStorage))) == NULL) {
            strcpy((char *)sqlstate, "U09999") ;
            strcpy((char *)ErrorMsg,"Memory allocation failure");
            return;
         }
         strcpy((char*)s1->ConcatOfColumn, "INIT");   
         strcpy((char *)sqlstate, "00000");
         *ResultUdtIndicator = -1;
         /* Drop through to first row data */
      case AGR_DETAIL:
         if (*ColumnValueUdtIndicator != -1) { // if not NULL
            FNC_GetDistinctValue(*ColumnValueUdt, &ColumnValue,
                                 sizeof(ColumnValue), &length);
            /* Concatenate values */
            strcat((char*)s1->ConcatOfColumn,(char *)ColumnValue);
         }
         *ResultUdtIndicator = -1;
         break ;
      case AGR_COMBINE:
         /* Aggregate groups. */
         strcat((char*)s1->ConcatOfColumn,(char*)s2->ConcatOfColumn);
         *ResultUdtIndicator = -1;
         break ;
      case AGR_FINAL:
         /* Produce the final result */
         strcat((char*)s1->ConcatOfColumn, "FINAL");
         strcpy((char *)result,(char*)s1->ConcatOfColumn);
         FNC_SetDistinctValue(*resultUdt,&result,
                              sizeof(s1->ConcatOfColumn));
         *ResultUdtIndicator = 0;
         break;
      case AGR_NODATA:				// Set indicator to NULL
         *ResultUdtIndicator = -1 ;
         break ;
      default:
         strcpy (sqlstate, "U0005") ;
   }
   return ;
}