Darwen
Darwen is an in-memory engine for relational algebra inspired by The Third Manifesto of Date, Codd, and Darwen.
This is the final version of the library. Further development is not planned, because the project is educational.
Example
use ;
let users = new
.with_heading
.with_body
.build?;
let adults = users.restrict?;
let expected = new
.with_heading
.with_body
.build?;
assert_eq!;
# Ok::
Predicates
Darwen supports six predicate forms:
Not/Predicate::notnegates another predicate.And/Predicate::andperforms logical conjunction; both sides are always evaluated and errors are not hidden.Or/Predicate::orperforms logical disjunction; both sides are always evaluated and errors are not hidden.Eq/Predicate::eqcompares two operands for equality; onlyINTEGER = INTEGER,BOOLEAN = BOOLEAN,STRING = STRING, andBINARY = BINARYare valid. Mixed-type comparisons return an error.Gt/Predicate::gtcompares two operands with>; onlyINTEGER > INTEGERis valid. All other comparisons return an error.Lt/Predicate::ltcompares two operands with<; onlyINTEGER < INTEGERis valid. All other comparisons return an error.
Implemented Operations
RESTRICT/SELECTION (σ)
Example - cargo run --example restrict
σ age > 20 (people)
people
| name | age |
|---|---|
| Ann | 19 |
| Bob | 24 |
Output
| name | age |
|---|---|
| Bob | 24 |
Code
let adults = people.restrict?;
PROJECT (π)
Example - cargo run --example project
π name (people)
people
| name | age |
|---|---|
| Ann | 19 |
| Bob | 24 |
Output
| name |
|---|
| Ann |
| Bob |
Code
let names = people.project?;
RENAME (ρ)
Example - cargo run --example rename
ρ person_name/name (people)
people
| name |
|---|
| Ann |
| Bob |
Output
| person_name |
|---|
| Ann |
| Bob |
Code
let renamed = people.rename?;
UNION (⋃)
Example - cargo run --example union
a ⋃ b
a
| value |
|---|
| foo |
| bar |
b
| value |
|---|
| bar |
| baz |
Output
| value |
|---|
| foo |
| bar |
| baz |
Code
let result = a.union?;
DIFFERENCE (−)
Example - cargo run --example difference
a − b
a
| value |
|---|
| foo |
| bar |
| baz |
b
| value |
|---|
| bar |
Output
| value |
|---|
| foo |
| baz |
Code
let result = a.difference?;
PRODUCT (×)
Example - cargo run --example product
colors × sizes
colors
| color |
|---|
| red |
| blue |
sizes
| size |
|---|
| S |
| M |
Output
| color | size |
|---|---|
| red | S |
| red | M |
| blue | S |
| blue | M |
Code
let result = colors.product?;
JOIN (⋈)
Example - cargo run --example join
users ⋈ cities
users
| id | name |
|---|---|
| 1 | Ann |
| 2 | Bob |
cities
| id | city |
|---|---|
| 1 | Rome |
| 3 | Oslo |
Output
| id | name | city |
|---|---|---|
| 1 | Ann | Rome |
Code
let result = users.join?;
INTERSECT (∩)
Example - cargo run --example intersect
a ∩ b
a
| value |
|---|
| foo |
| bar |
b
| value |
|---|
| bar |
| baz |
Output
| value |
|---|
| bar |
Code
let result = a.intersect?;
DIVIDE (÷)
Example - cargo run --example divide
enrollments ÷ required_courses
enrollments
| student | course |
|---|---|
| Ann | Math |
| Ann | Rust |
| Bob | Math |
| Bob | Rust |
| Bob | DB |
| Kate | Math |
required_courses
| course |
|---|
| Math |
| Rust |
Output
| student |
|---|
| Ann |
| Bob |
Code
let result = enrollments.divide?;
Sources
- The Third Manifesto - the foundational manifesto of relational databases
- TutorialD - a practical implementation of relational algebra
- BNF for TutorialD from RelDB project