taosdata/TDengine

taso3.2.2.0 udf to achieve find_in_set

Open

#24.289 aperta il 30 dic 2023

Vedi su GitHub
 (0 commenti) (0 reazioni) (1 assegnatario)C (5002 fork)batch import
help wantedquestion

Metriche repository

Star
 (24.849 star)
Metriche merge PR
 (Merge medio 1g 1h) (4 PR mergiate in 30 g)

Descrizione

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

}

Guida contributor