llkv/
lib.rs

1//! LLKV: Arrow-Native SQL over Key-Value Storage
2//!
3//! This crate serves as the primary entrypoint for the LLKV database toolkit.
4//! It re-exports the high-level SQL engine and storage abstractions from the
5//! underlying `llkv-*` crates so downstream applications see a single surface
6//! for planning, execution, and storage.
7//!
8//! # Why LLKV Exists
9//!
10//! LLKV explores what an [Apache Arrow](https://arrow.apache.org/)-first SQL stack can look like when layered
11//! on top of key-value pagers instead of a purpose-built storage format. The
12//! project targets columnar OLAP workloads while insisting on compatibility with
13//! the reference SQLite [`sqllogictest`](https://sqlite.org/sqllogictest/doc/trunk/about.wiki) suite. Today every published SQLite test
14//! runs unmodified against LLKV, giving the engine a broad SQL regression net as
15//! new features land.
16//!
17//! LLKV also ingests a growing set of DuckDB `sqllogictest` cases. Those tests help
18//! keep transaction semantics honest as dual-dialect support takes shape, but the
19//! DuckDB integration is early and still expanding.
20//!
21//! # Story So Far
22//!
23//! LLKV is an experimental SQL database that layers Arrow columnar storage, a streaming execution
24//! engine, and MVCC transaction management on top of key-value pagers. Every crate in this workspace
25//! serves that goal, keeping [`arrow::record_batch::RecordBatch`] as the interchange format from
26//! storage through execution.
27//!
28//! The surface begins with [llkv-sql](https://docs.rs/llkv-sql/latest/llkv_sql/), which parses statements via
29//! [`sqlparser`](https://docs.rs/sqlparser) and lowers them into execution plans. Those plans feed
30//! into [llkv-runtime](https://docs.rs/llkv-runtime/latest/llkv_runtime/), the orchestration layer that injects MVCC
31//! metadata, coordinates transactions, and dispatches work across the execution and storage stacks.
32//! Query evaluation lives in [llkv-executor](https://docs.rs/llkv-executor/latest/llkv_executor/), which streams Arrow
33//! `RecordBatch` results without owning MVCC state, while [llkv-table](https://docs.rs/llkv-table/latest/llkv_table/)
34//! enforces schema rules and logical field tracking on top of the column store.
35//!
36//! At the storage layer, [llkv-column-map](https://docs.rs/llkv-column-map/latest/llkv_column_map/) persists column chunks as
37//! Arrow-serialized blobs keyed by pager-managed physical IDs. That layout lets backends such as
38//! [`simd-r-drive`] provide zero-copy buffers, and it keeps higher layers working against Arrow data
39//! structures end-to-end. Concurrency remains synchronous by default, leaning on [Rayon] and
40//! [Crossbeam] while still embedding inside [Tokio] when async orchestration is required.
41//!
42//! Compatibility is measured continuously. [`llkv_slt_tester`](https://docs.rs/llkv-slt-tester/latest/llkv_slt_tester/)
43//! executes SQLite and DuckDB `sqllogictest` suites via the
44//! [`sqllogictest` crate](https://crates.io/crates/sqllogictest), CI spans Linux, macOS, and Windows,
45//! and the
46//! documentation here stays synchronized with the rest of the repository so the rustdoc narrative
47//! matches the public design record.
48//!
49//! # Crate Topology
50//!
51//! LLKV ships as a layered Cargo workspace where higher crates depend on the ones below while sharing
52//! Arrow as their interchange format. The main strata are:
53//!
54//! - **SQL Interface**: [`llkv-sql`](https://docs.rs/llkv-sql/latest/llkv_sql/) exposes the SQL entry point and delegates
55//!   planning and execution.
56//! - **Query Planning**: [`llkv-plan`](https://docs.rs/llkv-plan/latest/llkv_plan/) and [`llkv-expr`](https://docs.rs/llkv-expr/latest/llkv_expr/)
57//!   define logical plans and expression ASTs.
58//! - **Runtime & Transactions**: [`llkv-runtime`](https://docs.rs/llkv-runtime/latest/llkv_runtime/) orchestrates sessions and
59//!   MVCC, while [`llkv-transaction`](https://docs.rs/llkv-transaction/latest/llkv_transaction/) manages transaction IDs and
60//!   snapshot isolation.
61//! - **Query Execution**: [`llkv-executor`](https://docs.rs/llkv-executor/latest/llkv_executor/) streams Arrow `RecordBatch`
62//!   results with help from [`llkv-aggregate`](https://docs.rs/llkv-aggregate/latest/llkv_aggregate/) and
63//!   [`llkv-join`](https://docs.rs/llkv-join/latest/llkv_join/).
64//! - **Table & Metadata**: [`llkv-table`](https://docs.rs/llkv-table/latest/llkv_table/) enforces schemas and catalogs atop
65//!   [`llkv-column-map`](https://docs.rs/llkv-column-map/latest/llkv_column_map/), the columnar storage engine.
66//! - **Storage & I/O**: [`llkv-storage`](https://docs.rs/llkv-storage/latest/llkv_storage/) provides the pager abstraction and
67//!   integrates with [`simd-r-drive`](https://crates.io/crates/simd-r-drive) backends.
68//! - **Supporting crates**: [`llkv-result`](https://docs.rs/llkv-result/latest/llkv_result/) unifies error handling,
69//!   [`llkv-csv`](https://docs.rs/llkv-csv/latest/llkv_csv/) handles CSV ingestion, [`llkv-test-utils`](https://docs.rs/llkv-test-utils/latest/llkv_test_utils/)
70//!   supplies testing helpers, and [`llkv-slt-tester`](https://docs.rs/llkv-slt-tester/latest/llkv_slt_tester/) drives SQL logic tests.
71//!
72//! # MVCC Snapshot Isolation
73//!
74//! LLKV tracks visibility with MVCC metadata injected into every table: hidden `row_id`,
75//! `created_by`, and `deleted_by` columns are managed by the runtime and storage layers. Transactions
76//! obtain 64-bit IDs from the [`llkv_transaction`](https://docs.rs/llkv-transaction/latest/llkv_transaction/) stack, capture a
77//! snapshot of the last committed transaction, and tag new or modified rows accordingly. Rows are
78//! visible when `created_by` is at or below the snapshot watermark and `deleted_by` is absent or
79//! greater than that watermark. `UPDATE` and `DELETE` use soft deletes (`deleted_by = txn_id`), so
80//! old versions remain until future compaction work lands, and auto-commit statements reuse a fast
81//! path that tags rows with the reserved auto-commit ID.
82//!
83//! Each transaction operates with both a base context (existing tables) and a staging context (new
84//! tables created during the transaction). On commit, staged operations replay into the base pager
85//! once the transaction watermark advances, preserving snapshot isolation without copying entire
86//! tables during the unit of work.
87//!
88//! # Roadmap Signals
89//!
90//! Active work centers on extending the transaction lifecycle (the
91//! [`TxnIdManager`](https://docs.rs/llkv-transaction/latest/llkv_transaction/mvcc/struct.TxnIdManager.html) still carries TODOs for next-ID
92//! management), expanding the constraint system across primary, unique, foreign-key, and check
93//! metadata, and tightening performance around Arrow batch sizing and columnar access patterns. The
94//! crates in this workspace continue to evolve together, keeping documentation and implementation in
95//! lockstep.
96//!
97//! # Dialect and Tooling Outlook
98//!
99//! - **SQLite compatibility**: LLKV parses SQLite-flavored SQL, batches `INSERT`
100//!   statements for throughput, and surfaces results in Arrow form. Passing the
101//!   upstream `sqllogictest` suites establishes a baseline but does not yet make
102//!   LLKV a drop-in SQLite replacement.
103//! - **DuckDB coverage**: Early DuckDB suites exercise MVCC and typed
104//!   transaction flows. They chart the roadmap rather than guarantee full DuckDB
105//!   parity today.
106//! - **Tokio-friendly, synchronous core**: Queries execute synchronously by
107//!   default, delegating concurrency to [Rayon] and [Crossbeam]. Embedders can still
108//!   tuck the engine inside [Tokio], which is how the SQL Logic Test runner drives
109//!   concurrent sessions.
110//!
111//! See [dev-docs/high-level-crate-linkage.md](../dev-docs/high-level-crate-linkage.md)
112//! and the [DeepWiki architecture overview](https://deepwiki.com/jzombie/rust-llkv)
113//! for diagrams and extended commentary.
114//!
115//! # Quick Start
116//!
117//! Create an in-memory SQL engine and execute queries:
118//!
119//! ```rust
120//! use std::sync::Arc;
121//! use llkv::{SqlEngine, MemPager};
122//!
123//! let engine = SqlEngine::new(Arc::new(MemPager::default()));
124//! let results = engine.execute("SELECT 42 AS answer").unwrap();
125//! ```
126//!
127//! # Architecture
128//!
129//! LLKV is organized as a layered workspace:
130//!
131//! - **SQL Interface** ([llkv-sql](https://docs.rs/llkv-sql/latest/llkv_sql/)): Parses and executes SQL statements.
132//! - **Query Planning** ([llkv-plan](https://docs.rs/llkv-plan/latest/llkv_plan/), [llkv-expr](https://docs.rs/llkv-expr/latest/llkv_expr/)): Defines logical plans and expression ASTs.
133//! - **Runtime** ([llkv-runtime](https://docs.rs/llkv-runtime/latest/llkv_runtime/), [llkv-transaction](https://docs.rs/llkv-transaction/latest/llkv_transaction/)): Coordinates MVCC transactions and statement execution.
134//! - **Execution** ([llkv-executor](https://docs.rs/llkv-executor/latest/llkv_executor/), [llkv-aggregate](https://docs.rs/llkv-aggregate/latest/llkv_aggregate/), [llkv-join](https://docs.rs/llkv-join/latest/llkv_join/)): Streams Arrow batches through operators.
135//! - **Storage** ([llkv-table](https://docs.rs/llkv-table/latest/llkv_table/), [llkv-column-map](https://docs.rs/llkv-column-map/latest/llkv_column_map/), [llkv-storage](https://docs.rs/llkv-storage/latest/llkv_storage/)): Manages columnar storage and pager abstractions.
136//!
137//! # Re-exports
138//!
139//! This crate re-exports the following modules for convenient access:
140//!
141//! - [`SqlEngine`]: The main SQL execution engine.
142//! - [`storage`]: Pager abstractions and implementations.
143//!
144//! [Rayon]: https://docs.rs/rayon
145//! [Crossbeam]: https://docs.rs/crossbeam
146//! [Tokio]: https://docs.rs/tokio
147//! [`simd-r-drive`]: https://crates.io/crates/simd-r-drive
148
149// Re-export the SQL engine as the primary user-facing API
150pub use llkv_sql::SqlEngine;
151
152// Re-export storage pager abstractions
153pub mod storage {
154    //! Storage layer abstractions and pager implementations.
155    //!
156    //! This module provides the `Pager` trait and concrete implementations
157    //! for both in-memory and persistent storage backends.
158
159    pub use llkv_storage::pager::{MemPager, Pager};
160
161    // SimdRDrivePager is only available when llkv-storage is built with simd-r-drive-support
162    #[cfg(feature = "simd-r-drive-support")]
163    pub use llkv_storage::pager::SimdRDrivePager;
164}
165
166// Re-export result types for error handling
167pub use llkv_result::{Error, Result};
168
169// Re-export runtime types that users might need when working with statement results
170pub use llkv_runtime::RuntimeStatementResult;