sum_double.c 2.5 KB
Newer Older
D
dapan1121 已提交
1 2 3 4 5 6 7 8 9 10 11 12
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct SUdfInit{
 int maybe_null;       /* 1 if function can return NULL */
 int decimals;     /* for real functions */
 long long length;       /* For string functions */
 char  *ptr;            /* free pointer for function data */
 int const_item;       /* 0 if result is independent of arguments */
} SUdfInit;

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


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

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

       *numOfOutput=1;
       r+=*((int *)data + i);
       *(int *)dataOutput=r;
D
dapan1121 已提交
33 34 35 36 37 38 39 40
     }

     printf("sum_double out, dataoutput:%d, numOfOutput:%d\n", *(int *)dataOutput, *numOfOutput);
   }
}



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

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


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


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