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 Unicode_Text
#include "sqltypes_td.h"
#include <string.h>
#define buffer_size 64000

void dclobcompress(UDT_HANDLE*         inputUDT,
                   LOB_RESULT_LOCATOR* resultLobLoc,
                   char                sqlstate[6] )
{
   LOB_LOCATOR sourceLobLoc;
   BYTE inputBuffer[buffer_size];
   BYTE outputBuffer[buffer_size];
   LOB_CONTEXT_ID id;
   FNC_LobLength_t readlen, writelen;
   int trunc_err = 0;
   readlen = 0;
   writelen = 0;

   FNC_GetDistinctInputLob(*inputUDT,&sourceLobLoc);
   FNC_LobOpen(sourceLobLoc, &id, 0, 0);

   while( FNC_LobRead(id, inputBuffer, buffer_size, &readlen) == 0 && !trunc_err )
   {
      /* Perform compression on the buffer (remove extra bytes)*/
      BYTE *inputPtr = inputBuffer;
      BYTE *outputPtr = outputBuffer;
      int i=0;
      for (i=0; i<(readlen/2); i++){
         memcpy(outputPtr,inputPtr,sizeof(BYTE));
         inputPtr += sizeof(BYTE);
         if (*inputPtr != 0)
         {
            strcpy(sqlstate,"38000",5);
            return;
         }
         inputPtr += sizeof(BYTE);

         outputPtr += sizeof(BYTE);
      }
      trunc_err = FNC_LobAppend(*resultLobLoc, outputBuffer, (readlen/2), &writelen);

      /* check trunc_err and properly report an error (performed in the same way as for
      a standard UDF) */
   }
   FNC_LobClose(id);
}