demo.c 2.6 KB
Newer Older
D
dapan1121 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#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;

typedef struct SDemo{
  double sum;
  int num;
  short otype;
}SDemo;

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

   for(i=0;i<numOfRows;++i) {
D
add int  
dapan1121 已提交
27 28 29
     if (itype == 4) {
       r=*((int *)data+i);
     } else if (itype == 6) {
D
dapan1121 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42
       r=*((float *)data+i);
     } else if (itype == 7) {
       r=*((double *)data+i);
     }

     p->sum += r*r;
   }

   p->otype = otype;
   p->num += numOfRows;

   *numOfOutput=1;

D
dapan1121 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
   printf("demo out, sum:%f, num:%d, numOfOutput:%d\n", p->sum, p->num, *numOfOutput);
}


void demo_merge(char* data, int32_t numOfRows, char* dataOutput, int32_t* numOfOutput, SUdfInit* buf) {
   int i;
   SDemo *p = (SDemo *)data;
   SDemo res = {0};
   printf("demo_merge input data:%p, rows:%d, dataoutput:%p, numOfOutput:%p, buf:%p\n", data, numOfRows, dataOutput, numOfOutput, buf);

   for(i=0;i<numOfRows;++i) {
     res.sum += p->sum * p->sum;
     res.num += p->num;
     p++;
   }

   p->sum = res.sum;
   p->num = res.num;

   *numOfOutput=1;

   printf("demo out, sum:%f, num:%d, numOfOutput:%d\n", p->sum, p->num, *numOfOutput);
D
dapan1121 已提交
65 66 67 68
}



D
dapan1121 已提交
69 70 71
void demo_finalize(char* dataOutput, char* interBuf, int* numOfOutput, SUdfInit* buf) {
   SDemo *p = (SDemo *)interBuf;
   printf("demo_finalize interbuf:%p, numOfOutput:%p, buf:%p, sum:%f, num:%d\n", interBuf, numOfOutput, buf, p->sum, p->num);
D
dapan1121 已提交
72
   if (p->otype == 6) {
D
dapan1121 已提交
73 74
     *(float *)dataOutput = (float)(p->sum / p->num);  
     printf("finalize values:%f\n", *(float *)dataOutput);
D
dapan1121 已提交
75 76
   } else if (p->otype == 7) {
     *(double *)dataOutput = (double)(p->sum / p->num);
D
dapan1121 已提交
77
     printf("finalize values:%f\n", *(double *)dataOutput);
D
dapan1121 已提交
78 79 80 81
   }

   *numOfOutput=1;

D
dapan1121 已提交
82
   printf("demo finalize, numOfOutput:%d\n", *numOfOutput);
D
dapan1121 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95
}


int demo_init(SUdfInit* buf) {
   printf("demo init\n");
   return 0;
}


void demo_destroy(SUdfInit* buf) {
   printf("demo destroy\n");
}