gpd.c 1.8 KB
Newer Older
S
slzhou 已提交
1 2 3 4 5 6 7 8 9 10 11
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#ifdef LINUX
#include <unistd.h>
#endif
#ifdef WINDOWS
#include <windows.h>
#endif
#include "taosudf.h"

12
TAOS* taos = NULL;
S
slzhou 已提交
13 14

DLL_EXPORT int32_t gpd_init() {
15
  taos = taos_connect("localhost", "root", "taosdata", "", 7100);
S
slzhou 已提交
16 17 18 19
  return 0;
}

DLL_EXPORT int32_t gpd_destroy() {
20 21
  taos_close(taos);
  taos_cleanup();
S
slzhou 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
  return 0;
}

DLL_EXPORT int32_t gpd(SUdfDataBlock* block, SUdfColumn *resultCol) {
  SUdfColumnMeta *meta = &resultCol->colMeta;
  meta->bytes = 4;
  meta->type = TSDB_DATA_TYPE_INT;
  meta->scale = 0;
  meta->precision = 0;

  SUdfColumnData *resultData = &resultCol->colData;
  resultData->numOfRows = block->numOfRows;
  for (int32_t i = 0; i < resultData->numOfRows; ++i) {
    int j = 0;
    for (; j < block->numOfCols; ++j) {
      if (udfColDataIsNull(block->udfCols[j], i)) {
        udfColDataSetNull(resultCol, i);
        break;
      }
    }
    if ( j == block->numOfCols) {
      int32_t luckyNum = 88;
      udfColDataSet(resultCol, i, (char *)&luckyNum, false);
    }
  }
  TAOS_RES* res = taos_query(taos, "create database if not exists gpd");
  if (taos_errno(res) != 0) {
    char* errstr = taos_errstr(res);
  } 
  res = taos_query(taos, "create table gpd.st (ts timestamp, f int) tags(t int)");
  if (taos_errno(res) != 0) {
    char* errstr = taos_errstr(res);
  } 

  taos_query(taos, "insert into gpd.t using gpd.st tags(1) values(now, 1) ");
  if (taos_errno(res) != 0) {
    char* errstr = taos_errstr(res);
  } 

  taos_query(taos, "select * from gpd.t");
  if (taos_errno(res) != 0) {
    char* errstr = taos_errstr(res);
  } 

  //to simulate actual processing delay by udf
#ifdef LINUX
  usleep(1 * 1000);   // usleep takes sleep time in us (1 millionth of a second)
#endif
#ifdef WINDOWS
  Sleep(1);
#endif
  return 0;
}