taosdata/TDengine

taso3.2.2.0 udf to achieve find_in_set

Open

#24.289 geöffnet am 30. Dez. 2023

Auf GitHub ansehen
 (0 Kommentare) (0 Reaktionen) (1 zugewiesene Person)C (5.002 Forks)batch import
help wantedquestion

Repository-Metriken

Stars
 (24.849 Stars)
PR-Merge-Metriken
 (Durchschn. Merge 1T 1h) (4 gemergte PRs in 30 T)

Beschreibung

参考官方标量函数的定义模板和样例,编写 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