Different pipes. One standard.

Type to search this site.

Search runs in your browser against a static index built with this site (Pagefind). Nothing you type is sent to a server.

Can a file-in/file-out tool be a step with no glue code?

Available — v0.0.1 (experimental)sqlduckdbbatch

DuckDB over batch files

With batch file delivery a tool that reads a file and writes a file becomes a pipe: the command contains {batch.input} and {batch.output}, and the runner starts it once per batch. The manifest is the whole pipe.

Available in v0.0.1: the files, README and tests are in examples/batch-file-duckdb of the openflange-go repository. Commands run from the repository root.

Needs: the duckdb CLI on PATH (the repository verified it with DuckDB 2.0.1).

A pipe is a data-processing step with declared inputs and outputs.

What goes in, what comes out

Input (stdin)
{"id":1,"qty":2,"unitCents":1250,"taxRate":0.2}
Command
printf '%s\n' '{"id":1,"qty":2,"unitCents":1250,"taxRate":0.2}' \
  | openflange run ./examples/batch-file-duckdb --grant subprocess
Output (stdout)
{"id":1,"qty":2,"unitCents":1250,"taxRate":0.2,"netCents":2500,"taxCents":500,"grossCents":3000}

Files and further commands

examples/batch-file-duckdb/OPENFLANGE.md
---
spec: openflange.org/pipe/v0.1
id: order-lines-duckdb
version: 0.1.0
title: Order line amounts (DuckDB, one process per batch)
description: Computes net, tax and gross amount of every order line with a row-wise DuckDB SQL query; DuckDB is started once per batch file.
license: Apache-2.0
keywords: [duckdb, sql, batch, file, example]
maturity: experimental
scope: record
cardinality: one-to-one
deterministic: true
batch:
  maxItems: 50000
  maxWaitMs: 1000
  delivery: file
inputs:
  lines:
    description: 'Order lines: {"id": integer, "qty": integer, "unitCents": integer, "taxRate": number}.'
    schema:
      format: json-schema
      definition:
        type: object
        required: [id, qty, unitCents, taxRate]
outputs:
  lines:
    description: The same lines, in order, with `netCents`, `taxCents` and `grossCents` added.
    schema:
      format: json-schema
      definition:
        type: object
        required: [id, netCents, taxCents, grossCents]
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)
permissions:
  subprocess: true
---
# Order line amounts (DuckDB, one process per batch)

Implements the [OpenFlange](https://openflange.org/) specification.

A file-in/file-out tool wrapped with **no protocol code at all**. The command
contains `{batch.input}` and `{batch.output}`, so the runner starts it once per
batch with the two paths filled in: DuckDB reads an NDJSON file of up to 50,000
order lines and writes an NDJSON file. See the [README](README.md).
Run its portable tests
openflange test ./examples/batch-file-duckdb

What to notice

  • Because the command contains {batch.input}, the runner starts it once per batch. It writes the batch to a private NDJSON file, substitutes the absolute paths into the arguments — plain text substitution, no shell — and reads the output file back when the command exits with status 0.
  • {{ is a literal {; the manifest uses it for DuckDB’s columns = {…}. Without {batch.output} the output of the batch would be read from standard output.
  • A process start per batch. That is negligible for a batch of tens of thousands of records and ruinous for ten. Use large batches, or a long-running process as in the R example.
  • Batch boundaries are not semantic. The query is row-wise. A GROUP BY, a window function or an ORDER BY here would give one partial answer per batch; for an aggregate over the whole input declare scope: dataset, as the DuckDB aggregation example does.
  • The batch directory is private to the node (mode 0700 under TMPDIR) and is removed when the node ends. It needs no filesystem permission, and it is not a sandbox.
  • Not re-run for this page (no DuckDB on the machine that built it); the command and output are copied from the repository README. The idea is explained in Batch mode.