Skip to content

Implement AssemblyScript WASM UDF ABI - #104606

Merged
vdimir merged 13 commits into
masterfrom
vdimir/wasm-asemblyscipt-abi
May 20, 2026
Merged

vdimir merged 13 commits into
masterfrom
vdimir/wasm-asemblyscipt-abi

Conversation

@vdimir

@vdimir vdimir commented May 11, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Experimental Feature

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

  • Added support for the AssemblyScript ABI for WebAssembly UDFs. You can try the AssemblyScript ABI without installing the toolchain locally by using the browser-based playground, which compiles AssemblyScript code and loads the resulting WebAssembly module into local ClickHouse.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

@vdimir
vdimir force-pushed the vdimir/wasm-asemblyscipt-abi branch from 5cf3392 to 14d5083 Compare May 11, 2026 16:08
@clickhouse-gh

clickhouse-gh Bot commented May 11, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [2537e89]

Summary: ✅


AI Review

Summary

This PR adds the ABI ASSEMBLYSCRIPT path for WASM UDFs, including per-row string marshaling through AssemblyScript runtime exports, runtime-side env.abort handling, and start-function cancellation plumbing in both runtimes. The core ABI implementation and cancellation fix look directionally correct, but two user-visible issues are still open in current head.

Findings

⚠️ Majors

  • [src/Interpreters/WebAssembly/HostApi.cpp:95] decodeAssemblyScriptString still replaces every non-ASCII UTF-16 code unit with ?, so env.abort diagnostics are lossy for Unicode messages/paths. This breaks the documented behavior that abort details are decoded and surfaced in the exception text.
    Suggested fix: reuse full UTF-16LE→UTF-8 decoding (equivalent to appendUTF16LEAsUTF8) instead of ASCII-only fallback.

💡 Nits

  • [docs/en/sql-reference/functions/wasm_udf.md:319] The ABI ASSEMBLYSCRIPT supported-type list still omits Int128 and UInt128, while implementation/tests now support them through v128 (as_add128). Documentation and behavior are out of sync.
    Suggested fix: add Int128/UInt128 to the numeric list.
Final Verdict

Status: ⚠️ Request changes

Minimum required actions:

  1. Preserve Unicode in env.abort message/file decoding (UTF-16LE → UTF-8) instead of replacing non-ASCII with ?.
  2. Update ABI ASSEMBLYSCRIPT docs to include Int128 and UInt128 in supported numeric types.

@clickhouse-gh clickhouse-gh Bot added the pr-experimental Experimental Feature label May 11, 2026
@antonio2368 antonio2368 self-assigned this May 12, 2026
@vdimir
vdimir marked this pull request as draft May 12, 2026 10:08
@vdimir
vdimir force-pushed the vdimir/wasm-asemblyscipt-abi branch from 14d5083 to 983342c Compare May 12, 2026 13:24

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You do a lot of memcpy here while ignoring endianess, I assume we need to cover all those places with proper check and std::byteswap? Maybe a helper functions would help here.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Added functions and use it for all other ABIs as well

T loadFromWasmMemory(const uint8_t * src)
{
T value;
std::memcpy(&value, src, sizeof(T));
if constexpr (std::endian::native == std::endian::big)
value = std::byteswap(value);
return value;
}
template <typename T>
requires std::is_integral_v<T>
void storeToWasmMemory(uint8_t * dst, T value)
{
if constexpr (std::endian::native == std::endian::big)
value = std::byteswap(value);
std::memcpy(dst, &value, sizeof(T));
}

case WasmValKind::I32: return compartment->invoke<uint32_t>(function_name, wasm_args, stop_token);
case WasmValKind::I64: return compartment->invoke<int64_t>(function_name, wasm_args, stop_token);
case WasmValKind::F32: return compartment->invoke<float>(function_name, wasm_args, stop_token);
case WasmValKind::F64: return compartment->invoke<double>(function_name, wasm_args, stop_token);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Do we want to add support for V128 in case it gets supported in the future?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Added v128 arguments support

/// runtime init) runs as part of `Linker::instantiate` below — set a very large relative
/// deadline now so module instantiation can complete. Per-call cancellation is still
/// handled through the `epoch_deadline_callback` configured in `WasmTimeCompartment`.
store.context().set_epoch_deadline(std::numeric_limits<uint64_t>::max());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

seems like this can allow users to initiate unkillable query if they have an infinite loop on module level.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

That is good catch. It's not assemblyscript specific actually, since start function can appear in any wasm module. I tried to solve it passing stop_token to instantiate and added a testcase https://github.com/ClickHouse/ClickHouse/blob/ed4c48543a19eeed48c9de2588b6ea4683b5c820/tests/queries/0_stateless/wasm/as_infinite_start.ts

@vdimir
vdimir marked this pull request as ready for review May 18, 2026 07:46
@vdimir

vdimir commented May 20, 2026

Copy link
Copy Markdown
Member Author

Stateless tests (amd_llvm_coverage, ParallelReplicas, s3 storage, parallel) FAIL
01710_projection_additional_filters

#104296


**Supported types**:

- Numeric: `Int8`/`UInt8`, `Int16`/`UInt16` (widened to `i32` at the boundary), `Int32`/`UInt32`, `Int64`/`UInt64`, `Float32`, `Float64`

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This supported-types list omits Int128 / UInt128, but the implementation and the new as_add128 test support them via v128. Please add them here so the ABI ASSEMBLYSCRIPT docs match the actual behavior.

for (WasmSizeT i = 0; i + 1 < data.size(); i += 2)
{
uint32_t code_unit = static_cast<uint32_t>(data[i]) | (static_cast<uint32_t>(data[i + 1]) << 8);
/// Naive transcoding: emit ASCII as-is, replace anything else with '?'.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

decodeAssemblyScriptString currently maps every non-ASCII UTF-16 code unit to '?', so env.abort messages / file names with Unicode become lossy (for example Привет becomes ??????). Since this PR documents that abort surfaces the decoded message and location, can we use real UTF-16LE -> UTF-8 decoding here (similar to appendUTF16LEAsUTF8) instead of ASCII-only fallback?

@clickhouse-gh

clickhouse-gh Bot commented May 20, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.10% 84.10% +0.00%
Functions 91.40% 91.40% +0.00%
Branches 76.50% 76.60% +0.10%

Changed lines: 75.26% (295/392) · Uncovered code

Full report · Diff report

@vdimir
vdimir added this pull request to the merge queue May 20, 2026
Merged via the queue into master with commit 669fd55 May 20, 2026
327 of 328 checks passed
@vdimir
vdimir deleted the vdimir/wasm-asemblyscipt-abi branch May 20, 2026 15:40
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label May 20, 2026
DavidHe-2008 pushed a commit to DavidHe-2008/ClickHouse that referenced this pull request Jun 1, 2026
…blyscipt-abi

Implement AssemblyScript WASM UDF ABI
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-experimental Experimental Feature pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants