{"id":20199798,"url":"https://github.com/milenkovicm/wasaffi","last_synced_at":"2025-05-07T06:31:03.307Z","repository":{"id":231674776,"uuid":"781700123","full_name":"milenkovicm/wasaffi","owner":"milenkovicm","description":"Datafusion WASM User Defined Functions","archived":false,"fork":false,"pushed_at":"2024-05-14T15:24:43.000Z","size":1273,"stargazers_count":6,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-05-15T01:32:41.039Z","etag":null,"topics":["datafusion","sql","userdefined-functions","wasm","wasm-bindgen","wasmedge"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/milenkovicm.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-04-03T21:50:55.000Z","updated_at":"2024-05-14T15:24:47.000Z","dependencies_parsed_at":"2024-04-11T07:29:45.154Z","dependency_job_id":null,"html_url":"https://github.com/milenkovicm/wasaffi","commit_stats":null,"previous_names":["milenkovicm/wasaffi"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/milenkovicm%2Fwasaffi","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/milenkovicm%2Fwasaffi/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/milenkovicm%2Fwasaffi/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/milenkovicm%2Fwasaffi/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/milenkovicm","download_url":"https://codeload.github.com/milenkovicm/wasaffi/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224568055,"owners_count":17332833,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["datafusion","sql","userdefined-functions","wasm","wasm-bindgen","wasmedge"],"created_at":"2024-11-14T04:38:55.910Z","updated_at":"2024-11-14T04:38:56.654Z","avatar_url":"https://github.com/milenkovicm.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Datafusion WASM User Defined Functions\n\nVery simplistic datafusion user defined functions written in WASM.\n\nPOC has been built on top of [Wasmedge](https://wasmedge.org) library.\nIt is not terribly performant, with lot of coping and serializing data.\n\nIt has been implemented to demonstrate DataFusion `FunctionFactory` functionality ([arrow-datafusion/pull#9333](https://github.com/apache/arrow-datafusion/pull/9333)) \u0026 `WASM UDF` ([arrow-datafusion/pull#9326](https://github.com/apache/arrow-datafusion/issues/9326)).\n\nOther project in `FunctionFactory` series:\n\n- [Torchfusion, Opinionated Torch Inference on DataFusion](https://github.com/milenkovicm/torchfusion)\n- [LightGBM Inference on DataFusion](https://github.com/milenkovicm/lightfusion)\n- [Apache Datafusion JVM User Defined Functions (UDF), integration nobody asked for 😀](https://github.com/milenkovicm/adhesive)\n\n\u003e [!NOTE]\n\u003e\n\u003e - It has not been envisaged as a actively maintained library.\n\u003e\n\n## Installation\n\nIn order to be able to compile project WasmEdge library [should be installed](https://wasmedge.org/docs/start/install).\n\nor using brew:\n\n```bash\nbrew install wasmedge\n```\n\nWhy [WasmEdge](https://wasmedge.org)? It provided good enough examples to pick up wasm/rust integration.\n\n## Define Function\n\nDefine a rust function ([wasm_function](wasm_function/)) like:\n\n```rust\n// expose function f1 as external function\n// add required bindgen, and required serialization/deserialization\nwasm_udf::export_udf_function!(f1);\n\n/// standard datafusion udf ... kind of \n/// should return ArrayRef or ArrowError (or any error implementing to_string)\nfn f1(args: \u0026[ArrayRef]) -\u003e Result\u003cArrayRef,ArrowError\u003e {\n    let base = args[0]\n        .as_any()\n        .downcast_ref::\u003cFloat64Array\u003e()\n        .expect(\"cast 0 failed\");\n    let exponent = args[1]\n        .as_any()\n        .downcast_ref::\u003cFloat64Array\u003e()\n        .expect(\"cast 1 failed\");\n\n    let array = base\n        .iter()\n        .zip(exponent.iter())\n        .map(|(base, exponent)| match (base, exponent) {\n            (Some(base), Some(exponent)) =\u003e Some(base.powf(exponent)),\n            _ =\u003e None,\n        })\n        .collect::\u003cFloat64Array\u003e();\n\n    Ok(Arc::new(array))\n}\n```\n\nwhich will be compiled to a `wasm` module with:\n\n```bash\ncd wasm_function\ncargo build\n```\n\nAn artifact should be available at `target/wasm32-unknown-unknown/debug/wasm_function.wasm`.\n\n`export_udf_function!` macro should add WasmEdge bindings and peace of code which would do Arrow IPC serialization/deserialization. Arrow arrays are effectively copied across rust/wasm boundary using Arrow Ipc.\n\nThis code currently handles happy path scenario, with some exceptional cases covered.\n\n## UDF Declaration\n\n```rust\nlet sql = r#\"\nCREATE FUNCTION f1(DOUBLE, DOUBLE)\nRETURNS DOUBLE\nLANGUAGE WASM\nAS 'wasm_function.wasm!f1'\n\"#;\n\nctx.sql(sql).await?.show().await?;\n\nctx.sql(\"select a, b, f1(a,b) from t\").await?.show().await?;\n```\n\n[full example](wasmedge_factory/examples/wasaffi.rs)\n\nshould produce something similar to:\n\n```text\n+-----+-----+-------------------+\n| a   | b   | f1(t.a,t.b)       |\n+-----+-----+-------------------+\n| 2.0 | 2.0 | 4.0               |\n| 3.0 | 3.0 | 27.0              |\n| 4.0 | 4.0 | 256.0             |\n| 5.0 | 5.1 | 3670.684197150057 |\n+-----+-----+-------------------+\n```\n\nFunction is declared in format `wasm_function.wasm!f1`, where `wasm_function.wasm` represents module to load and `f1` a function to call.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmilenkovicm%2Fwasaffi","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmilenkovicm%2Fwasaffi","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmilenkovicm%2Fwasaffi/lists"}