abs_max.c 2.5 KB
Newer Older
W
wpan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
#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;


W
wpan 已提交
14 15
#define TSDB_DATA_INT_NULL              0x80000000L
#define TSDB_DATA_BIGINT_NULL           0x8000000000000000L
W
wpan 已提交
16 17 18 19 20 21

void abs_max(char* data, short itype, short ibytes, int numOfRows, long long* ts, char* dataOutput, char* interBuf, char* tsOutput,
                        int* numOfOutput, short otype, short obytes, SUdfInit* buf) {
   int i;
   int r = 0;
   printf("abs_max 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);
W
wpan 已提交
22 23
   if (itype == 5) {
     r=*(long *)dataOutput;
W
wpan 已提交
24 25 26
     *numOfOutput=0;

     for(i=0;i<numOfRows;++i) {
W
wpan 已提交
27
       if (*((long *)data + i) == TSDB_DATA_BIGINT_NULL) {
W
wpan 已提交
28 29 30 31
         continue;
       }

       *numOfOutput=1;
W
wpan 已提交
32
       long v = abs(*((long *)data + i));
W
wpan 已提交
33 34 35 36 37
       if (v > r) {
          r = v;
       }
     }

W
wpan 已提交
38
     *(long *)dataOutput=r;
W
wpan 已提交
39

W
wpan 已提交
40
     printf("abs_max out, dataoutput:%ld, numOfOutput:%d\n", *(long *)dataOutput, *numOfOutput);
W
wpan 已提交
41 42 43 44 45 46 47 48 49 50
   }
}



void abs_max_finalize(char* dataOutput, char* interBuf, int* numOfOutput, SUdfInit* buf) {
   int i;
   int r = 0;
   printf("abs_max_finalize dataoutput:%p:%d, numOfOutput:%d, buf:%p\n", dataOutput, *dataOutput, *numOfOutput, buf);
   *numOfOutput=1;
W
wpan 已提交
51
   printf("abs_max finalize, dataoutput:%ld, numOfOutput:%d\n", *(long *)dataOutput, *numOfOutput);
W
wpan 已提交
52 53 54 55 56 57
}

void abs_max_merge(char* data, int32_t numOfRows, char* dataOutput, int32_t* numOfOutput, SUdfInit* buf) {
   int r = 0;
   
   if (numOfRows > 0) {
W
wpan 已提交
58
      r = *((long *)data);
W
wpan 已提交
59 60 61
   }
   printf("abs_max_merge numOfRows:%d, dataoutput:%p, buf:%p\n", numOfRows, dataOutput, buf);
   for (int i = 1; i < numOfRows; ++i) {
W
wpan 已提交
62 63 64
     printf("abs_max_merge %d - %ld\n", i, *((long *)data + i));
     if (*((long*)data + i) > r) {
        r= *((long*)data + i);
W
wpan 已提交
65 66 67
     }
   }
   
W
wpan 已提交
68
   *(long*)dataOutput=r;
W
wpan 已提交
69 70 71 72 73 74
   if (numOfRows > 0) {
     *numOfOutput=1;
   } else {
     *numOfOutput=0;
   }
   
W
wpan 已提交
75
   printf("abs_max_merge, dataoutput:%ld, numOfOutput:%d\n", *(long *)dataOutput, *numOfOutput);
W
wpan 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88
}


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


void abs_max_destroy(SUdfInit* buf) {
   printf("abs_max destroy\n");
}