taosdata/TDengine

taso3.2.2.0 udf to achieve find_in_set

Open

#24,289 opened on Dec 30, 2023

View on GitHub
 (0 comments) (0 reactions) (1 assignee)C (5,002 forks)batch import
help wantedquestion

Repository metrics

Stars
 (24,849 stars)
PR merge metrics
 (Avg merge 1d 1h) (4 merged PRs in 30d)

Description

参考官方标量函数的定义模板和样例,编写 C程序函数,部署完毕后执行得到的结果不是想要的,麻烦能给指出程序中的问题吗 find_in_set.c

#include "taos.h" #include "taoserror.h" #include "taosudf.h" #include <stdio.h> #include <stdlib.h> #include <string.h> DLL_EXPORT int32_t find_in_set_init() { return 0; }

DLL_EXPORT int32_t find_in_set_destroy() { return 0; }

DLL_EXPORT int32_t find_in_set(SUdfDataBlock* block, SUdfColumn* resultCol){

if (block->numOfCols !=2) {
    return TSDB_CODE_UDF_INVALID_INPUT;
}
for (int32_t i = 0; i < block->numOfCols; ++i) {
    SUdfColumn* col = block->udfCols[i];
    if (!(col->colMeta.type == TSDB_DATA_TYPE_VARCHAR)) {
            return TSDB_CODE_UDF_INVALID_INPUT;
    }
}
SUdfColumnData* resultData = &resultCol->colData;
for (int32_t i = 0; i < block->numOfRows; ++i) {
    if (udfColDataIsNull(block->udfCols[0], i)) {
      udfColDataSetNull(resultCol, i);
      continue;
    }
    if (udfColDataIsNull(block->udfCols[1], i)) {
      udfColDataSetNull(resultCol, i);
      continue;
    }
    char* str = udfColDataGetData(block->udfCols[0], i);
		 	char* set = udfColDataGetData(block->udfCols[1], i);
		  char* token;
		  char copy_set[256];
		  int32_t result = *(int32_t*)udfColDataGetData(block->udfCols[0], i);
		  result =0 ;
		  strcpy(copy_set, set);
		  token = strtok(copy_set, ",");
		  while (token != NULL) {
	      if (strcmp(str, token) == 0) {
	        result=1;
	        break;
	      }
	      token = strtok(NULL, ",");
	    }
		udfColDataSet(resultCol, i, (char*)&result, false);
}
resultData->numOfRows = block->numOfRows;
return TSDB_CODE_SUCCESS;

}

Contributor guide