未验证 提交 64e89620 编写于 作者: W wade zhang 提交者: GitHub

Merge pull request #21484 from taosdata/szhou/enhance-udf-doc

enhance: python udf sample with numpy
...@@ -377,7 +377,7 @@ The `pybitand` function implements bitwise addition for multiple columns. If the ...@@ -377,7 +377,7 @@ The `pybitand` function implements bitwise addition for multiple columns. If the
#### Aggregate Function [pyl2norm](https://github.com/taosdata/TDengine/blob/3.0/tests/script/sh/pyl2norm.py) #### Aggregate Function [pyl2norm](https://github.com/taosdata/TDengine/blob/3.0/tests/script/sh/pyl2norm.py)
The `pyl2norm` function finds the second-order norm for all data in the input column. This squares the values, takes a cumulative sum, and finds the square root. The `pyl2norm` function finds the second-order norm for all data in the input columns. This squares the values, takes a cumulative sum, and finds the square root.
<details> <details>
<summary>pyl2norm.py</summary> <summary>pyl2norm.py</summary>
...@@ -387,5 +387,16 @@ The `pyl2norm` function finds the second-order norm for all data in the input co ...@@ -387,5 +387,16 @@ The `pyl2norm` function finds the second-order norm for all data in the input co
</details> </details>
#### Aggregate Function [pycumsum](https://github.com/taosdata/TDengine/blob/3.0/tests/script/sh/pycumsum.py)
The `pycumsum` function finds the cumulative sum for all data in the input columns.
<details>
<summary>pycumsum.py</summary>
```c
{{#include tests/script/sh/pycumsum.py}}
```
</details>
## Manage and Use UDF ## Manage and Use UDF
You need to add UDF to TDengine before using it in SQL queries. For more information about how to manage UDF and how to invoke UDF, please see [Manage and Use UDF](../12-taos-sql/26-udf.md). You need to add UDF to TDengine before using it in SQL queries. For more information about how to manage UDF and how to invoke UDF, please see [Manage and Use UDF](../12-taos-sql/26-udf.md).
...@@ -335,7 +335,7 @@ def init() ...@@ -335,7 +335,7 @@ def init()
def destroy() def destroy()
``` ```
其中 init 完成初始化工作。 destroy 完成清理工作。如果没有初始化工作,无需定义 init 函数。如果没有清理工作,无需定义 destroy 函数。 其中 init 完成初始化工作。 destroy 完成清理工作。
### Python 和 TDengine之间的数据类型映射 ### Python 和 TDengine之间的数据类型映射
...@@ -386,6 +386,17 @@ pyl2norm 实现了输入列的所有数据的二阶范数,即对每个数据 ...@@ -386,6 +386,17 @@ pyl2norm 实现了输入列的所有数据的二阶范数,即对每个数据
</details> </details>
### 聚合函数示例 [pycumsum](https://github.com/taosdata/TDengine/blob/3.0/tests/script/sh/pycumsum.py)
pycumsum 使用 numpy 计算输入列所有数据的累积和。
<details>
<summary>pycumsum.py</summary>
```c
{{#include tests/script/sh/pycumsum.py}}
```
</details>
## 管理和使用 UDF ## 管理和使用 UDF
在使用 UDF 之前需要先将其加入到 TDengine 系统中。关于如何管理和使用 UDF,请参考[管理和使用 UDF](../12-taos-sql/26-udf.md) 在使用 UDF 之前需要先将其加入到 TDengine 系统中。关于如何管理和使用 UDF,请参考[管理和使用 UDF](../12-taos-sql/26-udf.md)
import pickle
import numpy as np
def init():
pass
def destroy():
pass
def start():
return pickle.dumps(0.0)
def finish(buf):
return pickle.loads(buf)
def reduce(datablock, buf):
(rows, cols) = datablock.shape()
state = pickle.loads(buf)
row = []
for i in range(rows):
for j in range(cols):
cell = datablock.data(i, j)
if cell is not None:
row.append(datablock.data(i, j))
if len(row) > 1:
new_state = np.cumsum(row)[-1]
else:
new_state = state
return pickle.dumps(new_state)
...@@ -15,6 +15,7 @@ system sh/prepare_pyudf.sh ...@@ -15,6 +15,7 @@ system sh/prepare_pyudf.sh
system mkdir -p /tmp/pyudf system mkdir -p /tmp/pyudf
system cp sh/pybitand.py /tmp/pyudf/ system cp sh/pybitand.py /tmp/pyudf/
system cp sh/pyl2norm.py /tmp/pyudf/ system cp sh/pyl2norm.py /tmp/pyudf/
system cp sh/pycumsum.py /tmp/pyudf/
system ls /tmp/pyudf system ls /tmp/pyudf
sql create database udf vgroups 3; sql create database udf vgroups 3;
...@@ -280,6 +281,18 @@ if $data20 != 8.000000000 then ...@@ -280,6 +281,18 @@ if $data20 != 8.000000000 then
return -1 return -1
endi endi
sql create aggregate function pycumsum as '/tmp/pyudf/pycumsum.py' outputtype double bufSize 128 language 'python';
sql select pycumsum(f2) from udf.t2
print ======= pycumsum
print $rows $data00
if $rows != 1 then
return -1
endi
if $data00 != 20.000000000 then
return -1
endi
sql drop function pycumsum
sql create or replace function bit_and as '/tmp/udf/libbitand.so' outputtype int sql create or replace function bit_and as '/tmp/udf/libbitand.so' outputtype int
sql select func_version from information_schema.ins_functions where name='bit_and' sql select func_version from information_schema.ins_functions where name='bit_and'
if $data00 != 1 then if $data00 != 1 then
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册