You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/en/developer/00-drivers/01-python.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@ title: Python
4
4
5
5
Databend offers the following Python packages enabling you to develop Python applications that interact with Databend:
6
6
7
-
-[databend-py (**Recommendation**)](https://github.com/databendcloud/databend-py): Provides a direct interface to the Databend database. It allows you to perform standard Databend operations such as user login, database and table creation, data insertion/loading, and querying.
7
+
-[databend-py (**Recommended**)](https://github.com/databendcloud/databend-py): Provides a direct interface to the Databend database. It allows you to perform standard Databend operations such as user login, database and table creation, data insertion/loading, and querying.
8
8
-[databend-sqlalchemy](https://github.com/databendcloud/databend-sqlalchemy): Provides a SQL toolkit and [Object-Relational Mapping](https://en.wikipedia.org/wiki/Object%E2%80%93relational_mapping) to interface with the Databend database. [SQLAlchemy](https://www.sqlalchemy.org/) is a popular SQL toolkit and ORM for Python, and databend-SQLAlchemy is a dialect for SQLAlchemy that allows you to use SQLAlchemy to interact with Databend.
9
9
10
10
Both packages require Python version 3.5 or higher. To check your Python version, run `python --version` in your command prompt. To install the latest `databend-py` or `databend-sqlalchemy` package:
Copy file name to clipboardExpand all lines: docs/en/guides/54-query/03-udf.md
+127-3Lines changed: 127 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -3,7 +3,7 @@ title: User-Defined Function
3
3
---
4
4
import IndexOverviewList from '@site/src/components/IndexOverviewList';
5
5
6
-
User-Defined Functions (UDFs) offer enhanced flexibility by supporting both anonymous lambda expressions and predefined handlers (JavaScript & WebAssembly) for defining UDFs. These features allow users to create custom operations tailored to their specific data processing needs. Databend UDFs are categorized into the following types:
6
+
User-Defined Functions (UDFs) offer enhanced flexibility by supporting both anonymous lambda expressions and predefined handlers (Python, JavaScript & WebAssembly) for defining UDFs. These features allow users to create custom operations tailored to their specific data processing needs. Databend UDFs are categorized into the following types:
7
7
8
8
-[Lambda UDFs](#lambda-udf)
9
9
-[Embedded UDFs](#embedded-udfs)
@@ -42,16 +42,140 @@ SELECT get_v1(data), get_v2(data) FROM json_table;
42
42
43
43
Embedded UDFs allow you to embed code written in the following programming languages within SQL:
44
44
45
+
-[Python](#python)
45
46
-[JavaScript](#javascript)
46
47
-[WebAssembly](#webassembly)
47
48
48
49
:::note
49
-
If your program content is large, you can compress it and then pass it to the stage. See the [Usage Examples](#usage-examples-2) for WebAssembly.
50
+
If your program content is large, you can compress it and then pass it to a stage. See the [Usage Examples](#usage-examples-2) for WebAssembly.
50
51
:::
51
52
53
+
### Python
54
+
55
+
A Python UDF allows you to invoke Python code from a SQL query via Databend's built-in handler, enabling seamless integration of Python logic within your SQL queries.
56
+
57
+
:::note
58
+
The Python UDF must use only Python's standard library; third-party imports are not allowed.
59
+
:::
60
+
61
+
#### Data Type Mappings
62
+
63
+
See [Data Type Mappings](/developer/drivers/python#data-type-mappings) in the Developer Guide.
64
+
65
+
#### Usage Examples
66
+
67
+
This example defines a Python UDF for sentiment analysis, creates a table, inserts sample data, and performs sentiment analysis on the text data.
68
+
69
+
1. Define a Python UDF named `sentiment_analysis`.
70
+
71
+
```sql
72
+
-- Create the sentiment analysis function
73
+
CREATE OR REPLACEFUNCTIONsentiment_analysis(STRING) RETURNS STRING
74
+
LANGUAGE python HANDLER ='sentiment_analysis'
75
+
AS $$
76
+
def remove_stop_words(text, stop_words):
77
+
"""
78
+
Removes common stop words from the text.
79
+
80
+
Args:
81
+
text (str): The input text.
82
+
stop_words (set): A set of stop words to remove.
83
+
84
+
Returns:
85
+
str: Text with stop words removed.
86
+
"""
87
+
return ''.join([word for word intext.split() if word.lower() not in stop_words])
| The movie was excellent and everyone loved it | Sentiment Score: 1; Sentiment Label: Positive |
173
+
| A terrible experience that made me feel bad | Sentiment Score: -2; Sentiment Label: Negative |
174
+
```
175
+
52
176
### JavaScript
53
177
54
-
A JavaScript UDF allows you to invoke JavaScript code from a SQL query via Databend's built-in handler, enabling seamless integration of JavaScript logic within your SQL queries.
178
+
A JavaScript UDF allows you to invoke JavaScript code from a SQL query via Databend's built-in handler, enabling seamless integration of JavaScript logic within your SQL queries.
0 commit comments