winterbaume-sqlengine-duckdb
DuckDB-backed SQL engine backend for winterbaume Athena and Redshift Data.
This crate is part of the winterbaume workspace — a suite of in-process AWS service mocks for Rust. Use the umbrella winterbaume crate to pull in all services at once, or depend on this crate directly.
Overview
This crate provides two backend implementations that execute SQL queries against an in-memory DuckDB instance:
DuckDbAthenaQueryBackend— implementsAthenaQueryBackend(Trino dialect)DuckDbRedshiftQueryBackend— implementsRedshiftQueryBackend(Redshift dialect)
SQL is transparently transpiled from the source dialect (Trino or Redshift) to DuckDB-compatible SQL via the papera crate before execution.
Usage
Basic (empty database)
use ;
use Connection;
use BehaviorVersion;
use AthenaService;
use MockAws;
use DuckDbAthenaQueryBackend;
async
Seeding the database
Because the Connection is injected from outside, you can seed it with tables and data before handing it to the backend:
use ;
use Connection;
use DuckDbAthenaQueryBackend;
let conn = new;
// Seed the database while you still hold a handle.
conn.lock.unwrap.execute_batch.expect;
let backend = new;
// Queries executed through this backend can now SELECT from the `users` table.
// You can also keep seeding via `conn` after construction.
The same pattern works for DuckDbRedshiftQueryBackend. You can also share a single connection across both backends:
use ;
use Connection;
use ;
let conn = new;
conn.lock.unwrap.execute_batch.expect;
let athena_backend = new;
let redshift_backend = new;
// Both backends query the same underlying database.
Loading from a file
You can also point at a pre-populated DuckDB database file:
use ;
use Connection;
use DuckDbAthenaQueryBackend;
let conn = new;
let backend = new;
How it works
Each backend struct holds a shared Arc<Mutex<Connection>>. When a query arrives:
- The mutex is locked briefly to call
Connection::try_clone(), which creates a lightweight handle to the same underlying DuckDB database. - The SQL is transpiled from the source dialect (Trino or Redshift) to DuckDB-compatible SQL.
- The query is executed on the cloned connection and results are returned.
This design means the database is shared across all queries while avoiding contention — the mutex is never held during query execution.
License
Licensed under Apache-2.0. See LICENSE for details.