Extensibility and custom compute
Daft lets users add computation without leaving the DataFrame model. Custom Python functions, stateful classes, custom aggregations, and native extension functions become lazy expressions that Daft can plan and run locally or across a distributed runner. This keeps user code connected to Daft's type, batch, scheduling, and aggregation semantics.
Extension model
Daft has two main extension paths. Python extensions are the common path for application logic and model or service work. Native extensions provide a lower-level path for reusable scalar and aggregate functions that need native execution.
Python computation
The Python API turns a function or class method into a Daft operation. A call with a DataFrame expression builds a lazy expression. A call with only scalar values runs immediately. This lets the same custom logic serve both query plans and ordinary Python code. @daft.func, @daft.cls, and @daft.udaf provide the main user-facing entry points.
Python functions cover several shapes of work:
- Row-wise computation maps one input row to one result.
- Batch computation receives column batches for vectorized libraries or batch model calls.
- Generator computation maps one input row to a variable number of output rows.
- Async computation overlaps I/O-bound calls.
- Stateful computation loads an expensive resource once per worker and reuses it across calls.
The function's type hints or declared return type tell Daft what output type to place in the expression. Batch functions must declare their return type. Generator functions must describe an iterator or generator result so Daft can infer the value produced for each output row. Stateful classes keep setup arguments in the expression and create the local resource when execution needs it.
References: daft/…/__init__.py• L21-79 daft/…/__init__.py• L251-276 daft/…/__init__.py• L358-413 daft/…/udf_v2.py• L51-128 daft/…/udf_v2.py• L243-268 daft/…/udf_v2.py• L418-498
Native extension functions
Native extensions package specialized scalar or aggregate functions behind a shared library. A Python package exposes friendly expression helpers, while the native library supplies the registered function. The extension boundary uses the Arrow C Data Interface so column data and schemas can cross the boundary without tying the extension to one Arrow library version. Native extensions are experimental.
An extension is loaded into a Daft session with Session.load_extension or daft.load_extension. Its install hook registers scalar functions and aggregate functions with that session. Python wrappers then resolve the registered names with get_function or get_aggregate_function, so the user calls an ordinary DataFrame expression rather than the native entry point directly.
References: extensions/overview.md• L62-78 extensions/authoring.md• L11-17 extensions/authoring.md• L165-195 daft/session.py• L269-295 src/…/session.rs• L7-16
Execution semantics
Custom compute is useful because it participates in the same lazy execution model as built-in expressions. Daft owns when and where the work runs. The extension supplies the computation and its type contract.
Expression composition
A custom operation can consume columns, derived expressions, and literal values. Its result can feed later selections, projections, or other expressions. The query does not run when an expression is built. It runs when the surrounding DataFrame is collected or otherwise executed.
When no argument contains a DataFrame expression, the Python function runs as an ordinary eager call. When an argument is an expression, Daft checks that the function and its state can be serialized, assigns the call a distinct identity, and places the operation in the query plan. This lets repeated uses of one function keep separate call-site state while Daft controls execution.
This boundary lets users hide domain logic inside a small API while Daft keeps control of the surrounding query. A library can therefore expose functions for text, media, models, files, or external services without requiring callers to manage a separate execution loop.
References: docs/…/func.md• L5-16 docs/…/func.md• L99-110 daft/…/udf_v2.py• L270-321 daft/…/udf_v2.py• L323-396 docs/…/overview.md• L20-39
Data shape and type contracts
Every extension point has a contract for the shape and type of its input and output. Row-wise functions return one value per input row. Batch functions return columnar data for the input batch. Generator functions produce multiple rows and can expand a struct result into named columns. Native functions declare their output schema before execution.
These contracts allow the planner and execution engine to place custom work in a larger query. A Python return type or return_dtype must match the data returned at runtime. A generator must expose an iterator or generator return type unless its output type is declared directly. When unnest=True, the result type must be a struct because Daft expands its fields into columns. A native function must accept the input schemas it declares and return data that matches its output schema.
References: daft/…/udf_v2.py• L175-189 daft/…/udf_v2.py• L243-268 daft/…/udf_v2.py• L323-396 src/…/function.rs• L12-17 docs/…/func.md• L202-245
Resource and error behavior
Custom work can state resource needs and execution behavior as part of its Daft contract. CPU and GPU hints limit concurrent work on a machine. Async functions can set a concurrency limit. Retry and error settings can control whether a failed call raises an error or produces a null result with logging or without logging.
Stateful classes make the resource boundary explicit. Daft serializes the setup arguments rather than the live local resource. Each worker creates its own instance when it first needs the function and can reuse that instance across calls. This is the suitable model for a model, connection pool, or client that should not be created once for every row.
References: docs/…/func.md• L403-452 docs/…/cls.md• L28-36 docs/…/cls.md• L62-97 daft/…/__init__.py• L358-413 daft/…/udf_v2.py• L175-201 daft/…/udf_v2.py• L418-498
Aggregation extensions
Aggregations differ from row-level functions because they reduce many input rows into one result per group or table. Daft supports custom aggregations through Python UDAFs and native aggregate functions. Both models preserve the distributed aggregation pattern.
Aggregation lifecycle
An aggregate extension has three logical stages:
- Aggregate turns input rows into a partial state.
- Combine merges partial states from different data partitions.
- Finalize turns the merged state into the result value.
The combine step must be associative and commutative because Daft can produce and merge partial states in different partitions. The state can contain one value or several named fields. This design lets aggregation happen near the data and reduces the amount of data that must move before the final result.
For a detailed guide to Python UDFs and aggregations, see UDFs and aggregations. For the native extension model, see Aggregate Functions.
References: custom-code/udaf.md• L39-53 custom-code/udaf.md• L75-101 extensions/authoring.md• L291-301 src/…/aggregate.rs• L12-30
Aggregation scope
Custom aggregations work in grouped and global queries. A grouped query produces one result for each group key. A global query produces one result for the whole input. The aggregate expression supplies the value logic while the DataFrame operation supplies the grouping scope.
References: docs/…/udaf.md• L3-4 docs/…/udaf.md• L148-166 daft/session.py• L602-612
Extension lifecycle
An extension has a load scope and an execution scope. The load scope decides which session can resolve the function. The execution scope decides which workers must have the extension available.
Session registration
Native functions are registered in the active session. get_function and get_aggregate_function resolve names from that session and return expression objects. A function loaded into one session is not automatically available in another. This gives applications a way to isolate function names and extension sets.
References: daft/session.py• L590-612 extensions/authoring.md• L364-397 extensions/authoring.md• L408-417
Distributed propagation
Distributed workers must load the same native extensions as the driver before they deserialize and run a plan. Daft propagates loaded extension paths to Ray workers when the distributed runner starts. Therefore, applications must load native extensions before the first query creates the runner. Loading one after that point does not update existing workers.
References: daft/…/flotilla.py• L102-136 daft/…/flotilla.py• L177-185 daft/…/flotilla.py• L725-735 daft/…/flotilla.py• L748-794
Extension choices
The right extension point depends on the data shape, state needs, and performance boundary of the custom work.
| Need | Extension point | Main contract |
|---|---|---|
| Simple custom logic per row | @daft.func | One row in and one value out |
| Vectorized or bulk work | @daft.func.batch | Series or column batches in and columnar output out |
| Variable-length output | Generator @daft.func | One input row can produce many output rows |
| Reusable model or client state | @daft.cls or @daft.method | Initialize state per worker and reuse it |
| Custom reduction by group or table | @daft.udaf | Aggregate, combine, and finalize state |
| Reusable native scalar or aggregate logic | Native extension | Register a typed function in a session |
Use the UDFs and aggregations page for the detailed Python extension paths. Use the Extensions overview and Authoring Guide when the extension should be a reusable package or needs native execution.
Change checklist
When these files or sections are changed, remember to consider these:
- Keep Python return annotations or
return_dtypealigned with the value shape returned by the function. A change from one-to-one to one-to-many output changes downstream row behavior. - Keep native function names synchronized across registration and the Python
get_functionorget_aggregate_functionwrapper. - Treat aggregate state fields as one contract across
aggregate,combine, andfinalize. A state schema change affects every stage. - Load-time changes must preserve extension propagation to distributed workers. The load must occur before the runner is created.
- Preserve session scope when adding or moving native registrations. A function must be loaded into the session that resolves the expression.
- Keep function and class state serializable at expression-build time. Put non-serializable resources in class setup so each worker creates them locally.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A Python function has no usable return annotation or declared return type | Daft cannot build its output type contract | Add a return type hint or return_dtype; batch functions require an explicit return type |
| A custom function returns data that does not match its declared type | The query fails during execution | Keep the declared type aligned with the actual output |
| A UDF or its captured class state cannot be serialized | Expression construction fails before distributed execution | Keep expression-time objects serializable and initialize non-serializable resources in a stateful class setup |
A synchronous @daft.func declares max_concurrency | Function creation fails because that setting has no effect for synchronous functions | Use an async function or a class-based function when concurrency control is needed |
| A native extension loads after the distributed runner starts | Existing Ray workers cannot resolve the extension | Load it before the first distributed query |
| An aggregate combine operation depends on input order | Partitioned aggregation can produce an incorrect result | Make partial-state combination associative and commutative |
| An async function also declares generator behavior | Daft rejects the unsupported combination | Choose async row or batch work, or a synchronous generator |
unnest=True is used with a non-struct return type | Daft rejects the function before it runs | Return a struct type when the result must expand into columns |
Key files
| File | Why to read it |
|---|---|
| docs/…/func.md | User guide for row-wise, batch, async, generator, and type-aware Python computation |
| docs/…/cls.md | User guide for worker-local state and class methods |
| docs/…/udaf.md | User guide for Python aggregation state and the three-stage reduction flow |
| docs/…/overview.md | Broad comparison of Python-package and native extension models |
| docs/…/authoring.md | End-to-end view of loading and exposing native scalar and aggregate functions |
| daft/…/__init__.py | Public Python entry points for functions, batches, stateful classes, methods, and aggregations |
| daft/…/udf_v2.py | Current Python function and class contracts that map calls to eager values or lazy expressions |
| daft/session.py | Session loading and name resolution for native functions |
| daft/…/flotilla.py | Distributed propagation and timing rules for native extensions |
| src/…/function.rs | Native scalar function contract at the extension boundary |
| src/…/aggregate.rs | Native aggregate lifecycle and state exchange contract |