From 3db80837256488ea086d85f253ba00eb17a64ce7 Mon Sep 17 00:00:00 2001 From: shenglian zhou Date: Thu, 4 Aug 2022 22:12:17 +0800 Subject: [PATCH 1/3] fix: restore tsim/query/udf.sim --- tests/script/sh/{bitwise_and.c => bit_and.c} | 6 +- tests/script/sh/compile_udf.sh | 10 ++ tests/script/sh/{squares_sum.c => sqr_sum.c} | 10 +- tests/script/tsim/query/udf.sim | 158 +++++++++++++++++++ 4 files changed, 176 insertions(+), 8 deletions(-) rename tests/script/sh/{bitwise_and.c => bit_and.c} (88%) create mode 100644 tests/script/sh/compile_udf.sh rename tests/script/sh/{squares_sum.c => sqr_sum.c} (84%) create mode 100644 tests/script/tsim/query/udf.sim diff --git a/tests/script/sh/bitwise_and.c b/tests/script/sh/bit_and.c similarity index 88% rename from tests/script/sh/bitwise_and.c rename to tests/script/sh/bit_and.c index 496d326dce..6aeeaf2530 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; diff --git a/tests/script/sh/compile_udf.sh b/tests/script/sh/compile_udf.sh new file mode 100644 index 0000000000..7cfd387c6d --- /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 bit_and.c -o /tmp/udf/libbitand.so +gcc -fPIC -shared 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..0386d38993 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_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..e0e0307c51 --- /dev/null +++ b/tests/script/tsim/query/udf.sim @@ -0,0 +1,158 @@ +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_add(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_add(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, f2-f1)), sqr_sum(bit_and(f2/f1, f1/f2)) from t2; +print $rows , $data00 , $data01 +if $rows != 1 then + return -1 +endi +if $data00 != 176.000000000 then + return -1 +endi +if $data01 != 152.420471066 then + return -1 +endi + +sql select sqr_sum(f2) from udf.t2 group by 1-bit_and(f1); +print $rows , $data00 , $data10 +if $rows != 2 then + return -1 +endi +if $data00 != 2.000000000 then + return -1 +endi +if $data10 != 12.083045974 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 From a51fd6d2dc0054c9b4cd7125bb16681d1c45b05e Mon Sep 17 00:00:00 2001 From: shenglian zhou Date: Thu, 4 Aug 2022 22:14:57 +0800 Subject: [PATCH 2/3] fix: restore tsim/query/udf.sim --- tests/script/tsim/query/udf.sim | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/script/tsim/query/udf.sim b/tests/script/tsim/query/udf.sim index e0e0307c51..d1858f0efe 100644 --- a/tests/script/tsim/query/udf.sim +++ b/tests/script/tsim/query/udf.sim @@ -55,7 +55,7 @@ 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_add(f1, f2) from t2; +sql select bit_and(f1, f2) from t2; if $rows != 2 then return -1 endi @@ -75,7 +75,7 @@ if $data00 != 1.414213562 then endi sql insert into t2 values(now+2s, 1, null)(now+3s, null, 2); -sql select bit_add(f1, f2) from t2; +sql select bit_and(f1, f2) from t2; print $rows , $data00 , $data10 , $data20 , $data30 if $rows != 4 then return -1 @@ -117,7 +117,7 @@ if $data01 != 18.547236991 then return -1 endi -sql select sqr_sum(bit_and(f2-f1, f2-f1)), sqr_sum(bit_and(f2/f1, f1/f2)) from t2; +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 From 521e5163a7c47678c1a6a542f9b831966e75d445 Mon Sep 17 00:00:00 2001 From: slzhou Date: Fri, 5 Aug 2022 09:18:03 +0800 Subject: [PATCH 3/3] fix: add sample udf cases and compile outside of tdengine --- tests/script/sh/bit_and.c | 4 ++++ tests/script/sh/compile_udf.sh | 4 ++-- tests/script/sh/sqr_sum.c | 2 +- tests/script/tsim/query/udf.sim | 15 +++++++++------ 4 files changed, 16 insertions(+), 9 deletions(-) mode change 100644 => 100755 tests/script/sh/compile_udf.sh diff --git a/tests/script/sh/bit_and.c b/tests/script/sh/bit_and.c index 6aeeaf2530..2f2e48fdb0 100644 --- a/tests/script/sh/bit_and.c +++ b/tests/script/sh/bit_and.c @@ -37,6 +37,10 @@ DLL_EXPORT int32_t bit_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 old mode 100644 new mode 100755 index 7cfd387c6d..12e922b2df --- a/tests/script/sh/compile_udf.sh +++ b/tests/script/sh/compile_udf.sh @@ -3,8 +3,8 @@ 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 bit_and.c -o /tmp/udf/libbitand.so -gcc -fPIC -shared sqr_sum.c -o /tmp/udf/libsqrsum.so +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/sqr_sum.c b/tests/script/sh/sqr_sum.c index 0386d38993..af57f377ab 100644 --- a/tests/script/sh/sqr_sum.c +++ b/tests/script/sh/sqr_sum.c @@ -67,7 +67,7 @@ DLL_EXPORT int32_t sqr_sum(SUdfDataBlock* block, SUdfInterBuf *interBuf, SUdfInt return 0; } -DLL_EXPORT int32_t sqr_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 index d1858f0efe..7259b1e779 100644 --- a/tests/script/tsim/query/udf.sim +++ b/tests/script/tsim/query/udf.sim @@ -122,22 +122,25 @@ print $rows , $data00 , $data01 if $rows != 1 then return -1 endi -if $data00 != 176.000000000 then +if $data00 != 1.414213562 then return -1 endi -if $data01 != 152.420471066 then +if $data01 != 1.414213562 then return -1 endi -sql select sqr_sum(f2) from udf.t2 group by 1-bit_and(f1); -print $rows , $data00 , $data10 -if $rows != 2 then +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 != 12.083045974 then +if $data10 != 9.055385138 then + return -1 +endi +if $data20 != 8.000000000 then return -1 endi