Skip to content

Commit 3049835

Browse files
authored
feat(query): add bool_and bool_or and udf wasm docs (#2431)
1 parent 9b96d2f commit 3049835

File tree

3 files changed

+178
-5
lines changed

3 files changed

+178
-5
lines changed

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

Lines changed: 77 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ User-Defined Functions (UDFs) allow you to create custom operations tailored to
1111
| UDF Type | Description | Languages | Use Case |
1212
|----------|-------------|-----------|----------|
1313
| [Lambda UDFs](#lambda-udfs) | Simple expressions using SQL syntax | SQL | Quick transformations and calculations |
14-
| [Embedded UDFs](#embedded-udfs) | Full programming language support | Python (Enterprise), JavaScript | Complex logic and algorithms |
14+
| [Embedded UDFs](#embedded-udfs) | Full programming language support | Python (Enterprise), JavaScript, WASM | Complex logic and algorithms |
1515

1616
## Lambda UDFs
1717

@@ -45,7 +45,7 @@ CREATE [OR REPLACE] FUNCTION <function_name> AS (<parameter_list>) -> <expressio
4545

4646
```sql
4747
-- Create a Lambda UDF to calculate age in years
48-
CREATE OR REPLACE FUNCTION age AS (dt) ->
48+
CREATE OR REPLACE FUNCTION age AS (dt) ->
4949
date_diff(year, dt, now());
5050

5151
-- Create a table with birthdates
@@ -199,15 +199,15 @@ export function calculateAge(birthDateStr) {
199199
// Parse the date string into a Date object
200200
const birthDate = new Date(birthDateStr);
201201
const today = new Date();
202-
202+
203203
let age = today.getFullYear() - birthDate.getFullYear();
204-
204+
205205
// Adjust age if birthday hasn't occurred yet this year
206206
const monthDiff = today.getMonth() - birthDate.getMonth();
207207
if (monthDiff < 0 || (monthDiff === 0 && today.getDate() < birthDate.getDate())) {
208208
age--;
209209
}
210-
210+
211211
return age;
212212
}
213213
$$;
@@ -223,6 +223,78 @@ SELECT calculate_age_js('1990-05-15') AS age_result;
223223
-- +------------+
224224
```
225225
226+
## WASM UDF
227+
228+
WASM UDFs allow you to use rust to define the functions and build it into wasm module, then load it into Databend.
229+
230+
#### Example: Fibonacci Calculation
231+
232+
1. Create a new project name `arrow-udf-example`
233+
234+
```bash
235+
cargo new arrow-udf-example
236+
```
237+
238+
2. Add the following dependencies to `Cargo.toml`
239+
240+
```toml
241+
[package]
242+
name = "arrow-udf-example"
243+
version = "0.1.0"
244+
245+
[lib]
246+
crate-type = ["cdylib"]
247+
248+
[dependencies]
249+
arrow-udf = "0.8"
250+
```
251+
252+
3. Implement the UDF in `src/lib.rs`
253+
254+
```rust
255+
use arrow_udf::function;
256+
257+
#[function("fib(int) -> int")]
258+
fn fib(n: i32) -> i32 {
259+
let (mut a, mut b) = (0, 1);
260+
for _ in 0..n {
261+
let c = a + b;
262+
a = b;
263+
b = c;
264+
}
265+
a
266+
}
267+
```
268+
269+
4. Build the project
270+
271+
```bash
272+
cargo build --release --target wasm32-wasip1
273+
```
274+
275+
5. Load the wasm module into Databend
276+
277+
```bash
278+
cp /target/wasm32-wasip1/release/arrow_udf_example.wasm /tmp
279+
```
280+
281+
And create stage and put the wasm module into stage via bendsql
282+
```sql
283+
🐳 root@default:) create stage s_udf;
284+
🐳 root@default:) put fs:///tmp/arrow_udf_example.wasm @s_udf/;
285+
286+
🐳 root@default:) CREATE OR REPLACE FUNCTION fib_wasm (INT) RETURNS INT LANGUAGE wasm HANDLER = 'fib' AS $$@s_udf/arrow_udf_example.wasm$$;
287+
288+
289+
🐳 root@default:) select fib_wasm(10::Int32);
290+
╭─────────────────────╮
291+
│ fib_wasm(10::Int32) │
292+
│ Nullable(Int32) │
293+
├─────────────────────┤
294+
│ 55 │
295+
╰─────────────────────╯
296+
```
297+
226298
## Managing UDFs
227299
228300
Databend provides several commands to help you manage your UDFs:
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
---
2+
title: bool_and
3+
---
4+
import FunctionDescription from '@site/src/components/FunctionDescription';
5+
6+
<FunctionDescription description="Introduced or updated: v1.2.756"/>
7+
8+
Returns true if all input values are true, otherwise false.
9+
10+
- NULL values are ignored.
11+
- If all input values are null, the result is null.
12+
- Supports for boolean types
13+
14+
## Syntax
15+
16+
```sql
17+
bool_and(<expr>)
18+
```
19+
20+
## Return Type
21+
22+
Same as the input type.
23+
24+
## Examples
25+
26+
```sql
27+
select bool_and(t) from (values (true), (true), (null)) a(t);
28+
╭───────────────────╮
29+
│ bool_and(t) │
30+
│ Nullable(Boolean) │
31+
├───────────────────┤
32+
│ true │
33+
╰───────────────────╯
34+
35+
select bool_and(t) from (values (true), (true), (true)) a(t);
36+
37+
╭───────────────────╮
38+
│ bool_and(t) │
39+
│ Nullable(Boolean) │
40+
├───────────────────┤
41+
│ true │
42+
╰───────────────────╯
43+
44+
select bool_and(t) from (values (true), (true), (false)) a(t);
45+
╭───────────────────╮
46+
│ bool_and(t) │
47+
│ Nullable(Boolean) │
48+
├───────────────────┤
49+
│ false │
50+
╰───────────────────╯
51+
```
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---
2+
title: bool_or
3+
---
4+
import FunctionDescription from '@site/src/components/FunctionDescription';
5+
6+
<FunctionDescription description="Introduced or updated: v1.2.756"/>
7+
8+
Returns true if at least one input value is true, otherwise false
9+
10+
- NULL values are ignored.
11+
- If all input values are null, the result is null.
12+
- Supports for boolean types
13+
14+
## Syntax
15+
16+
```sql
17+
bool_or(<expr>)
18+
```
19+
20+
## Return Type
21+
22+
Same as the input type.
23+
24+
## Examples
25+
26+
```sql
27+
select bool_or(t) from (values (true), (true), (null)) a(t);
28+
╭───────────────────╮
29+
│ bool_or(t) │
30+
│ Nullable(Boolean) │
31+
├───────────────────┤
32+
│ true │
33+
╰───────────────────╯
34+
35+
select bool_or(t) from (values (true), (true), (false)) a(t);
36+
╭───────────────────╮
37+
│ bool_or(t) │
38+
│ Nullable(Boolean) │
39+
├───────────────────┤
40+
│ true │
41+
╰───────────────────╯
42+
43+
select bool_or(t) from (values (false), (false), (false)) a(t);
44+
╭───────────────────╮
45+
│ bool_or(t) │
46+
│ Nullable(Boolean) │
47+
├───────────────────┤
48+
│ false │
49+
╰───────────────────╯
50+
```

0 commit comments

Comments
 (0)