dbquery is a small OCaml library + CLI that parses and evaluates a DSL for
CSV-backed tables.
- Parses a query language with assignments and table expressions.
- Evaluates programs left-to-right with mutable variable bindings.
- Supports
load,project,join,rename,print, andsave. - Validates CSV/runtime invariants with explicit
RuntimeErrormessages. - Uses a hash-indexed join implementation for faster key joins.
- OCaml
- Dune (
>= 3.16) csvlibrarymenhir
dune buildRun a sample program:
dune exec dbquery -- data/test2.dbqRun tests:
dune testProgram shape:
<command>; <command>; ...
Commands:
- Assignment:
x := <table_expr>; - Print:
print <table_expr>; - Save:
save <table_expr> "path/to/file.csv";
Table expressions:
- Variable:
x - Load CSV:
load "data/chem_grades.csv" - Project columns:
project ["ID"; "hw2"; "hw1"] from x - Join on key:
join a with b on "ID" - Rename column:
rename "old" to "new" from x - Parenthesized expression:
(<table_expr>)
a := load "data/chem_grades.csv";
b := project ["ID"; "hw2"; "hw1"] from a;
print b;
Run it:
dune exec dbquery -- ./example.dbq- Loaded CSVs must be non-empty.
- Loaded CSVs must be rectangular.
- Loaded CSV headers must be unique.
projectrequires requested columns to exist and be unique in the list.joinis an inner join on equality and emits the key column once.joinpreserves deterministic row order and indexes the right table by key.renamefails if the target column name already exists.
lib/: AST, lexer/parser frontend, evaluator.bin/main.ml: CLI entrypoint.test/: deterministic parser/runtime/integration tests.data/: sample CSV inputs and.dbqprograms.
Originally written for coursework and then cleaned up with stronger tests, clearer invariants, and evaluator optimizations.