17.10 - Example Using FNC_SetNullBitVectorByElemIndex - 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)

In this example, memset() is called to initialize a new NullBitVector by setting all bytes to 0 after it has been allocated by the caller for an ARRAY of type phonenumbers_ary. The required information for these operations is retrieved by calling FNC_GetArrayTypeInfo. Then, FNC_SetNullBitVectorByElemIndex is called to set one of the bits to "not present".

The following example is based on the following 2-D ARRAY definition:

/* Oracle-compatible and Teradata syntax respectively: */
CREATE TYPE myArray AS VARRAY(1:20)(1:20) OF INTEGER;
CREATE TYPE myArray AS INTEGER ARRAY[1:20][1:20];

void ArrayUDF ( ARRAY_HANDLE  *a,
                char           sqlstate[6])
{
    NullBitVecType *NullBitVector;
    array_info_t arrayInfo;
    long nullVecBufSize;
    int aryIndex[FNC_ARRAYMAXDIMENSIONS];
    bounds_t arrayScope[FNC_ARRAYMAXDIMENSIONS];

    /* Call FNC_GetArrayTypeInfo first to find out the number of */
    /* elements in the array. */
    FNC_GetArrayTypeInfo(*a,
                         &arrayInfo,
                         arrayScope);
    (arrayInfo.totalNumElements % 8 == 0) ?
       (nullVecBufSize = arrayInfo.totalNumElements / 8) :
       (nullVecBufSize = arrayInfo.totalNumElements / 8) + 1;

    /* Allocate a new NullBitVector array. */
    NullBitVector = (NullBitVecType*)FNC_malloc(nullVecBufSize);

    /* Initialize the NullBitVector to default values */
    memset(NullBitVector, 0, nullVecBufSize);

    /* Set one bit in NullBitVector to 1 (present) */
    aryIndex[0] = 1;
    aryIndex[1] = 1;
    FNC_SetNullBitVectorByElemIndex(NullBitVector, aryIndex, 1,
       nullVecBufSize, arrayScope, arrayInfo.numDimensions);
    ...
}