Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rust UDF error #46

Closed
ravi-k4 opened this issue Oct 14, 2022 · 5 comments
Closed

Rust UDF error #46

ravi-k4 opened this issue Oct 14, 2022 · 5 comments

Comments

@ravi-k4
Copy link

ravi-k4 commented Oct 14, 2022

I have built a Rust UDF and added the function to Singlestore database using this command

CREATE FUNCTION <function name> AS WASM FROM HTTP 'https://<>.wasm' with WIT from HTTP 'https://<>.wit'

when I ran select function(1,2,3) AS Result observing this error

ERROR 2654 UNKNOWN_ERR_CODE: Wasm module instantiation failed with error. unknown import: env::__extenddftf2 has not been defined

Can someone help me fix this issue?

@pvetere
Copy link
Member

pvetere commented Oct 14, 2022

Hi. That error means that the wasm module is expecting the host (the database, in this case) to provide it with an implementation of that function. SingleStore doesn't current supply any imports except what's included in WASI. A couple of questions for you:

  1. When you compiled, did you use the version of Rust included in this dev container? Could you paste the compile command you used?

  2. What 3rd party libraries are you including in your module? Would it be possible for you to attach your Cargo.toml file here?

@ravi-k4
Copy link
Author

ravi-k4 commented Oct 14, 2022

Yes, I have used the dev container.

[dev-shell]:~/src % cargo wasi build --lib --release Compiling version_check v0.9.4 Compiling memchr v2.5.0 Compiling libc v0.2.135 Compiling autocfg v1.1.0 Compiling libm v0.2.5 Compiling semver v1.0.14 Compiling proc-macro2 v1.0.46 Compiling quote v1.0.21 Compiling unicode-ident v1.0.5 Compiling tinyvec_macros v0.1.0 Compiling anyhow v1.0.65 Compiling syn v1.0.102 Compiling pulldown-cmark v0.8.0 Compiling cfg-if v1.0.0 Compiling bitflags v1.3.2 Compiling byteorder v1.4.3 Compiling id-arena v2.2.1 Compiling unicode-xid v0.2.4 Compiling stable_deref_trait v1.2.0 Compiling unicode-segmentation v1.10.0 Compiling regex-syntax v0.6.27 Compiling smallvec v1.10.0 Compiling wasi v0.11.0+wasi-snapshot-preview1 Compiling wit-bindgen-gen-rust-wasm v0.1.0 (https://github.com/bytecodealliance/wit-bindgen.git?rev=60e3c5b41e616fee239304d92128e117dd9be0a7#60e3c5b4) Compiling glob v0.3.0 Compiling pkg-config v0.3.25 Compiling log v0.4.17 Compiling once_cell v1.15.0 Compiling async-trait v0.1.57 Compiling thiserror v1.0.37 Compiling lazy_static v1.4.0 Compiling robust v0.2.3 Compiling unicase v2.6.0 Compiling ahash v0.7.6 Compiling ahash v0.8.0 Compiling num-traits v0.2.15 Compiling tinyvec v1.6.0 Compiling hash32 v0.2.1 Compiling getrandom v0.2.7 Compiling heck v0.3.3 Compiling geographiclib-rs v0.2.1 Compiling unicode-normalization v0.1.22 Compiling jobserver v0.1.25 Compiling aho-corasick v0.7.19 Compiling rustc_version v0.4.0 Compiling hashbrown v0.12.3 Compiling cc v1.0.73 Compiling heapless v0.7.16 Compiling regex v1.6.0 Compiling approx v0.5.1 Compiling float_next_after v0.1.5 Compiling float-cmp v0.6.0 Compiling wit-parser v0.1.0 (https://github.com/bytecodealliance/wit-bindgen.git?rev=60e3c5b41e616fee239304d92128e117dd9be0a7#60e3c5b4) Compiling libh3-sys v0.1.3 Compiling rstar v0.9.3 Compiling wit-bindgen-gen-core v0.1.0 (https://github.com/bytecodealliance/wit-bindgen.git?rev=60e3c5b41e616fee239304d92128e117dd9be0a7#60e3c5b4) Compiling geo-types v0.7.7 Compiling libh3 v0.1.8 Compiling wit-bindgen-gen-rust v0.1.0 (https://github.com/bytecodealliance/wit-bindgen.git?rev=60e3c5b41e616fee239304d92128e117dd9be0a7#60e3c5b4) Compiling h3ron-h3-sys v0.15.1 Compiling geo v0.22.1 Compiling thiserror-impl v1.0.37 Compiling wit-bindgen-rust-impl v0.1.0 (https://github.com/bytecodealliance/wit-bindgen.git?rev=60e3c5b41e616fee239304d92128e117dd9be0a7#60e3c5b4) Compiling wit-bindgen-rust v0.1.0 (https://github.com/bytecodealliance/wit-bindgen.git?rev=60e3c5b41e616fee239304d92128e117dd9be0a7#60e3c5b4) Compiling h3ron v0.15.1 Compiling h3index v0.1.0 (/home/ravi/src) Finished release [optimized] target(s) in 19.08s Optimizing with wasm-opt

I am trying to build udf to convert lat and lon to h3index. The code I have tested with is available here https://github.com/K4Mobility/singlestore-h3index-udf

@pvetere
Copy link
Member

pvetere commented Oct 14, 2022

Thank you for providing this information. I'll investigate and get back to you soon.

@pvetere
Copy link
Member

pvetere commented Oct 14, 2022

In the h3ron project, I found the following comment (from https://github.com/nmandery/h3ron/blob/main/h3ron/examples/compile_to_wasi/Makefile):

# 128 bit floats are not supported by rust. See https://github.com/rust-lang/compiler-builtins .
# This manifests in errors like:                                                 
#                                                                                
# ```                                                                            
# wasmtime ../../../target/wasm32-wasi/debug/compile_to_wasi.wasm                
# Error: failed to run main module `../../../target/wasm32-wasi/debug/compile_to_wasi.wasm`
#                                                                                
# Caused by:                                                                     
#     0: failed to instantiate "../../../target/wasm32-wasi/debug/compile_to_wasi.wasm"
#     1: unknown import: `env::__extenddftf2` has not been defined               
# ```                                                                            
#                                                                                
# To resolve this we link to the runtime library included in the SDK:            
export RUSTFLAGS:=-C target-feature=-crt-static -C link-arg=-L$(WASI_SDK_PATH)/lib/clang/14.0.4/lib/wasi -C link-arg=-lclang_rt.builtins-wasm32

Based on this, in the SingleStore development container, I set the RUSTFLAGS environment variable like this:

export RUSTFLAGS='-C target-feature=-crt-static -C link-arg=-L/opt/wasi-sdk/lib/clang/14.0.4/lib/wasi -C link-arg=-lclang_rt.builtins-wasm32'

Then, I deleted the target directory and rebuilt:

rm -rf target
cargo wasi build --release --lib

This allowed me to successfully run the function in the database:

MySQL [pete]> select h3index(3.4, 4.5, 4);
+----------------------+
| h3index(3.4, 4.5, 4) |
+----------------------+
|   596033906113773567 |
+----------------------+
1 row in set (0.119 sec)

@ravi-k4
Copy link
Author

ravi-k4 commented Oct 14, 2022

Nice. It works. Thank you very much!

@pvetere pvetere closed this as completed Oct 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants