Skip to content

[Bug]: bounded ROWS sliding window recomputes each frame and scales O(N×W) #27352

Closed

Description

@Ariznawlll

Description

A finite-width ROWS ... PRECEDING aggregate window is recomputed from scratch for every output row. For SUM, runtime grows approximately with row_count × frame_width, although a sliding aggregate can maintain the frame incrementally.

On 80,000 rows, increasing the frame from 0 to 1,024 rows increases MatrixOne runtime from about 0.05 s to 16.4 s. MySQL 8.0.46 remains about 0.08 s for every tested width. A 600,000-row MatrixOne control with width 1,024 takes 122.3 s.

Environment

  • Branch: main
  • Commit: e43d63edb59d1068eadf98613fd2d335e4f786d6
  • Deployment: local standalone MatrixOne CN; Connector/J 8.0.33 server-side PreparedStatement
  • Control: MySQL 8.0.46, same host and Connector/J

Steps to reproduce

drop database if exists window_frame_perf;
create database window_frame_perf;
create table window_frame_perf.t (
  id bigint primary key,
  ts timestamp(6),
  v int
);

insert into window_frame_perf.t
select result,
       timestampadd(microsecond, result, cast('2024-01-01 00:00:00.000000' as datetime(6))),
       case when mod(result, 17) = 0 then null else mod(result, 1000) end
from generate_series(1, 80000) g;

Prepare and execute the following statement three times for each bound 0, 31, 128, 512, and 1024:

select count(*), sum(ws), min(ws), max(ws)
from (
  select sum(coalesce(v, 0)) over (
    order by ts, id rows between ? preceding and current row
  ) ws
  from window_frame_perf.t
) q;

Actual behavior

All result summaries are correct, but MatrixOne runtime grows linearly with the finite frame width:

Bound MatrixOne 3 runs MySQL 8.0.46 3 runs
0 53 / 46 / 44 ms 80 / 78 / 78 ms
31 536 / 542 / 563 ms 79 / 84 / 81 ms
128 2088 / 2089 / 2086 ms 81 / 80 / 79 ms
512 8290 / 8110 / 8188 ms 85 / 79 / 80 ms
1024 16529 / 16375 / 16353 ms 80 / 81 / 80 ms

For bound 1024, both products return the same summary:

count=80000, sum(ws)=38224384305, min(ws)=1, max(ws)=494105

An additional 600,000-row run took 122,266 ms at bound 1024 versus 3,580 ms at bound 31. CN RSS was sampled during this scale run: 843,056 KiB before, 1,433,072 KiB peak, and 1,195,152 KiB after cleanup; the CN did not restart.

Expected behavior

Fixed-size cumulative aggregates such as SUM should maintain a bounded sliding state (add the entering row and remove the leaving row), or use another algorithm whose work does not replay the full frame for every output row. Runtime should not scale as O(row_count × frame_width) for this query shape.

Stability and controls

  • Reproducer: 3/3 at every frame width on the latest official main
  • MySQL control: 3/3 with identical values; 78–85 ms across all widths
  • Result oracle: independent Java sliding-window calculation; exact count/sum/min/max asserted
  • Prepared mode: actual Connector/J ServerPreparedStatement, reused while changing the frame parameter
  • Service state: no panic, OOM, or restart; database removed after both success and failure paths

Evidence

  • Full reproducer: WindowBoundedFramePerfProbe.java
  • Scale/control probe: WindowTemporalScaleProbe.java
  • Exact timing and result summaries are included above.

Code analysis

pkg/sql/colexec/window/window.go, processAggregateFuncRange, builds the interval for each output row and calls Fill for every row in that interval. This is O(N × W) for a bounded W PRECEDING frame.

The linear cumulative path added by PR #27006 only applies when the frame starts at the partition boundary (unbounded, or a finite bound covering the entire partition). It therefore fixes #23107 but intentionally does not handle a normal finite sliding frame such as 1,024 preceding rows.

Regression coverage

After the implementation is optimized:

  • add a focused Window benchmark covering several finite frame widths and asserting non-linear growth is eliminated;
  • retain a small deterministic SQL correctness case for Prepared frame parameters;
  • keep the 80,000+ row timing/scale gate in nightly big-data or performance coverage rather than BVT.

Related

Activity

  1. added this to the MO-202608 milestone on Aug 19, 2026
  2. added
    kind/bug
    on Aug 19, 2026
  3. added theissue type on Aug 19, 2026
  4. added
    ai-light
    on Aug 26, 2026
  5. Ariznawlll commented on Aug 26, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • commit:5bc051abc44f34ccb6813e2a0bb5585d5a1db67e
    • 部署方式:本地单 CN;Connector/J 8.0.33 server-side PreparedStatement
    • 测试日期:2026-08-26

    原问题验证

    按 Issue 原 SQL 创建 80,000 行数据,复用同一个 com.mysql.cj.jdbc.ServerPreparedStatement,依次绑定 frame bound 0/31/128/512/1024,每档执行 3 次。耗时已不再随 frame width 线性增长:

    Bound 当前 main 3 次耗时
    0 43 / 41 / 41 ms
    31 36 / 36 / 35 ms
    128 36 / 38 / 41 ms
    512 39 / 42 / 37 ms
    1024 46 / 51 / 40 ms

    1024 的结果摘要仍为 count=80000, sum=38224384305, min=1, max=494105,与 Issue oracle 完全一致。

    Happy Path 覆盖

    1. 五档 frame bound 的 count/sum/min/max 均为确定值,prepared statement 可反复复用。
    2. 60 万行尺度控制:bound 31 为 290/275/304 ms,bound 1024 为 286/290/297 ms,两档耗时保持同一量级。
    3. 60 万行结果稳定,测试清理后同连接 SELECT 1 成功。

    Unhappy Path 覆盖

    1. 本问题为性能复杂度缺陷,没有应被拒绝的非法输入;以 60 万行、1024 frame 作为压力边界验证。
    2. 未出现超时、OOM、连接断开或部分结果。

    白盒验证

    window/aggexec 的 bounded sliding SUM、prepared runtime bound、跨输出 batch、分区重置、NULL、INT64、DECIMAL64、取消和非连续输出保护测试连续执行 3 轮通过。

    回归测试

    测试证据

    Connector/J 探针确认 statement class 为 ServerPreparedStatement,80,000 行和 600,000 行所有执行均输出完整摘要;修复后的 1024 frame 相比 Issue 中约 16.4 秒已降至 40–51 ms。

    测试结论

    测试通过。

  6. Ariznawlll commented on Aug 26, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • commit:5bc051abc44f34ccb6813e2a0bb5585d5a1db67e
    • 部署方式:本地单机 MatrixOne,mysql-connector-python 9.7.0 binary prepared statement
    • 测试日期:2026-08-26

    原问题验证

    按 issue 中的数据分布生成 600,000 行,复用同一个 binary prepared statement,分别绑定 31 PRECEDING 和 1024 PRECEDING,每个 bound 执行 3 次。查询均返回精确一致的 count/sum/min/max,宽窗口相对窄窗口的 P50 比率为 0.992,未再随 frame width 线性增长。

    Happy Path 覆盖

    1. 31 PRECEDING:3 次耗时 314.483 / 332.725 / 327.890 ms,P50 327.890 ms;结果为 (600000, 9025830114, 1, 30489)。
    2. 1024 PRECEDING:3 次耗时 325.328 / 320.797 / 328.605 ms,P50 325.328 ms;结果为 (600000, 288797684381, 1, 494105)。
    3. 两组结果均通过独立 Python 滑动窗口 oracle 的精确校验。
    4. 6 次窗口查询完成后,在同一 prepared 连接执行 SELECT 1 成功;测试数据库在退出路径中完成清理。

    Unhappy Path 覆盖

    1. 该问题是合法窗口查询的性能退化,不涉及产品侧非法输入或拒绝语义;回归驱动单测已验证结果摘要不一致时立即失败。
    2. 回归驱动单测已用修复前的耗时样本验证门禁:1024/31 P50 比率超过 3.0 时立即失败,不会把原来的 O(N×W) 行为记录为成功基线。

    白盒验证

    不适用。本次补充的是 binary prepared 协议与 600,000 行规模下的黑盒性能门禁;窗口算法白盒覆盖由 MatrixOne 仓库已有测试负责。

    回归测试

    1. big-data 驱动(目标分支 big_data):https://github.com/matrixorigin/mo-nightly-regression/pull/1509
    2. workflow 接入(目标分支 main):https://github.com/matrixorigin/mo-nightly-regression/pull/1510
    3. workflow 新增 focused scope issue_27352,只运行 33 Issue 27352 Bounded ROWS Window,无需执行整套 1Y 数据准备。
    4. 本问题未加入 motr:小数据无法稳定触发并判断 frame width 的复杂度退化,按覆盖分层放入 big-data 更合适。

    测试证据

    • MatrixOne main commit:5bc051abc44f34ccb6813e2a0bb5585d5a1db67e
    • 驱动提交:af6878e3cff2639ed5acec50ad595dd9a81167af
    • workflow 提交:b1cc5b17a633821e67f0ef848659e09bd232bc6d
    • 本地实测最终门禁:wide_to_narrow_p50_ratio=0.992,prepared_connection_recovery=true,pass=true

    测试结论

    测试通过。

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

Metadata

Metadata

Assignees

Labels

ai-lightkind/bug

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions