TDengine/source/libs/function/test/udf2.c

81 lines
2.3 KiB
C
Raw Permalink Normal View History

#include <math.h>
2022-10-13 06:15:42 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2022-04-26 10:28:30 +00:00
#include "taosudf.h"
2022-04-26 10:28:30 +00:00
2022-10-13 06:15:42 +00:00
DLL_EXPORT int32_t udf2_init() { return 0; }
2022-04-26 10:28:30 +00:00
2022-10-13 06:15:42 +00:00
DLL_EXPORT int32_t udf2_destroy() { return 0; }
2022-04-26 10:28:30 +00:00
2022-10-13 06:15:42 +00:00
DLL_EXPORT int32_t udf2_start(SUdfInterBuf* buf) {
2024-06-13 08:57:49 +00:00
if(buf->buf == NULL || buf->bufLen < (sizeof(int64_t))) return TSDB_CODE_UDF_INVALID_BUFSIZE;
*(int64_t*)(buf->buf) = 0;
buf->bufLen = sizeof(double);
2023-02-24 04:20:55 +00:00
buf->numOfResult = 1;
return 0;
2022-04-26 10:28:30 +00:00
}
2022-10-13 06:15:42 +00:00
DLL_EXPORT int32_t udf2(SUdfDataBlock* block, SUdfInterBuf* interBuf, SUdfInterBuf* newInterBuf) {
2024-06-13 09:49:58 +00:00
if(newInterBuf->buf == NULL || newInterBuf->bufLen < (sizeof(double))) return TSDB_CODE_UDF_INVALID_BUFSIZE;
2023-02-24 04:20:55 +00:00
double sumSquares = 0;
if (interBuf->numOfResult == 1) {
sumSquares = *(double*)interBuf->buf;
}
int8_t numNotNull = 0;
2022-05-12 08:49:31 +00:00
for (int32_t i = 0; i < block->numOfCols; ++i) {
SUdfColumn* col = block->udfCols[i];
2022-10-13 06:15:42 +00:00
if (!(col->colMeta.type == TSDB_DATA_TYPE_INT || col->colMeta.type == TSDB_DATA_TYPE_DOUBLE)) {
2022-05-12 08:49:31 +00:00
return TSDB_CODE_UDF_INVALID_INPUT;
}
}
for (int32_t i = 0; i < block->numOfCols; ++i) {
2022-04-29 10:20:50 +00:00
for (int32_t j = 0; j < block->numOfRows; ++j) {
SUdfColumn* col = block->udfCols[i];
if (udfColDataIsNull(col, j)) {
continue;
}
switch (col->colMeta.type) {
case TSDB_DATA_TYPE_INT: {
2022-10-13 06:15:42 +00:00
char* cell = udfColDataGetData(col, j);
int32_t num = *(int32_t*)cell;
sumSquares += (double)num * num;
break;
}
case TSDB_DATA_TYPE_DOUBLE: {
2022-10-13 06:15:42 +00:00
char* cell = udfColDataGetData(col, j);
double num = *(double*)cell;
sumSquares += num * num;
break;
}
2022-10-13 06:15:42 +00:00
default:
break;
}
++numNotNull;
2022-04-26 10:28:30 +00:00
}
}
*(double*)(newInterBuf->buf) = sumSquares;
newInterBuf->bufLen = sizeof(double);
if (interBuf->numOfResult == 0 && numNotNull == 0) {
newInterBuf->numOfResult = 0;
} else {
newInterBuf->numOfResult = 1;
}
2022-04-26 10:28:30 +00:00
return 0;
}
2022-10-13 06:15:42 +00:00
DLL_EXPORT int32_t udf2_finish(SUdfInterBuf* buf, SUdfInterBuf* resultData) {
if (buf->numOfResult == 0) {
resultData->numOfResult = 0;
return 0;
}
double sumSquares = *(double*)(buf->buf);
*(double*)(resultData->buf) = sqrt(sumSquares);
resultData->bufLen = sizeof(double);
resultData->numOfResult = 1;
2022-04-26 10:28:30 +00:00
return 0;
}