Lateral joins let you build composable, type-safe SQL query eDSLs in Rust and Haskell, reusing parameterized subqueries like functions.
Key Takeaways
CROSS JOIN LATERAL lets subqueries reference columns from preceding FROM clauses, enabling function-like query composition without cartesian product blowup.
Haskell’s Rel8 library models this as a Query monad where each do-block line emits a CROSS JOIN LATERAL, making joins look like pure data manipulation.
The author built a Rust port (rust-rel8) that replicates Rel8’s API using Expr<'scope, T> with a lifetime parameter to enforce expression scoping at compile time.
.many() aggregates a Query<T> into Query<ListTable<T>> for (User, Vec<Post>) results; .optional() produces left outer join semantics as MaybeTable<T>.
A Mode::T<'scope, U> ADT swaps field types between U, Expr<U>, and String depending on context, letting the same struct serve as both a DB row and a query builder handle.