sum_double.c 2.6 KB
Newer Older
D
dapan1121 已提交
1 2 3
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
4
#include <inttypes.h> 
D
dapan1121 已提交
5 6 7 8

typedef struct SUdfInit{
 int maybe_null;       /* 1 if function can return NULL */
 int decimals;     /* for real functions */
9
 int64_t length;       /* For string functions */
D
dapan1121 已提交
10 11 12 13
 char  *ptr;            /* free pointer for function data */
 int const_item;       /* 0 if result is independent of arguments */
} SUdfInit;

D
dapan1121 已提交
14 15 16
#define TSDB_DATA_INT_NULL              0x80000000L


17
void sum_double(char* data, short itype, short ibytes, int numOfRows, int64_t* ts, char* dataOutput, char* interBuf, char* tsOutput,
D
dapan1121 已提交
18
                        int* numOfOutput, short otype, short obytes, SUdfInit* buf) {
D
dapan1121 已提交
19
   int i;
20 21
   int64_t r = 0;
   printf("sum_double input data:%p, type:%d, rows:%d, ts:%p,%"PRId64", dataoutput:%p, tsOutput:%p, numOfOutput:%p, buf:%p\n", data, itype, numOfRows, ts, *ts, dataOutput, tsOutput, numOfOutput, buf);
D
dapan1121 已提交
22
   if (itype == 4) {
23
     r=*(int64_t *)dataOutput;
D
dapan1121 已提交
24 25
     *numOfOutput=0;

D
dapan1121 已提交
26
     for(i=0;i<numOfRows;++i) {
D
dapan1121 已提交
27 28
       if (*((int *)data + i) == TSDB_DATA_INT_NULL) {
         continue;
D
dapan1121 已提交
29
       }
D
dapan1121 已提交
30 31 32

       *numOfOutput=1;
       r+=*((int *)data + i);
33 34
       *(int64_t *)dataOutput=r;
     } 
D
dapan1121 已提交
35

36
    //  printf("sum_double out, dataoutput:%"PRId64", numOfOutput:%d\n", *(int64_t *)dataOutput, *numOfOutput);
D
dapan1121 已提交
37 38 39 40 41
   }
}



D
dapan1121 已提交
42
void sum_double_finalize(char* dataOutput, char* interBuf, int* numOfOutput, SUdfInit* buf) {
D
dapan1121 已提交
43
   int i;
44 45 46 47 48 49
   int64_t r = 0;
  //  printf("sum_double_finalize dataoutput:%p:%"PRId64", numOfOutput:%d, buf:%p\n", dataOutput, *(int64_t*)dataOutput, *numOfOutput, buf);
  //  *numOfOutput=1;
   *(int64_t*)(buf->ptr)=*(int64_t*)dataOutput*2;
   *(int64_t*)dataOutput=*(int64_t*)(buf->ptr);
  //  printf("sum_double finalize, dataoutput:%"PRId64", numOfOutput:%d\n", *(int64_t *)dataOutput, *numOfOutput);
D
dapan1121 已提交
50 51
}

52
void sum_double_merge(char* data, int32_t numOfRows, char* dataOutput, int* numOfOutput, SUdfInit* buf) {
D
dapan1121 已提交
53
   int r = 0;
54
   int64_t sum = 0;
D
dapan1121 已提交
55
   
56
  //  printf("sum_double_merge numOfRows:%d, dataoutput:%p, buf:%p\n", numOfRows, dataOutput, buf);
D
dapan1121 已提交
57
   for (int i = 0; i < numOfRows; ++i) {
58 59
    //  printf("sum_double_merge %d - %"PRId64"\n", i, *((int64_t*)data + i));
     sum +=*((int64_t*)data + i);
D
dapan1121 已提交
60 61
   }
   
62
   *(int64_t*)dataOutput+=sum;
D
dapan1121 已提交
63 64 65 66 67
   if (numOfRows > 0) {
     *numOfOutput=1;
   } else {
     *numOfOutput=0;
   }
D
dapan1121 已提交
68
   
69
  //  printf("sum_double_merge, dataoutput:%"PRId64", numOfOutput:%d\n", *(int64_t *)dataOutput, *numOfOutput);
D
dapan1121 已提交
70 71 72
}


D
dapan1121 已提交
73 74
int sum_double_init(SUdfInit* buf) {
   buf->maybe_null=1;
75 76
   buf->ptr = malloc(sizeof(int64_t));
  //  printf("sum_double init\n");
D
dapan1121 已提交
77 78 79 80 81 82
   return 0;
}


void sum_double_destroy(SUdfInit* buf) {
   free(buf->ptr);
83 84
  //  printf("sum_double destroy\n");
}