Skip to main content
Vane Data / Reference

Relation.map_batches

Relation.map_batches passes a batch of input rows to a synchronous Python callable as a pyarrow.Table. The function may change columns and row count, and schema declares the complete output.

Signature

text
Relation.map_batches(
    function: Callable[..., typing.Any],
    schema: dict[str, sqltypes.DuckDBPyType] | None = None,
    *,
    batch_size: int | None = None,
    output_batch_size: int | None = None,
    min_task_batch_size: int | None = None,
    preserve_compute_batch_boundaries: bool | None = None,
    cpus: float | None = None,
    gpus: float | None = None,
    memory_bytes: int | None = None,
    execution_backend: typing.Literal["subprocess_task", "subprocess_actor", "ray_task", "ray_actor"] | None = None,
    actor_number: int | None = None,
    ray_actor_thread_policy: typing.Literal["managed", "ray_native"] | None = None,
    target_max_batch_bytes: int | None = None,
    task_input_max_bytes: int | None = None,
    output_target_max_bytes: int | None = None,
) -> DuckDBPyRelation

Parameters

NameTypeDescriptionDefault
functionSynchronous function, bound method, or zero-argument callable classReceives a pyarrow.Table and returns a materialized pyarrow.Table, pyarrow.RecordBatch, column dict, a synchronous iterable yielding those types, or NoneRequired
schemaNon-empty dict[str, DuckDBPyType]Complete output names, order, and types. The current runtime requires an explicit valueRequired
batch_sizePositive integer or NoneMaximum rows passed to one callable invocationNone
output_batch_sizePositive integer or NoneTarget rows per output Arrow blockNone
min_task_batch_sizePositive integer or NoneSoft Task-input floor; requires batch_size and cannot be smallerNone
preserve_compute_batch_boundariesbool or NoneFlushes output after each compute batch when TrueNone
cpusFinite non-negative number or NoneCPU resource per Task or ActorNone
gpusFinite non-negative number or NoneGPU resource per Task or Actor; positive values require RayNone
memory_bytesPositive integer or NoneMemory resource per Task or Actor; available only with Ray backendsNone
execution_backendsubprocess_task, subprocess_actor, ray_task, ray_actor, or NoneExecution backend; defaults from the runner and callable shapeNone
actor_numberPositive integer or NoneActor instance count; required for Actor backends and invalid for Task backendsNone
ray_actor_thread_policyray_native, managed, or NoneRay Actor thread policy; valid only for ray_actor and currently resolves to ray_native by defaultNone
target_max_batch_bytesPositive integer or NoneCommon byte target for Task input and output blocksNone
task_input_max_bytesPositive integer or NoneInput-byte target for one Task or Actor callNone
output_target_max_bytesPositive integer or NoneOutput-block byte targetNone

Returns and errors

map_batches() returns a new Relation and leaves the input unchanged. The result contains only the columns declared in schema.

The callable must return materialized output. pyarrow.RecordBatchReader is not supported.

Invalid functions, schemas, or execution options raise when map_batches() is called. Function errors, unsupported return values, and columns that do not match schema raise when results are fetched.

Task and Actor backends may retry calls, so external effects must be idempotent. A callable class runs in independent, ephemeral Actors with no work affinity or global ordering; Actor reconstruction resets local state.

Example

example.py
import vane




def keep_large_values(table):
    import pyarrow.compute as pc


    return table.filter(pc.greater(table["value"], 1))




source = vane.sql("SELECT * FROM (VALUES (1), (2), (3)) AS t(value)")
result = source.map_batches(
    keep_large_values,
    schema={"value": vane.sqltypes.BIGINT},
)


print(result.order("value").fetchall())
vane.close()

Output:

text
[(2,), (3,)]

The callable filters three input rows down to two, demonstrating N → M cardinality. The result contains the complete output declared by schema.