Skip to content

Add SLIDE parameter support to CAPACITY table-valued function#17456

Open
JackieTien97 wants to merge 2 commits intomasterfrom
capacity-slide-support
Open

Add SLIDE parameter support to CAPACITY table-valued function#17456
JackieTien97 wants to merge 2 commits intomasterfrom
capacity-slide-support

Conversation

@JackieTien97
Copy link
Copy Markdown
Contributor

Summary

The CAPACITY table-valued function currently only supports the SIZE parameter to specify the window length (number of rows per window). This PR adds an optional SLIDE parameter to specify the sliding step, enabling overlapping and gap windows based on row count.

Motivation

In time-series analysis, sliding windows are a common pattern. While the HOP function already supports sliding windows based on time, there was no equivalent for row-count-based windows. Adding SLIDE to CAPACITY fills this gap, enabling users to create:

  • Overlapping windows (SLIDE < SIZE): A single row can belong to multiple windows, useful for moving averages, rolling aggregations, etc.
  • Non-overlapping windows (SLIDE = SIZE): Equivalent to the current behavior (backward compatible).
  • Gap windows (SLIDE > SIZE): Windows with gaps between them, where some rows are discarded.

Changes

Core Implementation

CapacityTableFunction.java (iotdb-core/node-commons):

  1. New SLIDE parameter: Added as an optional INT64 scalar parameter with POSITIVE_LONG_CHECKER validation. Defaults to SIZE when not specified (preserving full backward compatibility).

  2. Updated analyze(): Reads and validates the SLIDE value, stores both SIZE and SLIDE in the MapTableFunctionHandle.

  3. Rewritten CapacityDataProcessor: Changed from a deferred batch-output model to per-row window assignment. For each row at index curIndex, the processor computes all windows k satisfying k * slide <= curIndex < k * slide + size (where k >= 0), and emits a (window_index, passThroughIndex) pair for each matching window.

  4. Added POSITIVE_LONG_CHECKER to SIZE: The original SIZE parameter used manual validation in analyze(). Now uses POSITIVE_LONG_CHECKER for consistency with other TVFs (HOP, TUMBLE, etc.).

Algorithm

For each row at position curIndex (0-based within its partition):

firstWindow = max(0, (curIndex - size + slide) / slide)
lastWindow  = curIndex / slide

For k in [firstWindow, lastWindow]:
    if k * slide <= curIndex < k * slide + size:
        emit (window_index=k, passThroughIndex=curIndex)

SQL Syntax

-- Without SLIDE (backward compatible, same as SLIDE = SIZE)
SELECT * FROM CAPACITY(DATA => t PARTITION BY id ORDER BY time, SIZE => 4)

-- With SLIDE (sliding window)
SELECT * FROM CAPACITY(DATA => t PARTITION BY id ORDER BY time, SIZE => 4, SLIDE => 2)

Integration Tests

IoTDBWindowTVFIT.java — Added 7 test scenarios to testCapacityFunction():

# Scenario Parameters Validates
1 SLIDE = SIZE SIZE=>2, SLIDE=>2 Backward compatibility — identical to no SLIDE
2 Overlapping windows SIZE=>2, SLIDE=>1 Same row appears in multiple windows with correct window_index
3 Overlapping (different params) SIZE=>3, SLIDE=>2 Different SIZE/SLIDE combinations
4 Gap windows SIZE=>2, SLIDE=>3 Rows in gaps are discarded
5 Overlapping + GROUP BY SIZE=>2, SLIDE=>1 + aggregation avg(price) computed correctly across overlapping windows
6 Negative SLIDE SLIDE=>-1 Error: Invalid scalar argument SLIDE, should be a positive value
7 Zero SLIDE SLIDE=>0 Error: Invalid scalar argument SLIDE, should be a positive value

All tests pass (verified locally with TableSimpleIT profile).

@codecov
Copy link
Copy Markdown

codecov bot commented Apr 10, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 39.83%. Comparing base (ab6289a) to head (edbff87).
⚠️ Report is 3 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff              @@
##             master   #17456      +/-   ##
============================================
+ Coverage     39.80%   39.83%   +0.02%     
  Complexity      312      312              
============================================
  Files          5135     5135              
  Lines        347075   347085      +10     
  Branches      44224    44221       -3     
============================================
+ Hits         138169   138274     +105     
+ Misses       208906   208811      -95     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@JackieTien97 JackieTien97 force-pushed the capacity-slide-support branch from 463d524 to 7073de7 Compare April 10, 2026 09:07
@sonarqubecloud
Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant