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.

TransformExperimental

std:partition #

Routes items to one of up to eight outputs by key.

For example: spread records over up to eight branches by key.

A pipe is a data processing step with declared inputs and outputs; standard pipes are built into the runner.

Ports #

std:partition takes in and produces p0, p1, p2, p3, p4, p5, p6, p7.std:partitioninrecordsp0records · optionalp1records · optionalp2records · optionalp3records · optionalp4records · optionalp5records · optionalp6records · optionalp7records · optional
std:partition takes in and produces p0, p1, p2, p3, p4, p5, p6, p7.std:partitioninrecordsp0records · optionalp1records · optionalp2records · optionalp3records · optionalp4records · optionalp5records · optionalp6records · optionalp7records · optional
Named ports from the latest published contract. Dashed lines are optional ports.
Ports of std:partition
PortDirectionKindRequiredDescription
ininputrecordsyes
p0outputrecordsno
p1outputrecordsno
p2outputrecordsno
p3outputrecordsno
p4outputrecordsno
p5outputrecordsno
p6outputrecordsno
p7outputrecordsno

Parameters #

Parameters of std:partition
NameTypeDefaultRequiredDescription
keystringnoneyesStarlark expression over item and params.
partitionsintegernoneyes Minimum 1. Maximum 8.

When to use it #

  • Keep everything that belongs together on one branch: all records with the same key go to the same output within a run.
  • A fixed layout: with an integer key k the record goes to output p{k mod n}, so region 0, 1 and 2 always land on p0, p1 and p2.

When to use something else

  • For a yes/no decision use std:split. For keys that are not non-negative integers the assignment is implementation-defined in v0.1: compute an integer key when the layout matters.

How it works #

std:partition routes seven items to three outputs by keyThe items 0, 1, 2, 3, 4, 5 and 7 go in. With three partitions and the item itself as the key, each item goes to the output numbered by its remainder when divided by 3: 0 and 3 to p0; 1, 4 and 7 to p1; 2 and 5 to p2.in0123457std:partitionkey: item, partitions: 3p003p1147p225std:partition routes seven items to three outputs by keyThe items 0, 1, 2, 3, 4, 5 and 7 go in. With three partitions and the item itself as the key, each item goes to the output numbered by its remainder when divided by 3: 0 and 3 to p0; 1, 4 and 7 to p1; 2 and 5 to p2.in0123457std:partitionkey: itempartitions: 3p003p1147p225

The items 0, 1, 2, 3, 4, 5 and 7 go in. With three partitions and the item itself as the key, each item goes to the output numbered by its remainder when divided by 3: 0 and 3 to p0; 1, 4 and 7 to p1; 2 and 5 to p2.

Drawn from a real run of the reference implementation.

Routing by key #

key is evaluated for every item, and the item goes to one of partitions outputs, p0 … p{n-1}. Items with equal keys — equal as JSON values, so 1 and 1.0 are the same key — always go to the same output within a run, and the order of the items is kept on each output.

Which output a key maps to is implementation-defined in v0.1, with one exception that makes layouts portable: a key that is a non-negative integer k goes to p{k mod n}. In the drawing partitions is 3 and the key is the item itself, so 7 lands on p1.

Connect exactly the outputs in use #

The pipe declares eight optional outputs, p0 to p7, and partitions says how many are in use:

  • every output below partitions must be connected or explicitly discarded — otherwise the pipeline is refused with E_UNCONNECTED, because items routed there would be lost;
  • an output at or above partitions must not be connected (E_PARAM);
  • partitions larger than 8 is rejected (E_PARAM).

There is no “everything else” output: every key maps to one of the outputs in use.

Partition or split #

For a yes/no decision, std:split reads better: it has two named outputs, matched and unmatched. Use std:partition when the number of branches follows from a key.

A tested example #

Nothing here is invented: this is the test “integer keys go to k mod n” from the pipe's own portable test file, which every conformant runner must pass.

Parameters
{
  "key": "item",
  "partitions": 3
}
Input: in
0
1
2
3
4
5
7
Expected output: p0
0
3
Expected output: p1
1
4
7
Expected output: p2
2
5
Expected output: p3

Not connected in this test.

Expected output: p4

Not connected in this test.

Expected output: p5

Not connected in this test.

Expected output: p6

Not connected in this test.

Expected output: p7

Not connected in this test.

The same step as a node of a pipeline.yaml:

pipeline.yaml
nodes:
  partition:
    pipe: std:partition
    inputs:
      in: {from: inputs.in}
    parameters:
      key: "item"
      partitions: 3

Notes from the manifest #

The pipe's manifest, OPENFLANGE.md, carries these notes.

Part of the OpenFlange standard library (std, version 0.1).

Evaluates key for every item and sends the unchanged item to one of the outputs p0 … p(n-1), where n is partitions (1 to 8). Items with equal keys (JSON value equality, as for std:distinct) always share an output within a run, and each output preserves input order.

Assignment:

  • a key that is a non-negative integer k goes to p(k mod n) — this is normative and portable (an integral number such as 4.0 counts as 4);
  • any other key is assigned by an implementation-defined function. This runner uses the 64-bit FNV-1a hash of the key's canonical JSON, which is stable across runs but not portable to other runners. Compute an integer key when the layout matters.

Connections are checked before the run: every output p(i) with i < n must be connected or explicitly discarded (E_UNCONNECTED otherwise, since its items would be lost), and an output with i ≥ n must not be connected (E_PARAM).

yaml
nodes:
  shard:
    pipe: std:partition
    inputs: {in: {from: inputs.events}}
    parameters: {key: "item['user_id']", partitions: 2}
outputs:
  a: {from: shard.outputs.p0}
  b: {from: shard.outputs.p1}

Reference #

Maturity
Experimental
Processing scope
record
Manifest version
0.1.0