Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

ClickHouse 13 min read Case study mediumAdept

How to use Rust in ClickHouse: avoiding a full rewrite

ClickHouse, a 1.5M-line C++ analytic database, is adopting Rust incrementally rather than rewriting. It started with Corrosion to wire Rust into CMake and a BLAKE3 hash function, then took on a client history-navigation feature, the PRQL query language, and Databricks' delta-kernel-rs for Delta Lake support. The integration problems were mostly practical: hermetic reproducible builds with vendored crates, buggy FFI wrappers caught quickly by existing CI and fuzzing, library panics that crash the server, compiling Rust with sanitizers (now easier with an MSan toolchain), duplicated cross-compilation setup, and transitive dependencies linking system OpenSSL.

Related posts

  • A Year of Rust in ClickHouse

    ClickHouse mediumAdept

    ClickHouse added Rust to its C++ codebase as an option for new components rather than a rewrite, starting with BLAKE3, then skim for clickhouse-client history, PRQL as a switchable query dialect, and Databricks' delta-kernel-rs replacing a sketchy C++ Delta Lake implementation. The integration needed hermetic builds with vendored crates and careful C++/Rust wrappers. Panics had to be kept from crashing a multi-tenant server, and MSan required instrumenting Rust with a pinned nightly toolchain. Further friction came from cross-compilation, static OpenSSL linking, enormous Rust symbol names, library conventions for memory accounting and connection pools that don't compose with ClickHouse's, and missing build caching, while CI fuzzing caught crashes in the Rust libraries just as it does in C libraries.

  • More Than 2x Faster Hashing in ClickHouse Using Rust

    ClickHouse mediumAdept

    ClickHouse added optional Rust library support to its C++/CMake build using Corrosion-rs, with BLAKE3 as the first library, exposed to C++ through a Rust shim function using C-compatible FFI types and a header file. BLAKE3 hashes more than 2x faster than SHA224/SHA256 and slightly faster than MD5, while staying secure and resistant to length extension. Perf flamegraphs show the shim's type conversion costs about 1.15% of query time. Integration hurdles included MemorySanitizer false positives on Rust memory, fixed with __msan_unpoison and explicit byte conversion, plus cross-platform linking, with aarch64-darwin still unsupported. The integration also had to stay optional, statically linked, cross-compilable and compatible with sanitizers.

  • Integrating the Rust Delta Kernel into ClickHouse

    ClickHouse hardMaster

    ClickHouse replaced its hand-written Delta Lake protocol implementation with the Rust Delta Kernel (delta-kernel-rs). The kernel takes over transaction log parsing, snapshot resolution and data skipping, while ClickHouse keeps its own optimized Parquet reader through the kernel's Engine APIs. Adopting it brought writes, schema evolution via logical/physical schema mapping, time travel, Change Data Feed (exposed in 25.12 through snapshot version settings), and partition and statistics pruning. Embedding a Rust library in ClickHouse's dependency-free C++ binary required nightly Rust for sanitizer builds, crates vendored with cargo-local-registry to avoid network fetch failures, and building the kernel as its own workspace.

  • chDB - A Rocket Engine on a Bicycle

    ClickHouse hardMaster

    chDB embeds the ClickHouse engine as an in-process Python module instead of shelling out to the 500MB clickhouse-local binary, using WriteBufferFromVector, Python memoryview and Pybind11 for zero-copy result retrieval. Getting jemalloc to work inside the shared library meant redirecting malloc with the linker flag -Wl,-wrap,malloc. That exposed crashes when glibc functions like getcwd returned glibc-malloc'd memory that was then freed by je_free, so the author patched jemalloc (PR #2424) to make arenas.lookup safe for checking ownership. The result cut memory usage by 50%, and chDB ranks as the fastest stateless/serverless engine on ClickBench. Later releases added SQL over Pandas DataFrames, query stats and Python UDFs.

  • Fuzzing ClickHouse

    ClickHouse mediumAdept

    ClickHouse's AST-based query fuzzer takes SQL queries from the regression test suite, parses them, and randomly mutates the AST. It swaps in bad numeric values such as NaN and integer limits, perturbs strings and arrays, and splices function arguments, expressions and tables from other test queries. The core is about 700 lines of C++ and has found more than 200 bugs, including segfaults and logic errors. Much of that comes from pairing it with assertions, a distinct LOGICAL_ERROR exception for internal bugs, and clang sanitizers. In CI it weights permutations toward new tests in pull requests and needs settings constraints, since it once disabled its own execution-time limit.

The quest for software engineering knowledge.

Look:
Layout: