DEVELOPER

Architecture

How Teide is structured: the C17 core, SQL pipeline, execution model, and memory system.

Overview

Teide is a layered system:

Project Structure

teide-rs/
  src/
    lib.rs            -- Library root
    ffi.rs            -- Raw C FFI bindings
    engine.rs         -- Safe RAII wrappers (Context, Table, Graph, Column)
    sql/
      mod.rs          -- Session, ExecResult
      planner.rs      -- Query planning (127KB)
      expr.rs         -- Expression planning (59KB)
    cli/              -- REPL binary
    server/           -- PgWire server binary
  build.rs            -- C core vendoring & compilation

SQL Pipeline

SQL text
  |
  v
+---------+     +----------+     +----------+     +---------+
|  Parse  | --> |   Plan   | --> | Optimize | --> | Execute |
|(sqlparser)    |(planner.rs)    |(C engine) |     |(C engine)|
+---------+     +----------+     +----------+     +---------+
  1. Parse: SQL text is parsed using the sqlparser crate with DuckDB dialect
  2. Plan: The Rust planner walks the AST and builds a Teide operation graph (DAG)
  3. Optimize: The C engine fuses element-wise operations and optimizes the DAG
  4. Execute: The C engine processes data in parallel morsels

C17 Core

The core engine is written in C17 with zero external dependencies:

Operation Graph

Computations are expressed as a directed acyclic graph (DAG) of operations:

The optimizer fuses adjacent element-wise operations to minimize memory traffic and maximize cache utilization.

Morsel-Based Execution

Data is processed in morsels of 1024 elements:

This approach provides excellent cache locality (morsels fit in L1/L2 cache) while scaling to many cores.

Memory Model

The .mem REPL command shows detailed allocation statistics including arena usage, direct allocations, slab hit rates, and peak memory.

Storage Formats

Format Access Use Case
CSV Full copy Import/export, ad-hoc analysis
Splayed Zero-copy mmap Single tables, fast column access
Partitioned Zero-copy mmap Time-series data with date partitions

Build Process

The build script (build.rs) handles C core integration:

  1. Checks for local vendor/teide/ directory
  2. Falls back to git clone --depth=1 for crates.io installs
  3. Compiles all C source with C17 standard, O3 optimization
  4. Links with libm and pthread on Linux/macOS
  5. Embeds git commit hash for version display