Hi all,

I'd like to introduce a recent enhancement to the CAPACITY table-valued
function: PR #17456 adds an optional SLIDE parameter to support sliding
windows based on row count.

Background

A community user reported an interesting use case. They store daily stock
closing prices in IoTDB and want to compute 5-week moving averages. Their
initial approach was to use the HOP (sliding time window) table-valued
function. However, they quickly ran into a problem: due to public holidays
— some of which span more than a week — certain time-based windows ended up
containing only 4 weeks of trading data instead of 5, because there were
simply no trades during those holiday periods.

To work around this, the user switched to the CAPACITY function, which
partitions data by absolute row count rather than time intervals, ensuring
each window always contains exactly the desired number of data points
regardless of time gaps. This solved the holiday problem, but CAPACITY did
not support a SLIDE parameter, meaning it could only produce
non-overlapping windows — which is insufficient for computing moving
averages where overlapping windows are essential.

What Changed

This PR adds an optional SLIDE parameter to the CAPACITY function:

- SLIDE < SIZE: Produces overlapping windows (a single row may belong to
multiple windows), enabling use cases like moving averages.
- SLIDE = SIZE (default): Equivalent to the current behavior —
non-overlapping windows. Full backward compatibility is preserved.
- SLIDE > SIZE: Produces windows with gaps, where rows falling in the gaps
are excluded.

Example usage:

-- 5-row window sliding by 1 row (for moving averages)
SELECT stock_id, window_index, avg(close_price) AS avg_close
FROM CAPACITY(DATA => trades PARTITION BY stock_id ORDER BY time, SIZE =>
5, SLIDE => 1)
GROUP BY window_index, stock_id
ORDER BY stock_id, window_index

The implementation follows the same pattern as the existing HOP function
but operates on row counts instead of timestamps. Only one source file was
modified (CapacityTableFunction.java), along with corresponding unit tests
and integration tests.

PR link: https://github.com/apache/iotdb/pull/17456

Feedback and reviews are welcome. Thanks!

Best regards,
----------------
Yuan Tian

Reply via email to