The problem #
A step that runs in another process pays for every record: a line to parse, a line to print, a round trip through two operating-system pipes. Three kinds of step feel it most. A request to a language model, an embedding service or a database costs per call, whatever the call carries. R and NumPy-style Python are fast on vectors and slow on scalars. And tools such as DuckDB want a file, not a stream of lines.
The usual answer is to buffer inside the program, with a home-made flush timer. Then every script reinvents it, and the runner — the program that runs pipes, and the place where waiting and memory are decided — knows nothing about it.
The fix #
The pipe says in its manifest that it wants its input in batches. The runner collects records and hands the script up to maxItems at a time, and never holds a record back for longer than maxWaitMs milliseconds. This is the manifest of the R example in the repository, with the port declarations left out:
cardinality: one-to-one
deterministic: true
batch:
maxItems: 1000
maxWaitMs: 200
# … inputs and outputs: one port each …
implementation:
type: script
language: r
path: tidy.R
permissions:
subprocess: truemaxItems is required. maxWaitMs is optional and defaults to 0: the runner never waits for a batch to fill. While the script works on one batch the runner keeps collecting the next, so a busy script gets fuller batches by itself.
The script reads one JSON array per line and answers each line with one JSON array. The answer line is also how the runner knows the batch is done, so the script flushes it. This is the loop of the same example, tidy.R; tidy is a vectorised function defined above it in the file:
con <- file("stdin", open = "r")
while (length(line <- readLines(con, n = 1, warn = FALSE)) > 0) {
df <- fromJSON(line) # a batch: array of records -> data frame
cat(toJSON(tidy(df), digits = NA), "\n", sep = "") # one answer line per batch
flush(stdout())
}Some tools want a file. With delivery: file each batch is a plain NDJSON file, one record per line. If the command contains {batch.input}, the runner starts it once per batch and puts the path of the file there; {batch.output} is where the tool writes its result. This DuckDB pipe has no script at all ({{ is a literal {):
batch:
maxItems: 50000
maxWaitMs: 1000
delivery: file
# … inputs and outputs …
implementation:
type: executable
command:
- duckdb
- -no-init
- -bail
- -c
- >-
copy (
select id, qty, unitCents, taxRate,
qty * unitCents as netCents,
cast(round(qty * unitCents * taxRate) as bigint) as taxCents,
qty * unitCents + cast(round(qty * unitCents * taxRate) as bigint) as grossCents
from read_json('{batch.input}', format = 'newline_delimited',
columns = {{id: 'BIGINT', qty: 'BIGINT', unitCents: 'BIGINT', taxRate: 'DOUBLE'})
) to '{batch.output}' (format json)Without {batch.output} the output of the batch is read from standard output. A command without placeholders is started once and is told the paths on standard input, one line per batch.
The precise terms #
- Batch mode is declared with
batch: {maxItems, maxWaitMs, delivery}.maxItemsis required.maxWaitMs(default 0) anddelivery(lines, the default, orfile) are optional. - It is for pipes whose code lives outside the runner or in a script: executable pipes and script pipes (Starlark, R, Python, Node). A Starlark script defines
process_batch(items, params)and always receives a list in memory;delivery: fileis for executables and for R, Python and Node scripts. Built-in, expression, composite and HTTP pipes cannot declare batch mode. - The pipe must have exactly one input port, of kind
records,valuesorwindows. - A batch is delivered when it holds
maxItemsitems, whenmaxWaitMshas passed since its first item arrived, or when the input ends. There is no minimum size, and one batch is in flight at a time. - Batch boundaries carry no meaning. A conformant pipe gives the same output, in the same order, however its input is cut. A sum or a rank across a batch is a bug. Grouping that means something is a window (
std:window); a statistic over the whole input isscope: dataset. - Memory stays bounded: a node holds at most twice
maxItemsitems. A pipeline node or a test may only tighten the settings, never loosen them. - Batch mode grants nothing. A pipe that sends batches to a remote service still declares
networkandremoteData. The examples in the repository call no network service; for an API the script has the same shape — one request per array instead of one per record. - Batch mode is experimental and in release v0.0.1 of the reference runner. It was accepted in RFC 0003; the wire format is in the CLI specification, sections 5.1 and 5.2.
- Runnable examples: Python, R, Starlark and DuckDB with batch files.