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™
/* function context area */
typedef struct arg_storage {
   int  MaxOfColumn ;
} AgrStorage ;

void AggFncWAnytype(
   FNC_Phase       phase,
   FNC_Context_t  *fContext,
   void           *input,
   INTEGER        *result,
   int            *inputIsNull,
   int            *resultIsNull,
   char            sqlstate[6],
   SQL_TEXT        extname[129],
   SQL_TEXT        specific_name[129],
   SQL_TEXT        error_message[257] )

{
   int                   sumValue = 0;
   anytype_param_info_t  paraminfo;
   int                   numAnyType;

   AgrStorage *s1 = fContext->interim1 ;
   AgrStorage *s2 = fContext->interim2 ;

   switch (phase)
   {
      case AGR_INIT:
         /* Allocate and initialize the intermediate storage. */
         if ((s1 = FNC_DefMem(sizeof(AgrStorage))) == NULL) {
            strcpy((char *)sqlstate, "U09999") ;
            strcpy((char *)&(error_message[0]),"Memory allocation failure");
            return;
         }

         s1->MaxOfColumn = 0;
         strcpy((char *)sqlstate, "00000");
         memset(&(error_message[0]), 0, sizeof(error_message));

      case AGR_DETAIL:
         FNC_GetAnyTypeParamInfo( sizeof(anytype_param_info_t), &numAnyType, &paraminfo);

         /* Process the value based upon the data type. */
         switch(paraminfo.datatype)
         {
            case BYTEINT_DT:
            case SMALLINT_DT:
            case INTEGER_DT:
               /* Process the value */
               s1->MaxOfColumn = 10;
               break;
            case VARCHAR_DT:
            case CHAR_DT:
               s1->MaxOfColumn = 11;
               break;
            case CLOB_REFERENCE_DT:
               s1->MaxOfColumn = 12;
               break;
            default:
               /* Invalid string */
               strcpy((char *)sqlstate, "22023");
               sprintf((char*)error_message, "\n Invalid Data Type passed into UDF");
               break;
         } /* end of the switch statement */
         break;

      case AGR_COMBINE:
         /* combine the intermediate results */
         if(s1->MaxOfColumn < s2->MaxOfColumn)
            s1->MaxOfColumn =s2->MaxOfColumn;
         break;

      case AGR_FINAL:
         /* produce the final result */
         *result = s1->MaxOfColumn;
         break;

      case AGR_NODATA:
         /* No data passed in ... so nothing to do */
         //Indicate that OUTPUT is being produced.
         *resultIsNull =IsNull;
         break;

      default:
         strcpy (sqlstate, "U0005") ;
         sprintf((char*)error_message,"\n Unknown Aggregate Phase!");
         return;
   }
}