diff --git a/tests/script/sh/bitwise_and.c b/tests/script/sh/bit_and.c similarity index 82% rename from tests/script/sh/bitwise_and.c rename to tests/script/sh/bit_and.c index 496d326dce..2f2e48fdb0 100644 --- a/tests/script/sh/bitwise_and.c +++ b/tests/script/sh/bit_and.c @@ -4,15 +4,15 @@ #include "taosudf.h" -DLL_EXPORT int32_t bitwise_and_init() { +DLL_EXPORT int32_t bit_and_init() { return 0; } -DLL_EXPORT int32_t bitwise_and_destroy() { +DLL_EXPORT int32_t bit_and_destroy() { return 0; } -DLL_EXPORT int32_t bitwise_and(SUdfDataBlock* block, SUdfColumn *resultCol) { +DLL_EXPORT int32_t bit_and(SUdfDataBlock* block, SUdfColumn *resultCol) { if (block->numOfCols < 2) { return TSDB_CODE_UDF_INVALID_INPUT; @@ -37,6 +37,10 @@ DLL_EXPORT int32_t bitwise_and(SUdfDataBlock* block, SUdfColumn *resultCol) { resultData->numOfRows = block->numOfRows; for (int32_t i = 0; i < resultData->numOfRows; ++i) { + if (udfColDataIsNull(block->udfCols[0], i)) { + udfColDataSetNull(resultCol, i); + continue; + } int32_t result = *(int32_t*)udfColDataGetData(block->udfCols[0], i); int j = 1; for (; j < block->numOfCols; ++j) { diff --git a/tests/script/sh/compile_udf.sh b/tests/script/sh/compile_udf.sh new file mode 100755 index 0000000000..12e922b2df --- /dev/null +++ b/tests/script/sh/compile_udf.sh @@ -0,0 +1,10 @@ +set +e + +rm -rf /tmp/udf/libbitand.so /tmp/udf/libsqrsum.so +mkdir -p /tmp/udf +echo "compile udf bit_and and sqr_sum" +gcc -fPIC -shared sh/bit_and.c -o /tmp/udf/libbitand.so +gcc -fPIC -shared sh/sqr_sum.c -o /tmp/udf/libsqrsum.so +echo "debug show /tmp/udf/*.so" +ls /tmp/udf/*.so + diff --git a/tests/script/sh/squares_sum.c b/tests/script/sh/sqr_sum.c similarity index 84% rename from tests/script/sh/squares_sum.c rename to tests/script/sh/sqr_sum.c index 6c6d448b55..af57f377ab 100644 --- a/tests/script/sh/squares_sum.c +++ b/tests/script/sh/sqr_sum.c @@ -5,22 +5,22 @@ #include "taosudf.h" -DLL_EXPORT int32_t squares_sum_init() { +DLL_EXPORT int32_t sqr_sum_init() { return 0; } -DLL_EXPORT int32_t squares_sum_destroy() { +DLL_EXPORT int32_t sqr_sum_destroy() { return 0; } -DLL_EXPORT int32_t squares_sum_start(SUdfInterBuf *buf) { +DLL_EXPORT int32_t sqr_sum_start(SUdfInterBuf *buf) { *(int64_t*)(buf->buf) = 0; buf->bufLen = sizeof(double); buf->numOfResult = 0; return 0; } -DLL_EXPORT int32_t squares_sum(SUdfDataBlock* block, SUdfInterBuf *interBuf, SUdfInterBuf *newInterBuf) { +DLL_EXPORT int32_t sqr_sum(SUdfDataBlock* block, SUdfInterBuf *interBuf, SUdfInterBuf *newInterBuf) { double sumSquares = *(double*)interBuf->buf; int8_t numNotNull = 0; for (int32_t i = 0; i < block->numOfCols; ++i) { @@ -67,7 +67,7 @@ DLL_EXPORT int32_t squares_sum(SUdfDataBlock* block, SUdfInterBuf *interBuf, SUd return 0; } -DLL_EXPORT int32_t udf2_finish(SUdfInterBuf* buf, SUdfInterBuf *resultData) { +DLL_EXPORT int32_t sqr_sum_finish(SUdfInterBuf* buf, SUdfInterBuf *resultData) { if (buf->numOfResult == 0) { resultData->numOfResult = 0; return 0; diff --git a/tests/script/tsim/query/udf.sim b/tests/script/tsim/query/udf.sim new file mode 100644 index 0000000000..7259b1e779 --- /dev/null +++ b/tests/script/tsim/query/udf.sim @@ -0,0 +1,161 @@ +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 show databases; + +sql create table t (ts timestamp, f int); +sql insert into t values(now, 1)(now+1s, 2); + +system_content printf %OS% +if $system_content == Windows_NT then + return 0; +endi +if $system_content == Windows_NT then + sql create function bit_and as 'C:\\Windows\\Temp\\bitand.dll' outputtype int bufSize 8; + sql create aggregate function sqr_sum as 'C:\\Windows\\Temp\\sqrsum.dll' outputtype double bufSize 8; +else + sql create function bit_and as '/tmp/udf/libbitand.so' outputtype int bufSize 8; + sql create aggregate function sqr_sum as '/tmp/udf/libsqrsum.so' outputtype double bufSize 8; +endi +sql show functions; +if $rows != 2 then + return -1 +endi +sql select bit_and(f, f) from t; +if $rows != 2 then + return -1 +endi +if $data00 != 1 then + return -1 +endi +if $data10 != 2 then + return -1 +endi + +sql select sqr_sum(f) from t; +if $rows != 1 then + print expect 1, actual $rows + return -1 +endi +if $data00 != 2.236067977 then + return -1 +endi + +sql create table t2 (ts timestamp, f1 int, f2 int); +sql insert into t2 values(now, 0, 0)(now+1s, 1, 1); +sql select bit_and(f1, f2) from t2; +if $rows != 2 then + return -1 +endi +if $data00 != 0 then + return -1 +endi +if $data10 != 1 then + return -1 +endi + +sql select sqr_sum(f1, f2) from t2; +if $rows != 1 then + return -1 +endi +if $data00 != 1.414213562 then + return -1 +endi + +sql insert into t2 values(now+2s, 1, null)(now+3s, null, 2); +sql select bit_and(f1, f2) from t2; +print $rows , $data00 , $data10 , $data20 , $data30 +if $rows != 4 then + return -1 +endi +if $data00 != 0 then + return -1 +endi +if $data10 != 1 then + return -1 +endi + +if $data20 != NULL then + return -1 +endi + +if $data30 != NULL then + return -1 +endi + +sql select sqr_sum(f1, f2) from t2; +print $rows, $data00 +if $rows != 1 then + return -1 +endi +if $data00 != 2.645751311 then + return -1 +endi + +sql insert into t2 values(now+4s, 4, 8)(now+5s, 5, 9); +sql select sqr_sum(f1-f2), sqr_sum(f1+f2) from t2; +print $rows , $data00 , $data01 +if $rows != 1 then + return -1; +endi +if $data00 != 5.656854249 then + return -1 +endi +if $data01 != 18.547236991 then + return -1 +endi + +sql select sqr_sum(bit_and(f2, f1)), sqr_sum(bit_and(f1, f2)) from t2; +print $rows , $data00 , $data01 +if $rows != 1 then + return -1 +endi +if $data00 != 1.414213562 then + return -1 +endi +if $data01 != 1.414213562 then + return -1 +endi + +sql select sqr_sum(f2) from udf.t2 group by 1-bit_and(f1, f2) order by 1-bit_and(f1,f2); +print $rows , $data00 , $data10 , $data20 +if $rows != 3 then + return -1 +endi +if $data00 != 2.000000000 then + return -1 +endi +if $data10 != 9.055385138 then + return -1 +endi +if $data20 != 8.000000000 then + return -1 +endi + +sql drop function bit_and; +sql show functions; +if $rows != 1 then + return -1 +endi +if $data00 != @sqr_sum@ then + return -1 + endi +sql drop function sqr_sum; +sql show functions; +if $rows != 0 then + return -1 +endi + +system sh/exec.sh -n dnode1 -s stop -x SIGINT