LLKV SQL
Work in Progress
llkv-sql exposes SqlEngine, the primary entry point for executing SQL against the LLKV stack. It converts SQL text into typed plans, handles dialect quirks, batches insert workloads, and delegates execution to the runtime layer.
Responsibilities
- Parse SQL statements, translating source text to
sqlparserASTs and then tollkv-planstructures. - Provide user-facing APIs for ad-hoc queries (
sql) and multi-statement execution (execute). - Manage SQL preprocessing so SQLite/DuckDB conveniences map cleanly onto
sqlparserand the planner. - Coordinate with
llkv-runtimefor transaction control, MVCC visibility, and plan execution.
SqlEngine Highlights
SqlEngine::newwires a pager-backed runtime;SqlEngine::with_contextreuses an existing runtime session when embedding the engine.execute(&self, sql: &str)accepts batches of statements, runs the preprocessing pipeline, parses each statement, manages theInsertBuffer, and returnsVec<SqlStatementResult>.sql(&self, sql: &str)enforces singleSELECTsemantics and yieldsVec<RecordBatch>produced by the executor.session(&self)surfaces the underlying [llkv-runtime::RuntimeSession] for advanced transaction control.
SQL Preprocessing Pipeline
The engine normalizes dialect-specific syntax before parsing:
- Converts
CREATE TYPEtoCREATE DOMAIN, rewrites SQLite trigger shorthands, and patchesREINDEXstatements. - Expands SQLite
expr IN tablenameshorthands intoSELECTsubqueries so the planner sees a consistent AST. - Strips SQLite
INDEXED BY/NOT INDEXEDhints and removes trailing commas inVALUESclauses for DuckDB compatibility. - Handles
EXCLUDEqualified names and other minor dialect mismatches to keep the rest of the stack parser-agnostic.
INSERT Buffering
- Disabled by default; enable with
set_insert_buffering(true)when bulk-loading data. - Buffers consecutive
INSERT ... VALUESstatements targeting the same table, columns, and conflict action to reduce planning churn. - Flush triggers include buffer size limits (8,192 rows), cross-table inserts, non-
INSERTstatements, explicit transaction boundaries, expectation hints, or manualflush_pending_inserts()calls. - The buffered rows coalesce into one
InsertPlan, improving batch ingestion throughput without bypassing MVCC.
Transaction Support
- Standard
BEGIN,COMMIT, andROLLBACKstatements wire through the runtime to allocate or finalize MVCC snapshots. - Auto-commit mode executes each statement in its own transaction when no explicit transaction is active.
- Result variants map to
llkv-runtimestatement outcomes, includingSelect,Insert,Update,Delete, and catalog DDL responses.
Testing Hooks
- The SLT harness (
llkv-slt-tester) usesSqlEnginevia anEngineHarnessthat enables insert buffering and translates results to sqllogictest expectations. - Query-duration metrics (
LLKV_SLT_STATS=1) surface per-statement timing when running sqllogictest workloads.
License
Licensed under the Apache-2.0 License.