diff --git a/source/libs/function/src/tudf.c b/source/libs/function/src/tudf.c index 13233c4964109b80f0ae552d8e37ee8341ab5ebd..0b309bc8f56abd3870a07eebdb94e61f357ee23c 100644 --- a/source/libs/function/src/tudf.c +++ b/source/libs/function/src/tudf.c @@ -853,8 +853,15 @@ int32_t convertScalarParamToDataBlock(SScalarParam *input, int32_t numOfCols, SS if (isNull) { colDataAppendNNULL(pColInfoData, startRow, expandRows); } else { - char* data = colDataGetData(pColInfoData, (input + i)->numOfRows - 1); - colDataAppendNItems(pColInfoData, startRow, data, expandRows); + char* src = colDataGetData(pColInfoData, (input + i)->numOfRows - 1); + int32_t bytes = pColInfoData->info.bytes; + char* data = taosMemoryMalloc(bytes); + memcpy(data, src, bytes); + for (int j = 0; j < expandRows; ++j) { + colDataAppend(pColInfoData, startRow+j, data, false); + } + //colDataAppendNItems(pColInfoData, startRow, data, expandRows); + taosMemoryFree(data); } } diff --git a/tests/script/sh/compile_udf.sh b/tests/script/sh/compile_udf.sh index c7148d7d7d5200fe12cb2af32172dfd6b1d9c68a..5265e5a99b3400ac93702d7ee8a972f5d2893b33 100755 --- a/tests/script/sh/compile_udf.sh +++ b/tests/script/sh/compile_udf.sh @@ -1,10 +1,11 @@ set +e -rm -rf /tmp/udf/libbitand.so /tmp/udf/libsqrsum.so +rm -rf /tmp/udf/libbitand.so /tmp/udf/libsqrsum.so /tmp/udf/libgpd.so mkdir -p /tmp/udf echo "compile udf bit_and and sqr_sum" gcc -fPIC -shared sh/bit_and.c -I../../include/libs/function/ -I../../include/client -I../../include/util -o /tmp/udf/libbitand.so gcc -fPIC -shared sh/l2norm.c -I../../include/libs/function/ -I../../include/client -I../../include/util -o /tmp/udf/libl2norm.so +gcc -fPIC -shared sh/gpd.c -I../../include/libs/function/ -I../../include/client -I../../include/util -o /tmp/udf/libgpd.so echo "debug show /tmp/udf/*.so" ls /tmp/udf/*.so diff --git a/tests/script/sh/gpd.c b/tests/script/sh/gpd.c index 8d69bacb5edac29783ce860fb3a8dd5b407541d8..2259efa64a500cd7bf331642445c47a08883bec1 100644 --- a/tests/script/sh/gpd.c +++ b/tests/script/sh/gpd.c @@ -12,13 +12,10 @@ TAOS* taos = NULL; DLL_EXPORT int32_t gpd_init() { - taos = taos_connect("localhost", "root", "taosdata", "", 7100); return 0; } DLL_EXPORT int32_t gpd_destroy() { - taos_close(taos); - taos_cleanup(); return 0; } @@ -32,43 +29,18 @@ DLL_EXPORT int32_t gpd(SUdfDataBlock* block, SUdfColumn *resultCol) { SUdfColumnData *resultData = &resultCol->colData; resultData->numOfRows = block->numOfRows; for (int32_t i = 0; i < resultData->numOfRows; ++i) { - int j = 0; - for (; j < block->numOfCols; ++j) { - if (udfColDataIsNull(block->udfCols[j], i)) { - udfColDataSetNull(resultCol, i); - break; - } - } - if ( j == block->numOfCols) { - int32_t luckyNum = 88; - udfColDataSet(resultCol, i, (char *)&luckyNum, false); - } + int64_t* calc_ts = (int64_t*)udfColDataGetData(block->udfCols[0], i); + char* varTbname = udfColDataGetData(block->udfCols[1], i); + char* varDbname = udfColDataGetData(block->udfCols[2], i); + + char dbName[256] = {0}; + char tblName[256] = {0}; + memcpy(dbName, varDataVal(varDbname), varDataLen(varDbname)); + memcpy(tblName, varDataVal(varTbname), varDataLen(varTbname)); + printf("%s, %s\n", dbName, tblName); + int32_t result = 0; + udfColDataSet(resultCol, i, (char*)&result, false); } - TAOS_RES* res = taos_query(taos, "create database if not exists gpd"); - if (taos_errno(res) != 0) { - char* errstr = taos_errstr(res); - } - res = taos_query(taos, "create table gpd.st (ts timestamp, f int) tags(t int)"); - if (taos_errno(res) != 0) { - char* errstr = taos_errstr(res); - } - taos_query(taos, "insert into gpd.t using gpd.st tags(1) values(now, 1) "); - if (taos_errno(res) != 0) { - char* errstr = taos_errstr(res); - } - - taos_query(taos, "select * from gpd.t"); - if (taos_errno(res) != 0) { - char* errstr = taos_errstr(res); - } - - //to simulate actual processing delay by udf -#ifdef LINUX - usleep(1 * 1000); // usleep takes sleep time in us (1 millionth of a second) -#endif -#ifdef WINDOWS - Sleep(1); -#endif return 0; } diff --git a/tests/script/tsim/query/udf_with_const.sim b/tests/script/tsim/query/udf_with_const.sim new file mode 100644 index 0000000000000000000000000000000000000000..7a2a3389bd155e1c384823ce94c3fdd96b14139e --- /dev/null +++ b/tests/script/tsim/query/udf_with_const.sim @@ -0,0 +1,45 @@ +system_content printf %OS% +if $system_content == Windows_NT then + return 0; +endi + +system sh/stop_dnodes.sh +system sh/deploy.sh -n dnode1 -i 1 +system sh/cfg.sh -n dnode1 -c udf -v 1 +system sh/exec.sh -n dnode1 -s start +sql connect + +print ======== step1 udf +system sh/compile_udf.sh +sql create database udf vgroups 3; +sql use udf; + +sql create table t (ts timestamp, f int); +sql insert into t values(now, 1)(now+1s, 2)(now+2s,3)(now+3s,4)(now+4s,5)(now+5s,6)(now+6s,7); + +system_content printf %OS% +if $system_content == Windows_NT then + return 0; +endi + +if $system_content == Windows_NT then + sql create function gpd as 'C:\\Windows\\Temp\\gpd.dll' outputtype int bufSize 8; +else + sql create function gpd as '/tmp/udf/libgpd.so' outputtype int bufSize 8; +endi +sql show functions; +if $rows != 1 then + return -1 +endi + +sql select gpd(ts, tbname, 'detail') from t; +if $rows != 7 then + return -1 +endi +print $data00 $data10 +if $data00 != @0@ then + return -1 +endi +sql drop function gpd; + +system sh/exec.sh -n dnode1 -s stop -x SIGINT