# timeSeriesLimitKMasks (/docs/reference/sql/aggregate-functions/timeserieslimitkmasks)



This page describes the RawTree engine binary used to generate this reference. It does not define Query API support. Descriptions retain upstream ClickHouse terminology. See [SQL interface scope](/docs/reference/clickhouse-compatibility#service-interfaces).

Selects up to k time series at each time step of a time grid, in a deterministic pseudo-random fashion.

Each input row is one time series: `key` identifies the series, `sampling_key` is a per-series hash used as the
selection order, and `values` contains the values of the series aligned to a common time grid (so the `values` arrays
of all rows must have the same size). At each time step, the series with the k smallest sampling keys among the series
with non-NULL values at that step are selected. Sampling key ties are broken by preferring the series with the smaller `key`.

This function implements the `limitk()` aggregation operator of PromQL and keeps only one bounded heap of size `k` per
time step, so its state size does not depend on the number of aggregated series.

:::note
This function is experimental, enable it by setting `allow_experimental_time_series_aggregate_functions = 1`.
:::

**Syntax**

```sql
timeSeriesLimitKMasks(k, key, sampling_key, values)
```

**Arguments**

* `k` — How many series to select at each time step, either one value for all time steps or an array with one value per time step. Must be the same for all rows. [`UInt*`](/reference/data-types/int-uint) or [`Array(UInt*)`](/reference/data-types/array)
* `key` — Identifier of the time series. [`UInt64`](/reference/data-types/int-uint)
* `sampling_key` — A per-series hash defining the selection order, e.g. `timeSeriesGroupToSamplingKey(key)`. [`UInt64`](/reference/data-types/int-uint)
* `values` — Values of the time series aligned to the time grid, one element per time step. [`Array(Nullable(Float32))`](/reference/data-types/array) or [`Array(Nullable(Float64))`](/reference/data-types/array) or [`Array(Float32)`](/reference/data-types/array) or [`Array(Float64)`](/reference/data-types/array)

**Returned value**

Returns the selected series in the order of ascending `key`, each together with its per-step mask: `steps_mask[t] = 1` if the series is selected at time step `t`. Series which are selected at no time step are not returned. [`Array(Tuple(key UInt64, steps_mask Array(UInt8)))`](/reference/data-types/array)

**Examples**

**Selecting 2 series per time step by the smallest sampling keys**

```sql title=Query
SET allow_experimental_time_series_aggregate_functions = 1;
WITH [(1, [10., 1., NULL], 300), (2, [20., 2., 2.], 100), (3, [30., NULL, 1.], 200)]::Array(Tuple(UInt64, Array(Nullable(Float64)), UInt64)) AS series
SELECT timeSeriesLimitKMasks(2, s.1, s.3, s.2)
FROM (SELECT arrayJoin(series) AS s);
```

```text title=Response
[(1,[0,1,0]),(2,[1,1,1]),(3,[1,0,1])]
```

*Introduced in version 26.8.0.*
