Skip to content

Commit 679c9ef

Browse files
💬Generate LLM translations (#1739)
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: z <[email protected]>
1 parent 4e07d4a commit 679c9ef

File tree

1 file changed

+104
-69
lines changed

1 file changed

+104
-69
lines changed

docs/cn/guides/54-query/03-udf.md

Lines changed: 104 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,29 @@
1+
```markdown
12
---
2-
title: 用户定义函数
3+
title: User-Defined Function
34
---
5+
46
import IndexOverviewList from '@site/src/components/IndexOverviewList';
57

68
import EEFeature from '@site/src/components/EEFeature';
79

810
<EEFeature featureName='Python UDF'/>
911

10-
用户定义函数(UDFs通过支持匿名 lambda 表达式和预定义的处理程序(Python、JavaScript 和 WebAssembly)来定义 UDF,提供了更高的灵活性。这些功能允许用户根据其特定的数据处理需求创建自定义操作。Databend 的 UDFs 分为以下几种类型
12+
用户自定义函数 (UDFs) 通过支持匿名 lambda 表达式和预定义的处理程序(Python、JavaScript 和 WebAssembly)来定义 UDF,从而提供增强的灵活性。这些功能允许用户创建根据其特定数据处理需求量身定制的自定义操作。Databend UDF 分为以下类型
1113

1214
- [Lambda UDFs](#lambda-udfs)
13-
- [嵌入式 UDFs](#embedded-udfs)
15+
- [Embedded UDFs](#embedded-udfs)
1416

1517
## Lambda UDFs
1618

17-
Lambda UDF 允许用户直接在查询中使用匿名函数(lambda 表达式)定义自定义操作。这些 lambda 表达式通常简洁,可用于执行特定的数据转换或计算,这些操作可能无法仅通过内置函数实现
19+
lambda UDF 允许用户直接在其查询中使用匿名函数(lambda 表达式)来定义自定义操作。这些 lambda 表达式通常简洁明了,可用于执行仅使用内置函数可能无法实现的特定数据转换或计算
1820

19-
### 使用示例
21+
### Usage Examples
2022

21-
本示例创建 UDFs,以使用 SQL 查询从表中的 JSON 数据中提取特定值。
23+
此示例创建 UDF,以使用 SQL 查询从表中的 JSON 数据中提取特定值。
2224

2325
```sql
24-
-- 定义 UDFs
26+
-- Define UDFs
2527
CREATE FUNCTION get_v1 AS (input_json) -> input_json['v1'];
2628
CREATE FUNCTION get_v2 AS (input_json) -> input_json['v2'];
2729

@@ -34,13 +36,13 @@ SHOW USER FUNCTIONS;
3436
│ get_v2 │ NULL │ │ {"parameters":["input_json"]} │ SQL │ 2024-11-18 23:21:46.838744 │
3537
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
3638

37-
-- 创建表
39+
-- Create a table
3840
CREATE TABLE json_table(time TIMESTAMP, data JSON);
3941

40-
-- 插入时间事件
42+
-- Insert a time event
4143
INSERT INTO json_table VALUES('2022-06-01 00:00:00.00000', PARSE_JSON('{"v1":1.5, "v2":20.5}'));
4244

43-
-- 从事件中获取 v1 v2
45+
-- Get v1 and v2 value from the event
4446
SELECT get_v1(data), get_v2(data) FROM json_table;
4547
+------------+------------+
4648
| data['v1'] | data['v2'] |
@@ -49,79 +51,82 @@ SELECT get_v1(data), get_v2(data) FROM json_table;
4951
+------------+------------+
5052
```
5153

52-
## 嵌入式 UDFs
54+
## Embedded UDFs
5355

54-
嵌入式 UDFs 允许您在 SQL 中嵌入以下编程语言编写的代码
56+
Embedded UDF 允许您在 SQL 中嵌入使用以下编程语言编写的代码
5557

5658
- [Python](#python)
5759
- [JavaScript](#javascript)
5860
- [WebAssembly](#webassembly)
5961

62+
使用 Embedded UDF,您可以创建标量函数和聚合函数。标量函数对单行输入进行操作并返回单个值,而聚合函数处理多行输入并返回单个聚合结果,例如总和或平均值。
63+
6064
:::note
61-
如果您的程序内容较大,可以将其压缩后传递到 stage。请参阅 WebAssembly 的[使用示例](#usage-examples-2)
65+
- 尚不支持使用 WebAssembly 创建聚合 UDF。
66+
- 如果您的程序内容很大,您可以压缩它,然后将其传递到 Stage。有关 WebAssembly,请参见 [Usage Examples](#usage-examples-2)
6267
:::
6368

64-
### Python(需要 Databend Enterprise
69+
### Python (requires Databend Enterprise)
6570

66-
Python UDF 允许您通过 Databend 的内置处理程序从 SQL 查询中调用 Python 代码,从而在 SQL 查询中无缝集成 Python 逻辑。
71+
Python UDF 允许您通过 Databend 的内置处理程序从 SQL 查询中调用 Python 代码,从而可以在 SQL 查询中无缝集成 Python 逻辑。
6772

6873
:::note
69-
Python UDF 必须仅使用 Python 的标准库;不允许使用第三方导入
74+
Python UDF 必须仅使用 Python 的标准库;不允许第三方导入
7075
:::
7176

72-
#### 数据类型映射
77+
#### Data Type Mappings
7378

74-
请参阅开发者指南中的[数据类型映射](/developer/drivers/python#data-type-mappings)
79+
请参见开发者指南中的 [Data Type Mappings](/developer/drivers/python#data-type-mappings)
7580

76-
#### 使用示例
81+
#### Usage Examples
7782

78-
本示例定义了一个用于情感分析的 Python UDF,创建了一个表,插入了示例数据,并对文本数据进行了情感分析
83+
此示例定义了一个用于情感分析的 Python UDF,创建了一个表,插入了示例数据,并对文本数据执行情感分析
7984

8085
1. 定义一个名为 `sentiment_analysis` 的 Python UDF。
8186

8287
```sql
83-
-- 创建情感分析函数
88+
-- Create the sentiment analysis function
8489
CREATE OR REPLACE FUNCTION sentiment_analysis(STRING) RETURNS STRING
8590
LANGUAGE python HANDLER = 'sentiment_analysis_handler'
8691
AS $$
8792
def remove_stop_words(text, stop_words):
8893
"""
89-
从文本中移除常见的停用词。
90-
91-
参数:
92-
text (str): 输入文本。
93-
stop_words (set): 要移除的停用词集合。
94-
95-
返回:
96-
str: 移除停用词后的文本。
94+
Removes common stop words from the text.
95+
96+
Args:
97+
text (str): The input text.
98+
stop_words (set): A set of stop words to remove.
99+
100+
Returns:
101+
str: Text with stop words removed.
97102
"""
98103
return ' '.join([word for word in text.split() if word.lower() not in stop_words])
99104

100105
def calculate_sentiment(text, positive_words, negative_words):
101106
"""
102-
计算文本的情感得分。
103-
104-
参数:
105-
text (str): 输入文本。
106-
positive_words (set): 正面词集合。
107-
negative_words (set): 负面词集合。
108-
109-
返回:
110-
int: 情感得分。
107+
Calculates the sentiment score of the text.
108+
109+
Args:
110+
text (str): The input text.
111+
positive_words (set): A set of positive words.
112+
negative_words (set): A set of negative words.
113+
114+
Returns:
115+
int: Sentiment score.
111116
"""
112117
words = text.split()
113118
score = sum(1 for word in words if word in positive_words) - sum(1 for word in words if word in negative_words)
114119
return score
115120

116121
def get_sentiment_label(score):
117122
"""
118-
根据情感得分确定情感标签。
119-
120-
参数:
121-
score (int): 情感得分。
122-
123-
返回:
124-
str: 情感标签('Positive', 'Negative', 'Neutral')。
123+
Determines the sentiment label based on the sentiment score.
124+
125+
Args:
126+
score (int): The sentiment score.
127+
128+
Returns:
129+
str: Sentiment label ('Positive', 'Negative', 'Neutral').
125130
"""
126131
if score > 0:
127132
return 'Positive'
@@ -132,13 +137,13 @@ def get_sentiment_label(score):
132137

133138
def sentiment_analysis_handler(text):
134139
"""
135-
分析输入文本的情感。
136-
137-
参数:
138-
text (str): 输入文本。
139-
140-
返回:
141-
str: 情感分析结果,包括得分和标签。
140+
Analyzes the sentiment of the input text.
141+
142+
Args:
143+
text (str): The input text.
144+
145+
Returns:
146+
str: Sentiment analysis result including the score and label.
142147
"""
143148
stop_words = set(["a", "an", "the", "and", "or", "but", "if", "then", "so"])
144149
positive_words = set(["good", "happy", "joy", "excellent", "positive", "love"])
@@ -147,21 +152,21 @@ def sentiment_analysis_handler(text):
147152
clean_text = remove_stop_words(text, stop_words)
148153
sentiment_score = calculate_sentiment(clean_text, positive_words, negative_words)
149154
sentiment_label = get_sentiment_label(sentiment_score)
150-
155+
151156
return f'Sentiment Score: {sentiment_score}; Sentiment Label: {sentiment_label}'
152157
$$;
153158
```
154159

155-
2. 使用 `sentiment_analysis` 函数对文本数据进行情感分析
160+
2. 使用 `sentiment_analysis` 函数对文本数据执行情感分析
156161

157162
```sql
158163
CREATE OR REPLACE TABLE texts (
159164
original_text STRING
160165
);
161166

162-
-- 插入示例数据
167+
-- Insert sample data
163168
INSERT INTO texts (original_text)
164-
VALUES
169+
VALUES
165170
('The quick brown fox feels happy and joyful'),
166171
('A hard journey, but it was painful and sad'),
167172
('Uncertain outcomes leave everyone unsure and hesitant'),
@@ -186,14 +191,14 @@ FROM
186191

187192
### JavaScript
188193

189-
JavaScript UDF 允许您通过 Databend 的内置处理程序从 SQL 查询中调用 JavaScript 代码,从而在 SQL 查询中无缝集成 JavaScript 逻辑。
194+
JavaScript UDF 允许您通过 Databend 的内置处理程序从 SQL 查询中调用 JavaScript 代码,从而可以在 SQL 查询中无缝集成 JavaScript 逻辑。
190195

191-
#### 数据类型映射
196+
#### Data Type Mappings
192197

193198
下表显示了 Databend 和 JavaScript 之间的类型映射:
194199

195-
| Databend 类型 | JS 类型 |
196-
|-------------------|------------|
200+
| Databend Type | JS Type |
201+
| ----------------- | ---------- |
197202
| NULL | null |
198203
| BOOLEAN | Boolean |
199204
| TINYINT | Number |
@@ -211,9 +216,9 @@ JavaScript UDF 允许您通过 Databend 的内置处理程序从 SQL 查询中
211216
| DECIMAL | BigDecimal |
212217
| BINARY | Uint8Array |
213218

214-
#### 使用示例
219+
#### Usage Examples
215220

216-
本示例定义了一个名为 "gcd_js" 的 JavaScript UDF,用于计算两个整数的最大公约数GCD,并在 SQL 查询中应用它:
221+
此示例定义了一个名为 "gcd_js" 的 JavaScript UDF,用于计算两个整数的最大公约数 (GCD),并在 SQL 查询中应用它:
217222

218223
```sql
219224
CREATE FUNCTION gcd_js (INT, INT) RETURNS BIGINT LANGUAGE javascript HANDLER = 'gcd_js' AS $$
@@ -237,18 +242,47 @@ WHERE
237242
ORDER BY 1;
238243
```
239244

245+
此示例定义了一个聚合 UDF,该 UDF 通过根据一组值的相应权重对其进行聚合来计算该组值的加权平均值:
246+
247+
```sql
248+
CREATE FUNCTION weighted_avg (INT, INT) STATE {sum INT, weight INT} RETURNS FLOAT
249+
LANGUAGE javascript AS $$
250+
export function create_state() {
251+
return {sum: 0, weight: 0};
252+
}
253+
export function accumulate(state, value, weight) {
254+
state.sum += value * weight;
255+
state.weight += weight;
256+
return state;
257+
}
258+
export function retract(state, value, weight) {
259+
state.sum -= value * weight;
260+
state.weight -= weight;
261+
return state;
262+
}
263+
export function merge(state1, state2) {
264+
state1.sum += state2.sum;
265+
state1.weight += state2.weight;
266+
return state1;
267+
}
268+
export function finish(state) {
269+
return state.sum / state.weight;
270+
}
271+
$$;
272+
```
273+
240274
### WebAssembly
241275

242-
WebAssembly UDF 允许用户使用编译为 WebAssembly 的语言定义自定义逻辑或操作。这些 UDFs 可以直接在 SQL 查询中调用,以执行特定的计算或数据转换。
276+
WebAssembly UDF 允许用户使用编译为 WebAssembly 的语言定义自定义逻辑或操作。然后可以直接在 SQL 查询中调用这些 UDF,以执行特定的计算或数据转换。
243277

244-
#### 使用示例
278+
#### Usage Examples
245279

246-
在本示例中,创建了 "wasm_gcd" 函数来计算两个整数的最大公约数GCD。该函数使用 WebAssembly 定义,其实现位于 'test10_udf_wasm_gcd.wasm.zst' 二进制文件中。
280+
在此示例中,创建 "wasm_gcd" 函数来计算两个整数的最大公约数 (GCD)。该函数使用 WebAssembly 定义,其实现在 'test10_udf_wasm_gcd.wasm.zst' 二进制文件中。
247281

248-
在执行之前,函数实现经过一系列步骤。首先,它被编译为二进制文件,然后压缩为 'test10_udf_wasm_gcd.wasm.zst'。最后,压缩文件被提前上传到 stage
282+
在执行之前,函数实现会经过一系列步骤。首先,将其编译为二进制文件,然后压缩为 'test10_udf_wasm_gcd.wasm.zst'。最后,将压缩文件提前上传到 Stage
249283

250284
:::note
251-
该函数可以用 Rust 实现,如 https://github.com/risingwavelabs/arrow-udf/blob/main/arrow-udf-wasm/examples/wasm.rs 中的示例所示。
285+
该函数可以使用 Rust 实现,如 https://github.com/risingwavelabs/arrow-udf/blob/main/arrow-udf-wasm/examples/wasm.rs 提供的示例所示
252286
:::
253287

254288
```sql
@@ -264,6 +298,7 @@ WHERE
264298
ORDER BY 1;
265299
```
266300

267-
## 管理 UDFs
301+
## Managing UDFs
268302

269-
Databend 提供了多种命令来管理 UDFs。详情请参阅[用户定义函数](/sql/sql-commands/ddl/udf/)
303+
Databend 提供了各种命令来管理 UDF。有关详细信息,请参见 [User-Defined Function](/sql/sql-commands/ddl/udf/)
304+
```

0 commit comments

Comments
 (0)