Skip to main content

Ddx

Struct Ddx 

Source
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

Source

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).

Source

pub fn for_datafusion() -> Self

The DataFusion-flavored engine (FoldUnquoted). Pair with GenericDialect/DataFusion’s dialect when calling Ddx::rewrite_sql.

Source

pub fn for_duckdb() -> Self

The DuckDB-flavored engine (FoldAll — DuckDB folds quoted identifiers too). Pair with DuckDbDialect when calling Ddx::rewrite_sql.

Source

pub fn with_casing(casing: IdentCasing) -> Self

A new engine with the built-in rules and an explicit identifier policy.

Source

pub fn unary_rule_names(&self) -> Vec<String>

The unary function names this engine can differentiate, sorted.

Read from the rule registry, so it reflects what is implemented rather than a list kept in step by hand — and it includes anything registered through Ddx::register.

Source

pub fn casing(&self) -> IdentCasing

The identifier-folding policy this engine compares columns under.

Source

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).

Source

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).

Source

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 summary
Source

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).

Source

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.)

Source

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).

Trait Implementations§

Source§

impl Clone for Ddx

Source§

fn clone(&self) -> Ddx

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Default for Ddx

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl !RefUnwindSafe for Ddx

§

impl !UnwindSafe for Ddx

§

impl Freeze for Ddx

§

impl Send for Ddx

§

impl Sync for Ddx

§

impl Unpin for Ddx

§

impl UnsafeUnpin for Ddx

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.