fix: format udf example codes
This commit is contained in:
parent
05be996342
commit
09f6411f66
|
@ -55,7 +55,7 @@ DLL_EXPORT int32_t bit_and(SUdfDataBlock* block, SUdfColumn* resultCol) {
|
||||||
}
|
}
|
||||||
|
|
||||||
resultData->numOfRows = block->numOfRows;
|
resultData->numOfRows = block->numOfRows;
|
||||||
udfTrace("block:%p, processing completed, rows:%d, cols:%d,", block, block->numOfRows, block->numOfCols);
|
udfTrace("block:%p, processing completed", block);
|
||||||
|
|
||||||
return TSDB_CODE_SUCCESS;
|
return TSDB_CODE_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,32 +1,26 @@
|
||||||
#include <string.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
#include "taosudf.h"
|
#include "taosudf.h"
|
||||||
|
|
||||||
DLL_EXPORT int32_t l2norm_init() {
|
DLL_EXPORT int32_t l2norm_init() { return 0; }
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
DLL_EXPORT int32_t l2norm_destroy() {
|
DLL_EXPORT int32_t l2norm_destroy() { return 0; }
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
DLL_EXPORT int32_t l2norm_start(SUdfInterBuf *buf) {
|
DLL_EXPORT int32_t l2norm_start(SUdfInterBuf* buf) {
|
||||||
*(int64_t*)(buf->buf) = 0;
|
*(int64_t*)(buf->buf) = 0;
|
||||||
buf->bufLen = sizeof(double);
|
buf->bufLen = sizeof(double);
|
||||||
buf->numOfResult = 1;
|
buf->numOfResult = 1;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
DLL_EXPORT int32_t l2norm(SUdfDataBlock* block, SUdfInterBuf *interBuf, SUdfInterBuf *newInterBuf) {
|
DLL_EXPORT int32_t l2norm(SUdfDataBlock* block, SUdfInterBuf* interBuf, SUdfInterBuf* newInterBuf) {
|
||||||
double sumSquares = *(double*)interBuf->buf;
|
double sumSquares = *(double*)interBuf->buf;
|
||||||
int8_t numNotNull = 0;
|
int8_t numNotNull = 0;
|
||||||
for (int32_t i = 0; i < block->numOfCols; ++i) {
|
for (int32_t i = 0; i < block->numOfCols; ++i) {
|
||||||
SUdfColumn* col = block->udfCols[i];
|
SUdfColumn* col = block->udfCols[i];
|
||||||
if (!(col->colMeta.type == TSDB_DATA_TYPE_INT ||
|
if (!(col->colMeta.type == TSDB_DATA_TYPE_INT || col->colMeta.type == TSDB_DATA_TYPE_DOUBLE)) {
|
||||||
col->colMeta.type == TSDB_DATA_TYPE_DOUBLE)) {
|
|
||||||
return TSDB_CODE_UDF_INVALID_INPUT;
|
return TSDB_CODE_UDF_INVALID_INPUT;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -38,18 +32,18 @@ DLL_EXPORT int32_t l2norm(SUdfDataBlock* block, SUdfInterBuf *interBuf, SUdfInte
|
||||||
}
|
}
|
||||||
switch (col->colMeta.type) {
|
switch (col->colMeta.type) {
|
||||||
case TSDB_DATA_TYPE_INT: {
|
case TSDB_DATA_TYPE_INT: {
|
||||||
char* cell = udfColDataGetData(col, j);
|
char* cell = udfColDataGetData(col, j);
|
||||||
int32_t num = *(int32_t*)cell;
|
int32_t num = *(int32_t*)cell;
|
||||||
sumSquares += (double)num * num;
|
sumSquares += (double)num * num;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case TSDB_DATA_TYPE_DOUBLE: {
|
case TSDB_DATA_TYPE_DOUBLE: {
|
||||||
char* cell = udfColDataGetData(col, j);
|
char* cell = udfColDataGetData(col, j);
|
||||||
double num = *(double*)cell;
|
double num = *(double*)cell;
|
||||||
sumSquares += num * num;
|
sumSquares += num * num;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
++numNotNull;
|
++numNotNull;
|
||||||
|
@ -62,7 +56,7 @@ DLL_EXPORT int32_t l2norm(SUdfDataBlock* block, SUdfInterBuf *interBuf, SUdfInte
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
DLL_EXPORT int32_t l2norm_finish(SUdfInterBuf* buf, SUdfInterBuf *resultData) {
|
DLL_EXPORT int32_t l2norm_finish(SUdfInterBuf* buf, SUdfInterBuf* resultData) {
|
||||||
double sumSquares = *(double*)(buf->buf);
|
double sumSquares = *(double*)(buf->buf);
|
||||||
*(double*)(resultData->buf) = sqrt(sumSquares);
|
*(double*)(resultData->buf) = sqrt(sumSquares);
|
||||||
resultData->bufLen = sizeof(double);
|
resultData->bufLen = sizeof(double);
|
||||||
|
|
|
@ -1,101 +1,90 @@
|
||||||
#include <string.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
#include "taosudf.h"
|
#include "taosudf.h"
|
||||||
|
|
||||||
#define STR_MAX_LEN 256 // inter buffer length
|
#define STR_MAX_LEN 256 // inter buffer length
|
||||||
|
|
||||||
// init
|
// init
|
||||||
DLL_EXPORT int32_t max_vol_init()
|
DLL_EXPORT int32_t max_vol_init() { return 0; }
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// destory
|
// destory
|
||||||
DLL_EXPORT int32_t max_vol_destroy()
|
DLL_EXPORT int32_t max_vol_destroy() { return 0; }
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// start
|
// start
|
||||||
DLL_EXPORT int32_t max_vol_start(SUdfInterBuf *buf)
|
DLL_EXPORT int32_t max_vol_start(SUdfInterBuf *buf) {
|
||||||
{
|
memset(buf->buf, 0, sizeof(float) + STR_MAX_LEN);
|
||||||
memset(buf->buf, 0, sizeof(float) + STR_MAX_LEN);
|
// set init value
|
||||||
// set init value
|
*((float *)buf->buf) = -10000000;
|
||||||
*((float*)buf->buf) = -10000000;
|
buf->bufLen = sizeof(float) + STR_MAX_LEN;
|
||||||
buf->bufLen = sizeof(float) + STR_MAX_LEN;
|
buf->numOfResult = 0;
|
||||||
buf->numOfResult = 0;
|
return 0;
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
DLL_EXPORT int32_t max_vol(SUdfDataBlock *block, SUdfInterBuf *interBuf, SUdfInterBuf *newInterBuf) {
|
DLL_EXPORT int32_t max_vol(SUdfDataBlock *block, SUdfInterBuf *interBuf, SUdfInterBuf *newInterBuf) {
|
||||||
float maxValue = *(float *)interBuf->buf;
|
float maxValue = *(float *)interBuf->buf;
|
||||||
char strBuff[STR_MAX_LEN] = "inter1buf";
|
char strBuff[STR_MAX_LEN] = "inter1buf";
|
||||||
|
|
||||||
if (block->numOfCols < 2)
|
if (block->numOfCols < 2) {
|
||||||
{
|
return TSDB_CODE_UDF_INVALID_INPUT;
|
||||||
|
}
|
||||||
|
|
||||||
|
// check data type
|
||||||
|
for (int32_t i = 0; i < block->numOfCols; ++i) {
|
||||||
|
SUdfColumn *col = block->udfCols[i];
|
||||||
|
if (i == block->numOfCols - 1) {
|
||||||
|
// last column is device id , must varchar
|
||||||
|
if (col->colMeta.type != TSDB_DATA_TYPE_VARCHAR) {
|
||||||
return TSDB_CODE_UDF_INVALID_INPUT;
|
return TSDB_CODE_UDF_INVALID_INPUT;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (col->colMeta.type != TSDB_DATA_TYPE_FLOAT) {
|
||||||
|
return TSDB_CODE_UDF_INVALID_INPUT;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// check data type
|
// calc max voltage
|
||||||
for (int32_t i = 0; i < block->numOfCols; ++i)
|
SUdfColumn *lastCol = block->udfCols[block->numOfCols - 1];
|
||||||
{
|
for (int32_t i = 0; i < (block->numOfCols - 1); ++i) {
|
||||||
SUdfColumn *col = block->udfCols[i];
|
for (int32_t j = 0; j < block->numOfRows; ++j) {
|
||||||
if( i == block->numOfCols - 1) {
|
SUdfColumn *col = block->udfCols[i];
|
||||||
// last column is device id , must varchar
|
if (udfColDataIsNull(col, j)) {
|
||||||
if (col->colMeta.type != TSDB_DATA_TYPE_VARCHAR ) {
|
continue;
|
||||||
return TSDB_CODE_UDF_INVALID_INPUT;
|
}
|
||||||
}
|
char *data = udfColDataGetData(col, j);
|
||||||
} else {
|
float voltage = *(float *)data;
|
||||||
if (col->colMeta.type != TSDB_DATA_TYPE_FLOAT) {
|
if (voltage > maxValue) {
|
||||||
return TSDB_CODE_UDF_INVALID_INPUT;
|
maxValue = voltage;
|
||||||
}
|
char *valData = udfColDataGetData(lastCol, j);
|
||||||
}
|
// get device id
|
||||||
|
char *deviceId = valData + sizeof(uint16_t);
|
||||||
|
sprintf(strBuff, "%s_(%d,%d)_%f", deviceId, j, i, maxValue);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// calc max voltage
|
*(float *)newInterBuf->buf = maxValue;
|
||||||
SUdfColumn *lastCol = block->udfCols[block->numOfCols - 1];
|
strcpy(newInterBuf->buf + sizeof(float), strBuff);
|
||||||
for (int32_t i = 0; i < (block->numOfCols - 1); ++i) {
|
newInterBuf->bufLen = sizeof(float) + strlen(strBuff) + 1;
|
||||||
for (int32_t j = 0; j < block->numOfRows; ++j) {
|
newInterBuf->numOfResult = 1;
|
||||||
SUdfColumn *col = block->udfCols[i];
|
return 0;
|
||||||
if (udfColDataIsNull(col, j)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
char *data = udfColDataGetData(col, j);
|
|
||||||
float voltage = *(float *)data;
|
|
||||||
if (voltage > maxValue) {
|
|
||||||
maxValue = voltage;
|
|
||||||
char *valData = udfColDataGetData(lastCol, j);
|
|
||||||
// get device id
|
|
||||||
char *deviceId = valData + sizeof(uint16_t);
|
|
||||||
sprintf(strBuff, "%s_(%d,%d)_%f", deviceId, j, i, maxValue);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
*(float*)newInterBuf->buf = maxValue;
|
|
||||||
strcpy(newInterBuf->buf + sizeof(float), strBuff);
|
|
||||||
newInterBuf->bufLen = sizeof(float) + strlen(strBuff)+1;
|
|
||||||
newInterBuf->numOfResult = 1;
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
DLL_EXPORT int32_t max_vol_finish(SUdfInterBuf *buf, SUdfInterBuf *resultData)
|
DLL_EXPORT int32_t max_vol_finish(SUdfInterBuf *buf, SUdfInterBuf *resultData) {
|
||||||
{
|
char *str = buf->buf + sizeof(float);
|
||||||
char * str = buf->buf + sizeof(float);
|
// copy to des
|
||||||
// copy to des
|
char *des = resultData->buf + sizeof(uint16_t);
|
||||||
char * des = resultData->buf + sizeof(uint16_t);
|
strcpy(des, str);
|
||||||
strcpy(des, str);
|
|
||||||
|
|
||||||
// set binary type len
|
// set binary type len
|
||||||
uint16_t len = strlen(str);
|
uint16_t len = strlen(str);
|
||||||
*((uint16_t*)resultData->buf) = len;
|
*((uint16_t *)resultData->buf) = len;
|
||||||
|
|
||||||
// set buf len
|
// set buf len
|
||||||
resultData->bufLen = len + sizeof(uint16_t);
|
resultData->bufLen = len + sizeof(uint16_t);
|
||||||
// set row count
|
// set row count
|
||||||
resultData->numOfResult = 1;
|
resultData->numOfResult = 1;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue