Ports #
| Port | Direction | Kind | Required | Description |
|---|---|---|---|---|
in | input | records | yes | |
p0 | output | records | no | |
p1 | output | records | no | |
p2 | output | records | no | |
p3 | output | records | no | |
p4 | output | records | no | |
p5 | output | records | no | |
p6 | output | records | no | |
p7 | output | records | no |
Parameters #
| Name | Type | Default | Required | Description |
|---|---|---|---|---|
key | string | none | yes | Starlark expression over item and params. |
partitions | integer | none | yes | 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
kthe record goes to outputp{k mod n}, so region 0, 1 and 2 always land onp0,p1andp2.
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 #
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
partitionsmust be connected or explicitly discarded — otherwise the pipeline is refused withE_UNCONNECTED, because items routed there would be lost; - an output at or above
partitionsmust not be connected (E_PARAM); partitionslarger 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.
{
"key": "item",
"partitions": 3
}0
1
2
3
4
5
70
31
4
72
5Not connected in this test.
Not connected in this test.
Not connected in this test.
Not connected in this test.
Not connected in this test.
The same step as a node of a pipeline.yaml:
nodes:
partition:
pipe: std:partition
inputs:
in: {from: inputs.in}
parameters:
key: "item"
partitions: 3Notes 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
kgoes top(k mod n)— this is normative and portable (an integral number such as4.0counts as4); - 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).
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
- Normative definition: std:partition in the standard library specification
- Manifest and tests: std/0.1/partition in the specification repository
- Expression parameters are Starlark: how expressions are evaluated