udf2.c 1.2 KB
Newer Older
S
slzhou 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

#include "tudf.h"

#undef malloc
#define malloc malloc
#undef free
#define free free

int32_t udf2_init() {
  return 0;
}

int32_t udf2_destroy() {
  return 0;
}

int32_t udf2_start(SUdfInterBuf *buf) {
21 22 23 24
  *(int64_t*)(buf->buf) = 0;
  buf->bufLen = sizeof(int64_t);
  buf->numOfResult = 0;
  return 0;
S
slzhou 已提交
25 26
}

S
shenglian zhou 已提交
27
int32_t udf2(SUdfDataBlock* block, SUdfInterBuf *interBuf, SUdfInterBuf *newInterBuf) {
28 29
  int64_t sumSquares = *(int64_t*)interBuf->buf;
  for (int32_t i = 0; i < block->numOfCols; ++i) {
S
slzhou 已提交
30
    for (int32_t j = 0; j < block->numOfRows; ++j) {
31 32 33 34
      SUdfColumn* col = block->udfCols[i];
      //TODO: check the bitmap for null value
      int32_t* rows = (int32_t*)col->colData.fixLenCol.data;
      sumSquares += rows[j] * rows[j];
S
slzhou 已提交
35 36 37
    }
  }

S
slzhou 已提交
38
  *(int64_t*)(newInterBuf->buf) = sumSquares;
S
shenglian zhou 已提交
39
  newInterBuf->bufLen = sizeof(int64_t);
40
  //TODO: if all null value, numOfResult = 0;
S
shenglian zhou 已提交
41
  newInterBuf->numOfResult = 1;
S
slzhou 已提交
42 43 44
  return 0;
}

45 46 47 48 49 50
int32_t udf2_finish(SUdfInterBuf* buf, SUdfInterBuf *resultData) {
  //TODO: check numOfResults;
  int64_t sumSquares = *(int64_t*)(buf->buf);
  *(double*)(resultData->buf) = sqrt(sumSquares);
  resultData->bufLen = sizeof(double);
  resultData->numOfResult = 1;
S
slzhou 已提交
51 52
  return 0;
}