@@ -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
228300Databend provides several commands to help you manage your UDFs:
0 commit comments