2022-06-28 03:54:16 +00:00
|
|
|
#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
|
|
|
|
2022-06-28 03:54:16 +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;
|
2022-04-26 23:27:58 +00:00
|
|
|
*(int64_t*)(buf->buf) = 0;
|
2022-05-12 14:58:49 +00:00
|
|
|
buf->bufLen = sizeof(double);
|
2023-02-24 04:20:55 +00:00
|
|
|
buf->numOfResult = 1;
|
2022-04-26 23:27:58 +00:00
|
|
|
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;
|
|
|
|
|
}
|
2022-05-19 12:54:30 +00:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-04-26 23:27:58 +00:00
|
|
|
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) {
|
2022-04-26 23:27:58 +00:00
|
|
|
SUdfColumn* col = block->udfCols[i];
|
2022-05-07 11:35:44 +00:00
|
|
|
if (udfColDataIsNull(col, j)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2022-05-12 14:58:49 +00:00
|
|
|
switch (col->colMeta.type) {
|
|
|
|
|
case TSDB_DATA_TYPE_INT: {
|
2022-10-13 06:15:42 +00:00
|
|
|
char* cell = udfColDataGetData(col, j);
|
2022-05-12 14:58:49 +00:00
|
|
|
int32_t num = *(int32_t*)cell;
|
2022-05-13 13:02:32 +00:00
|
|
|
sumSquares += (double)num * num;
|
2022-05-12 14:58:49 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case TSDB_DATA_TYPE_DOUBLE: {
|
2022-10-13 06:15:42 +00:00
|
|
|
char* cell = udfColDataGetData(col, j);
|
2022-05-12 14:58:49 +00:00
|
|
|
double num = *(double*)cell;
|
|
|
|
|
sumSquares += num * num;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2022-10-13 06:15:42 +00:00
|
|
|
default:
|
2022-05-12 14:58:49 +00:00
|
|
|
break;
|
|
|
|
|
}
|
2022-05-19 12:54:30 +00:00
|
|
|
++numNotNull;
|
2022-04-26 10:28:30 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-19 12:54: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-05-07 11:35:44 +00:00
|
|
|
}
|
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) {
|
2022-05-07 11:35:44 +00:00
|
|
|
if (buf->numOfResult == 0) {
|
|
|
|
|
resultData->numOfResult = 0;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2022-05-12 14:58:49 +00:00
|
|
|
double sumSquares = *(double*)(buf->buf);
|
2022-04-26 23:27:58 +00:00
|
|
|
*(double*)(resultData->buf) = sqrt(sumSquares);
|
|
|
|
|
resultData->bufLen = sizeof(double);
|
|
|
|
|
resultData->numOfResult = 1;
|
2022-04-26 10:28:30 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|