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
}

27 28 29 30 31 32 33 34
int32_t udf2(SUdfDataBlock* block, SUdfInterBuf *interBuf) {
  int64_t sumSquares = *(int64_t*)interBuf->buf;
  for (int32_t i = 0; i < block->numOfCols; ++i) {
    for (int32_t j = 0; j < block->numOfRows; ++i) {
      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
    }
  }

38 39 40 41
  *(int64_t*)interBuf = sumSquares;
  interBuf->bufLen = sizeof(int64_t);
  //TODO: if all null value, numOfResult = 0;
  interBuf->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;
}