Skip to content

Commit 685193a

Browse files
authored
docs: improve external function documentation with end-to-end examples and protocol fix (#3153)
* docs: add end-to-end Python UDF server example for external function * docs: fix external function protocol description, replace HTTP/HTTPS with Arrow Flight
1 parent 5d592e9 commit 685193a

4 files changed

Lines changed: 106 additions & 12 deletions

File tree

docs/cn/sql-reference/10-sql-commands/00-ddl/11-external-function/ddl-create-function.md

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,59 @@ CREATE [ OR REPLACE ] FUNCTION [ IF NOT EXISTS ] <function_name>
3535

3636
## 示例
3737

38-
以下示例创建一个外部函数,用于计算两个整数的最大公约数(GCD):
38+
以下示例完整演示如何创建一个计算最大公约数(GCD)的外部函数。
39+
40+
### 第一步:启动 Python UDF 服务
41+
42+
安装 `databend-udf` 包:
43+
44+
```bash
45+
pip install databend-udf
46+
```
47+
48+
创建文件 `udf_server.py`
49+
50+
```python
51+
from databend_udf import udf, UDFServer
52+
53+
@udf(
54+
input_types=["INT", "INT"],
55+
result_type="INT",
56+
skip_null=True,
57+
)
58+
def gcd(x: int, y: int) -> int:
59+
while y != 0:
60+
(x, y) = (y, x % y)
61+
return x
62+
63+
if __name__ == '__main__':
64+
server = UDFServer("0.0.0.0:8815")
65+
server.add_function(gcd)
66+
server.serve()
67+
```
68+
69+
启动服务:
70+
71+
```bash
72+
python udf_server.py
73+
```
74+
75+
### 第二步:在 Databend 中注册函数
3976

4077
```sql
41-
CREATE FUNCTION gcd AS (INT, INT)
42-
RETURNS INT
43-
LANGUAGE python
44-
HANDLER = 'gcd'
78+
CREATE FUNCTION gcd AS (INT, INT)
79+
RETURNS INT
80+
LANGUAGE python
81+
HANDLER = 'gcd'
4582
ADDRESS = 'http://localhost:8815';
4683
```
84+
85+
### 第三步:调用函数
86+
87+
```sql
88+
SELECT gcd(48, 18);
89+
-- 返回:6
90+
91+
SELECT gcd(100, 75);
92+
-- 返回:25
93+
```

docs/cn/sql-reference/10-sql-commands/00-ddl/11-external-function/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ title: 外部函数(External Function)
1313
| [DROP EXTERNAL FUNCTION](ddl-drop-function.md) | 移除一个外部函数(External Function) |
1414

1515
:::note
16-
Databend 中的外部函数(External Function)允许你通过与 HTTP/HTTPS 端点集成的外部服务来扩展功能,从而利用外部处理能力
16+
Databend 外部函数允许你用自定义逻辑扩展 SQL,逻辑运行在远程服务器上,通过 [Apache Arrow Flight](https://arrow.apache.org/docs/format/Flight.html) 协议与 Databend 通信。远程处理程序可以用任意语言实现,Python 是最常见的选择,可通过 [databend-udf](https://pypi.org/project/databend-udf) 包快速上手
1717
:::

docs/en/sql-reference/10-sql-commands/00-ddl/11-external-function/ddl-create-function.md

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,59 @@ CREATE [ OR REPLACE ] FUNCTION [ IF NOT EXISTS ] <function_name>
3535

3636
## Examples
3737

38-
This example creates an external function that calculates the greatest common divisor (GCD) of two integers:
38+
This example walks through a complete end-to-end setup for an external function that calculates the greatest common divisor (GCD) of two integers.
39+
40+
### Step 1: Set Up the Python UDF Server
41+
42+
Install the `databend-udf` package:
43+
44+
```bash
45+
pip install databend-udf
46+
```
47+
48+
Create a file `udf_server.py` with the following content:
49+
50+
```python
51+
from databend_udf import udf, UDFServer
52+
53+
@udf(
54+
input_types=["INT", "INT"],
55+
result_type="INT",
56+
skip_null=True,
57+
)
58+
def gcd(x: int, y: int) -> int:
59+
while y != 0:
60+
(x, y) = (y, x % y)
61+
return x
62+
63+
if __name__ == '__main__':
64+
server = UDFServer("0.0.0.0:8815")
65+
server.add_function(gcd)
66+
server.serve()
67+
```
68+
69+
Start the server:
70+
71+
```bash
72+
python udf_server.py
73+
```
74+
75+
### Step 2: Register the Function in Databend
3976

4077
```sql
41-
CREATE FUNCTION gcd AS (INT, INT)
42-
RETURNS INT
43-
LANGUAGE python
44-
HANDLER = 'gcd'
78+
CREATE FUNCTION gcd AS (INT, INT)
79+
RETURNS INT
80+
LANGUAGE python
81+
HANDLER = 'gcd'
4582
ADDRESS = 'http://localhost:8815';
4683
```
84+
85+
### Step 3: Call the Function
86+
87+
```sql
88+
SELECT gcd(48, 18);
89+
-- Returns: 6
90+
91+
SELECT gcd(100, 75);
92+
-- Returns: 25
93+
```

docs/en/sql-reference/10-sql-commands/00-ddl/11-external-function/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ This page provides a comprehensive overview of External Function operations in D
1313
| [DROP EXTERNAL FUNCTION](ddl-drop-function.md) | Removes an external function |
1414

1515
:::note
16-
External Functions in Databend allow you to extend functionality by integrating with external services through HTTP/HTTPS endpoints, enabling you to leverage external processing capabilities.
16+
External Functions in Databend allow you to extend SQL with custom logic running on a remote server. The server communicates with Databend over the [Apache Arrow Flight](https://arrow.apache.org/docs/format/Flight.html) protocol. The remote handler can be written in any language — Python is the most common choice via the [databend-udf](https://pypi.org/project/databend-udf) package.
1717
:::

0 commit comments

Comments
 (0)