Ports #
| Port | Direction | Kind | Required | Description |
|---|---|---|---|---|
in | input | records | yes | |
out | output | windows | yes |
Parameters #
| Name | Type | Default | Required | Description |
|---|---|---|---|---|
size | integer | none | yes | Minimum 1. Maximum 1000000. |
step | integer | none | no | Default: size (tumbling). Must not exceed size. Minimum 1. Maximum 1000000. |
partitionBy | string | none | no | Starlark expression over item and params. |
emitPartial | boolean | true | no | |
maxPartitions | integer | 10000 | no | Minimum 1. |
When to use it #
- A moving statistic: the mean of the last 5 sensor readings, recomputed for every new reading (
size: 5, step: 1). - Fixed groups by count: a summary for every 100 orders (
size: 100). - The same, separately for every customer or device:
partitionBy: "item['customer']".
When to use something else
- Windows are counted in records, not in time. Groups without a meaning for the next pipe are
std:batch. SQL window functions (OVER (…)) are a feature of SQL engines, not this pipe.
How it works #
The same ten items, a0 to b9, are grouped three ways. Tumbling, size 4: windows of items 0 to 3 and 4 to 7, then a partial window with the last two. Sliding, size 4 and step 2: a window starts every two items, so neighbours share two items; the last window is partial. Partitioned, size 2 by key: the a items and the b items form their own windows of two, each ending with a partial window of one. A dashed frame is a partial window, emitted at the end of the stream.
Drawn from a real run of the reference implementation.
What a window is #
Some questions are about a group of records, not about one: the average of the last 5 readings, a total for every 100 orders, running figures per customer. A window is such a group — a set of items with a boundary that means something. std:window cuts a stream into windows by counting records, and hands each complete window to the next pipe as one line.
The drawing above groups the same ten items three ways. An item written a0 is {"k":"a","v":0}.
Tumbling, sliding, partitioned #
- Tumbling —
size: 4. Every item belongs to exactly one window: items 0–3, then 4–7. The last two items do not fill a window; they are emitted at the end of the stream as a partial window (dashed). - Sliding —
size: 4, step: 2. A new window starts every 2 items, so consecutive windows overlap bysize − step= 2 items and most items appear in two windows. - Partitioned —
size: 2, partitionBy: "item['k']". Windows are formed separately for every value of the expression. Theaitems and thebitems are counted independently, each in its own order of arrival.
The parameters #
| Parameter | What it changes |
|---|---|
size |
The number of items in a full window. Required. |
step |
The distance between the starts of consecutive windows. The default is size (tumbling). It must satisfy 1 ≤ step ≤ size; anything else is rejected with E_PARAM. |
partitionBy |
A Starlark expression over item and params. Its JSON value names the partition of an item; without it the whole stream is one partition whose value is null. |
emitPartial |
What happens to unfilled windows at the end of the stream: true (default) emits them, false discards them. |
maxPartitions |
The largest number of partitions the pipe holds state for (default 10000). |
Within a partition, items are numbered 0, 1, 2, … in arrival order. Window k covers the items numbered k·step to k·step + size − 1, and is emitted as soon as its last item arrives.
What the next pipe receives #
The output port has the kind windows: every line is one complete window frame — a small window object and the items. With size: 3, step: 1 the input "a" "b" "c" "d" produces exactly these four lines:
{"window":{"index":0,"partition":null,"start":0,"end":2,"final":false,"partial":false},"items":["a","b","c"]}
{"window":{"index":1,"partition":null,"start":1,"end":3,"final":false,"partial":false},"items":["b","c","d"]}
{"window":{"index":2,"partition":null,"start":2,"end":3,"final":true,"partial":true},"items":["c","d"]}
{"window":{"index":3,"partition":null,"start":3,"end":3,"final":true,"partial":true},"items":["d"]}index counts the windows of a partition from 0; start and end are the positions, within the partition, of the first and last item in the frame; items is never empty. A pipe that consumes windows needs only a line reader and a JSON parser. A windows output connects only to a windows input: connecting it to a pipe that expects plain records is a validation error (E_SCHEMA).
The incomplete last window #
When the input ends, every window that has started but not filled is emitted with the items it has, marked partial: true and final: true — partitions in order of first appearance, windows in increasing index. In the example above that is two frames, because with step: 1 two windows had started. With emitPartial: false these windows are discarded instead, which is what a true moving average wants. Empty windows are never emitted, and there is no end marker: the stream simply ends.
Record count, not time #
std:window decides membership by the position of an item in its partition, never by a clock. std:window-processing-time groups items by the moment the runner receives them instead; the specification calls it a draft, inherently non-deterministic, with results that cannot be reproduced. Event-time windows, watermarks and session windows are not part of v0.1.
Memory #
The pipe buffers at most size items per partition and holds state for at most maxPartitions partitions. One partition more fails the run with E_RESOURCE; partitions are never dropped silently. With a small step, the flush at the end of the stream emits up to size / step partial frames per partition.
A worked pipeline #
The moving-window example feeds the frames to an R script that answers one window at a time. This is its pipeline.yaml, from the reference implementation:
spec: openflange.org/pipeline/v0.1
nodes:
window:
pipe: std:window
inputs:
in: {from: inputs.records}
parameters:
size: {$param: size}
step: {$param: step}
emitPartial: false # only full windows: a true moving average
mean:
pipe: ./window-mean
inputs:
windows: {from: window.outputs.out}
grants:
subprocess: true
outputs:
means: {from: mean.outputs.means}Window framing is proposed in v0.1 (RFC 0001): it is specified so that it can be implemented and tried, and it may still change incompatibly.
A tested example #
Nothing here is invented: this is the test “tumbling with a partial tail” from the pipe's own portable test file, which every conformant runner must pass.
{
"size": 2
}1
2
3
4
5{"window":{"index":0,"partition":null,"start":0,"end":1,"final":false,"partial":false},"items":[1,2]}
{"window":{"index":1,"partition":null,"start":2,"end":3,"final":false,"partial":false},"items":[3,4]}
{"window":{"index":2,"partition":null,"start":4,"end":4,"final":true,"partial":true},"items":[5]}The same step as a node of a pipeline.yaml:
nodes:
window:
pipe: std:window
inputs:
in: {from: inputs.in}
parameters:
size: 2Run it #
The pipe runs on its own from a shell. The input is the test input above. The quickstart shows how to get the openflange command.
printf '%s\n' 1 2 3 4 5 | openflange run std:window --param size=2{"window":{"index":0,"partition":null,"start":0,"end":1,"final":false,"partial":false},"items":[1,2]}
{"window":{"index":1,"partition":null,"start":2,"end":3,"final":false,"partial":false},"items":[3,4]}
{"window":{"index":2,"partition":null,"start":4,"end":4,"final":true,"partial":true},"items":[5]}Executed with openflange 0.0.1 (go1.27.1 linux/amd64), built from commit 11cf8cc of the reference implementation; the output shown is what it printed, and it matches the test.
Notes from the manifest #
The pipe's manifest, OPENFLANGE.md, carries these notes.
Part of the OpenFlange standard library (std, version 0.1).
Groups items into logical windows by count: tumbling, sliding and
partitioned. The output port has kind windows: each line is one complete
window frame (the framing is proposed in v0.1):
{"window": {"index": 0, "partition": null, "start": 0, "end": 2, "final": false, "partial": false}, "items": ["a", "b", "c"]}Semantics #
- Partitions and ordinals. Without
partitionBythere is one partition whose value isnull; with it, the partition of an item is the JSON value of the expression (JSON value equality, as forstd:distinct). Items are numbered per partition from 0 (ordinals) in arrival order. - Membership. Window
kof a partition covers the ordinalsk·step…k·step + size − 1.stepdefaults tosize(tumbling);step < sizegives sliding windows in which an item belongs to several windows.step > sizeis rejected withE_PARAM. - Emission. A window is emitted as soon as its last item arrives, with
partial: false, final: false. Frames of different partitions appear in the order in which their windows fill.indexisk,start/endare the partition ordinals of the first and last item in the frame. - End of stream. Every window that has started (contains at least one
item) but has not filled is emitted with the items it has,
partial: true, final: true— partitions in order of first appearance, windows in increasingindex. WithemitPartial: falsethese windows are discarded instead (a documented consumption). - Empty windows are never emitted, and there is no end marker frame: the end of the window stream is the stream's EOF.
Example, size: 3, step: 1, input "a" "b" "c" "d": frames [a,b,c] and
[b,c,d], then at end of stream the started windows [c,d] (index 2) and
[d] (index 3), both partial and final.
Bounded state #
At most size items are buffered per partition, and state is held for at
most maxPartitions partitions; one partition more fails the run with
E_RESOURCE. Partitions are never evicted. Note that with a small step
the end-of-stream flush emits up to size / step partial frames per
partition.
Runner transport batches and std:batch arrays never imply window
boundaries; only these frames do.
Reference #
- Maturity
- Proposed
- Needs the runner capability
windows- Processing scope
record- Manifest version
0.1.0
- Normative definition: std:window (proposed) in the standard library specification
- Manifest and tests: std/0.1/window in the specification repository
- Expression parameters are Starlark: how expressions are evaluated