Skip to content

Add spatial support for Wasm#471

Open
georgestagg wants to merge 11 commits into
mainfrom
wip-wasm-spatialite
Open

Add spatial support for Wasm#471
georgestagg wants to merge 11 commits into
mainfrom
wip-wasm-spatialite

Conversation

@georgestagg

@georgestagg georgestagg commented Jun 11, 2026

Copy link
Copy Markdown
Collaborator

Adds support for dynamically loadable extensions for sqlite under wasm. The ggsql-wasm package gains a new function installExtension(), which asynchronously downloads additional extensions as .wasm binaries distributed alongside our current set of ggsql wasm files. With this, sqlite extensions are downloaded if (and only if) the user requests for them to be installed using this function.

Behind the scenes the .wasm extensions are loaded into the WebAssembly memory space, and we perform a dynamic linking step to hook them up with the sqlite wasm binary that's now been configured with a bunch of exports for linking to extensions (sqlite3_*, various built-in maths functions, additional rust_sqlite_* support functions).

Unfortunately, for this to work we need to fork and modify the sqlite-rs-wasm package to enable loadable extensions. I don't think it will be difficult to maintain a fork, but it is an unfortunate pain point. If it get's too much, we can easily back this out and revert to upstream sqlite-rs-wasm.

I have put the fork here: https://github.com/ggsql-dev/sqlite-wasm-rs. I have not used the posit-dev org for two reasons,

  1. This could potentially go away when we switch to using async duckdb to execute queries in wasm.
  2. This is not a "user-facing" package, more like a dev tool.

To avoid rebuilding the spatialite extension in every CI run, I've setup the forked repo above to build and release it as a .wasm binary, here. The ./build-wasm.sh script that runs the show from this side downloads and caches that binary in the target directory.

Some other tweaks:

  • Switched from a custom bump allocator to a using the Rust global allocator in our wasm sysroot, fixing a stack corruption bug for large allocations. This would have bitten us eventually even without spatial support.

  • Updated the sqlite reader to support our spatial ggsql queries, including a new dialect function sql_make_envelope.

  • Tweaked how parquet files are registered under wasm, so that geometry columns are stored as WKB binary blobs, then converted into Arrow binary columns, as expected.

  • If INSTALL spatial is detected in a ggsql code block in the Quarto docs, our runtime library will interpret that as a request to run the wasm installExtension() function to install the spatialite extension for this page.

  • Some examples added to the playground.

All said and done, with this we get spatial support in Wasm, and interactive docs:

Screen.Recording.2026-06-10.at.14.11.32.mov

@@ -0,0 +1,474 @@
// Wasm Exception Handling proposal types

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file contains our Wasm dynamic linker and implementations of related runtime functions like dlopen(). A whole bunch of low level memory layout fiddling and symbol offset calculations...

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

Successfully merging this pull request may close these issues.

1 participant