Skip to main content
Vane Data / Reference

Relation.flat_map

Relation.flat_map calls a synchronous Python callable for each input row. One row may produce zero, one, or many rows, and schema declares the complete output layout.

Signature

text
Relation.flat_map(
    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,
    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 row dict and returns a row dict, synchronous iterable, 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 per compute batch; the callable still receives one dict at a timeNone
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 NoneAccepted, but the row-output path has no distinct boundary-flush behaviorNone
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
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

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

Invalid functions, schemas, or execution options raise when flat_map() is called. Function errors, unsupported return values, and values 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 split_words(row):
    for word in row["text"].split():
        yield {"word": word.lower()}




source = vane.sql("SELECT 'Vane Data' AS text")
result = source.flat_map(
    split_words,
    schema={"word": vane.sqltypes.VARCHAR},
)


print(result.fetchall())
vane.close()

Output:

text
[('vane',), ('data',)]