Expand description
Darwen is an in-memory engine for relational algebra.
The project is inspired by The Third Manifesto by Date, Codd, and Darwen, and focuses on a compact in-memory model for experimenting with relational operators.
§Example
use darwen::{
heading,
tuple,
prelude::{
AttributeName, HeadingBuilder, Predicate, RelationBuilder, Scalar,
ScalarType, TupleBuilder,
},
};
let users = RelationBuilder::new()
.with_heading(heading!(name = ScalarType::String, age = ScalarType::Integer)?)
.with_body(vec![
tuple!(name = "Monica", age = 18)?,
tuple!(name = "Erica", age = 19)?,
tuple!(name = "Rita", age = 20)?,
tuple!(name = "Tina", age = 21)?,
tuple!(name = "Sandra", age = 22)?,
tuple!(name = "Mary", age = 23)?,
tuple!(name = "Jessica", age = 18)?,
])
.build()?;
let adults = users.restrict(&Predicate::gt(
AttributeName::from("age"),
Scalar::Integer(20),
))?;
let expected = RelationBuilder::new()
.with_heading(heading!(name = ScalarType::String, age = ScalarType::Integer)?)
.with_body(vec![
tuple!(name = "Tina", age = 21)?,
tuple!(name = "Sandra", age = 22)?,
tuple!(name = "Mary", age = 23)?,
])
.build()?;
assert_eq!(adults, expected);§Predicates
Darwen supports six predicate forms:
Predicate::Not/Predicate::notnegates another predicate.Predicate::And/Predicate::andperforms logical conjunction; both sides are always evaluated and errors are not hidden.Predicate::Or/Predicate::orperforms logical disjunction; both sides are always evaluated and errors are not hidden.Predicate::Eq/Predicate::eqcompares two operands for equality; onlyINTEGER = INTEGER,BOOLEAN = BOOLEAN,STRING = STRING, andBINARY = BINARYare valid. Mixed-type comparisons return an error.Predicate::Gt/Predicate::gtcompares two operands with>; onlyINTEGER > INTEGERis valid. All other comparisons return an error.Predicate::Lt/Predicate::ltcompares two operands with<; onlyINTEGER < INTEGERis valid. All other comparisons return an error.
§Operators
§Restrict
Relation::restrict implements selection (σ) and returns only tuples
that satisfy a Predicate.
§Project
Relation::project implements projection (π) and keeps only the
requested attributes.
§Rename
Relation::rename implements renaming (ρ) and changes attribute names
according to a mapping.
§Union
Relation::union implements union (⋃) for relations with identical
headings.
§Difference
Relation::difference implements difference (−) for relations with
identical headings.
§Product
Relation::product implements Cartesian product (×) for relations with
disjoint headings.
§Join
Relation::join implements natural join (⋈) on shared attributes.
§Intersect
Relation::intersect implements intersection (∩) for relations with
identical headings.
§Divide
Relation::divide implements relational division (÷).
Modules§
- prelude
- Convenient re-exports for the public relational algebra API.
Macros§
Structs§
- Attribute
Name - The name of an attribute in a heading or tuple.
- Heading
- Defines the schema of a relation as a set of named attributes and types.
- Heading
Builder - Builds a
Headingattribute by attribute. - Relation
- Represents a relation with a heading and a set of tuples.
- Relation
Builder - Builds a
Relationfrom a heading and a body of tuples. - Tuple
- Represents one tuple in a relation body.
- Tuple
Builder - Builds a
Tuplevalue by adding attributes one by one.
Enums§
- Error
- Errors returned by relation construction and relational algebra operations.
- Expression
- Describes a value used during predicate evaluation.
- Predicate
- Represents a boolean condition evaluated against a tuple.
- Scalar
- Stores a typed scalar value used in tuples and predicates.
- Scalar
Type - Enumerates the scalar types supported by the engine.