pub struct Ddx { /* private fields */ }Expand description
The ddx v1 differentiation engine.
Holds the (extensible) rule registry and the identifier-folding policy.
Construct one, optionally register custom rules, then drive it with
Ddx::rewrite_sql (the whole marker path) or the lower-level
Ddx::differentiate / Ddx::jvp (used by the DataFusion Path B bridge,
design.md §3.3).
§Example
use ddx_core::Ddx;
use ddx_core::sqlparser::dialect::GenericDialect;
let ddx = Ddx::new();
let out = ddx
.rewrite_sql("SELECT grad(sin(x), x) AS d FROM t", &GenericDialect {})
.unwrap();
assert_eq!(out, "SELECT (cos(x)) AS d FROM t");Implementations§
Source§impl Ddx
impl Ddx
Sourcepub fn new() -> Self
pub fn new() -> Self
A new engine with the built-in rule set and the generic
(FoldUnquoted) identifier policy — the DataFusion/Postgres rule
(unquoted identifiers fold to lowercase, quoted keep case).
Sourcepub fn for_datafusion() -> Self
pub fn for_datafusion() -> Self
The DataFusion-flavored engine (FoldUnquoted). Pair with
GenericDialect/DataFusion’s dialect when calling Ddx::rewrite_sql.
Sourcepub fn for_duckdb() -> Self
pub fn for_duckdb() -> Self
The DuckDB-flavored engine (FoldAll — DuckDB folds quoted identifiers
too). Pair with DuckDbDialect when calling Ddx::rewrite_sql.
Sourcepub fn with_casing(casing: IdentCasing) -> Self
pub fn with_casing(casing: IdentCasing) -> Self
A new engine with the built-in rules and an explicit identifier policy.
Sourcepub fn casing(&self) -> IdentCasing
pub fn casing(&self) -> IdentCasing
The identifier-folding policy this engine compares columns under.
Sourcepub fn register(&mut self, name: &str, rule: Rule)
pub fn register(&mut self, name: &str, rule: Rule)
Register (or override) a user differentiation rule for a unary function
name: the rule supplies f'(u) and the engine applies the chain rule
(design.md §3.2).
Sourcepub fn rewrite_sql(&self, sql: &str, dialect: &dyn Dialect) -> Result<String>
pub fn rewrite_sql(&self, sql: &str, dialect: &dyn Dialect) -> Result<String>
The whole marker path: rewrite every grad/jvp call in sql to
derivative SQL and return the rewritten text. A statement with no marker
is returned byte-identical (it is never even parsed, design.md §3.2).
dialect is used to parse the marker-bearing statement; the
identifier-folding policy used to match columns is this engine’s
(casing) — pair them (e.g. Ddx::for_duckdb() with DuckDbDialect).
Sourcepub fn explain(&self, sql: &str, dialect: &dyn Dialect) -> Result<Explanation>
pub fn explain(&self, sql: &str, dialect: &dyn Dialect) -> Result<Explanation>
Preview what Ddx::rewrite_sql would do to sql — every grad/jvp
marker and the derivative SQL it becomes, plus the fully rewritten
statement — without running anything. The returned Explanation is
inspectable field-by-field, and prints a readable summary via Display,
so it doubles as a quick interactive “what will this do?” for a REPL or
notebook.
use ddx_core::Ddx;
use ddx_core::sqlparser::dialect::GenericDialect;
let ddx = Ddx::new();
let ex = ddx
.explain("SELECT grad(sin(x), x) AS d FROM t", &GenericDialect {})
.unwrap();
assert_eq!(ex.rewritten, "SELECT (cos(x)) AS d FROM t");
assert_eq!(ex.steps.len(), 1);
assert_eq!(ex.steps[0].marker, "grad(sin(x), x)");
assert_eq!(ex.steps[0].derivative, "(cos(x))");
println!("{ex}"); // human-readable, inspect-before-you-run summarySourcepub fn differentiate(&self, e: &Expr, wrt: &ColRef) -> Result<Expr>
pub fn differentiate(&self, e: &Expr, wrt: &ColRef) -> Result<Expr>
Differentiate an AST expression with respect to wrt. The lower-level
entry the DataFusion bridge drives (design.md §3.3, Path B).
Sourcepub fn jvp(&self, e: &Expr, seeds: &[(ColRef, Expr)]) -> Result<Expr>
pub fn jvp(&self, e: &Expr, seeds: &[(ColRef, Expr)]) -> Result<Expr>
Forward-mode directional derivative: seed a tangent on each column in
seeds and push it through e (design.md §3.6).
(The design sketch names a HashMap<ColRef, Expr>; a slice of pairs is
used instead because ColRef equality is dialect-dependent — folding
makes it a poor hash key — so an explicit, ordered seed list is clearer
and preserves match order.)
Sourcepub fn differentiate_sql(
&self,
expr: &str,
wrt: &str,
dialect: &dyn Dialect,
) -> Result<String>
pub fn differentiate_sql( &self, expr: &str, wrt: &str, dialect: &dyn Dialect, ) -> Result<String>
The “calculus compiler” escape hatch: differentiate the scalar
expression expr (SQL text) with respect to the column wrt (a bare
column name), returning the derivative as SQL text — for embedding an
update rule where a marker can’t reach (design.md §3.6).