# Finishing

Ordering, limiting and windowing a pipeline, and the stages that turn it into one answer.

`sort` orders, `limit` cuts, `window` keeps a range of versions. `group_by`, `percentile` and `count` drain the pipeline into one answer, so nothing may follow them.

## Order and bound

- `limit`: Stop the upstream after at most n records or values at this point in the pipeline. Arguments: n: a whole number from 1 to 4294967295.
- `sort`: Order the whole upstream by a field before the quoted jq stage. Arguments: field: a top-level field with a leading dot; direction: optional desc after a semicolon.
- `desc`: Order the sort field from greatest to least instead of ascending order.
- `window`: Keep records by their revision append version, never by a time inside the record. Arguments: from: an included unsigned version or quoted RFC 3339 instant; to: an excluded bound of the same form, or $now.


## One answer

- `count`: Consume the whole upstream and answer its size as the last stage.
- `group_by`: Drain records before jq into key, count and values groups in jq value order; missing fields group under null and no continuation is available. Arguments: field: a top-level field with a leading dot.
- `percentile`: Drain records before jq into one nearest-rank JSON number; empty numeric input gives null, zero selects the minimum, non-numbers are counted as skipped, and no continuation is available. Arguments: field: a top-level numeric field with a leading dot; percentile: a number from 0 to 100 inclusive.


> Drain stages end the pipeline
> `group_by`, `percentile` and `count` finish the pipeline. A later stage is refused. `rerank` also drains its upstream, but accepts a following `limit`; the similarity example uses that form. Drain results have no continuation token.
