udf1.c 2.2 KB
Newer Older
S
shenglian zhou 已提交
1 2 3 4 5
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

#include "tudf.h"
S
shenglian zhou 已提交
6

7 8 9 10
#undef malloc
#define malloc malloc
#undef free
#define free free
S
slzhou 已提交
11

S
shenglian zhou 已提交
12 13 14 15 16 17
int32_t udf1_setup() {
  return 0;
}

int32_t udf1_teardown() {
  return 0;
S
shenglian zhou 已提交
18
}
S
shenglian zhou 已提交
19

S
slzhou 已提交
20 21
int32_t udf1(SUdfDataBlock block, SUdfColumn *resultCol) {
  SUdfColumnData *resultData = &resultCol->colData;
S
shenglian zhou 已提交
22 23 24 25 26
  resultData->numOfRows = block.numOfRows;
  SUdfColumnData *srcData = &block.udfCols[0]->colData;
  resultData->varLengthColumn = srcData->varLengthColumn;

  if (resultData->varLengthColumn) {
S
slzhou 已提交
27 28 29
    resultData->varLenCol.varOffsetsLen = srcData->varLenCol.varOffsetsLen;
    resultData->varLenCol.varOffsets = malloc(resultData->varLenCol.varOffsetsLen);
    memcpy(resultData->varLenCol.varOffsets, srcData->varLenCol.varOffsets, srcData->varLenCol.varOffsetsLen);
S
shenglian zhou 已提交
30

S
slzhou 已提交
31 32 33
    resultData->varLenCol.payloadLen = srcData->varLenCol.payloadLen;
    resultData->varLenCol.payload = malloc(resultData->varLenCol.payloadLen);
    memcpy(resultData->varLenCol.payload, srcData->varLenCol.payload, srcData->varLenCol.payloadLen);
S
shenglian zhou 已提交
34
  } else {
S
slzhou 已提交
35 36 37
    resultData->fixLenCol.nullBitmapLen = srcData->fixLenCol.nullBitmapLen;
    resultData->fixLenCol.nullBitmap = malloc(resultData->fixLenCol.nullBitmapLen);
    memcpy(resultData->fixLenCol.nullBitmap, srcData->fixLenCol.nullBitmap, srcData->fixLenCol.nullBitmapLen);
S
shenglian zhou 已提交
38

S
slzhou 已提交
39 40 41 42 43 44
    resultData->fixLenCol.dataLen = srcData->fixLenCol.dataLen;
    resultData->fixLenCol.data = malloc(resultData->fixLenCol.dataLen);
    memcpy(resultData->fixLenCol.data, srcData->fixLenCol.data, srcData->fixLenCol.dataLen);
    for (int32_t i = 0; i < resultData->numOfRows; ++i) {
      *(resultData->fixLenCol.data + i * sizeof(int32_t)) = 88;
    }
S
shenglian zhou 已提交
45 46
  }

S
slzhou 已提交
47 48 49 50 51
  SUdfColumnMeta *meta = &resultCol->colMeta;
  meta->bytes = 4;
  meta->type = TSDB_DATA_TYPE_INT;
  meta->scale = 0;
  meta->precision = 0;
S
shenglian zhou 已提交
52 53 54
  return 0;
}

S
slzhou 已提交
55 56
int32_t udf1_free(SUdfColumn *col) {
  SUdfColumnData *data = &col->colData;
S
shenglian zhou 已提交
57
  if (data->varLengthColumn) {
S
slzhou 已提交
58 59 60 61
    free(data->varLenCol.varOffsets);
    data->varLenCol.varOffsets = NULL;
    free(data->varLenCol.payload);
    data->varLenCol.payload = NULL;
S
shenglian zhou 已提交
62
  } else {
S
slzhou 已提交
63 64 65 66
    free(data->fixLenCol.nullBitmap);
    data->fixLenCol.nullBitmap = NULL;
    free(data->fixLenCol.data);
    data->fixLenCol.data = NULL;
S
shenglian zhou 已提交
67 68 69
  }
  return 0;
}