Skip to main content

ddx_sql

Function ddx_sql 

Source
pub async fn ddx_sql(ctx: &SessionContext, sql: &str) -> Result<DataFrame>
Expand description

Rewrite grad/jvp markers in sql and run the result on ctx — the one-liner form of the text rewrite.

The context needs no ddx setup at all: no marker UDFs, no analyzer rule. By the time the engine sees the statement the markers are gone, replaced by ordinary derivative SQL.

A statement containing no marker is passed through byte-identical and is never even parsed by ddx, so wrapping every query in ddx_sql costs essentially nothing.

let ctx = SessionContext::new();
ctx.sql("CREATE TABLE t AS VALUES (1.0), (2.0), (3.0)").await?.collect().await?;

// d(x*x)/dx = 2x, computed by the engine as an ordinary column.
let df = ddx_sql(&ctx, "SELECT grad(column1 * column1, column1) AS d FROM t").await?;
let batches = df.collect().await?;
assert_eq!(batches[0].num_rows(), 3);