Skip to content

[Bug]: LAST_VALUE and NTH_VALUE use full partition when ordered frame is omitted #27341

Closed

Description

@Ariznawlll

Is there an existing issue for the same bug?

  • I have checked the existing issues.

Branch Name

main

Commit ID

64fa15e

Other Environment Information

- MatrixOne: local single-CN deployment built from the commit above
- MySQL control: MySQL 8.0
- Client: MySQL Connector/J 8.0.33
- Verification: 3 independent rounds

Actual Behavior

When an ORDER BY is present and the frame clause is omitted, MatrixOne evaluates LAST_VALUE and NTH_VALUE over the entire partition. This is equivalent to ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING, rather than the default ordered frame.

MatrixOne result:

1 | 40 | 30
2 | 40 | 30
3 | 40 | 30
4 | 40 | 30

The same query returns the expected peer-aware running frame in MySQL:

1 | 20 | NULL
2 | 20 | NULL
3 | 30 | 30
4 | 40 | 30

An explicit RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW produces the expected MySQL-compatible result in MatrixOne. This isolates the problem to the omitted-frame path rather than value-function execution itself.

Expected Behavior

For an ordered window with no explicit frame, frame-sensitive value functions should use RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, consistent with MySQL's documented default-frame behavior.

Steps to Reproduce

DROP DATABASE IF EXISTS window_default_frame_repro;
CREATE DATABASE window_default_frame_repro;
USE window_default_frame_repro;

CREATE TABLE t(id INT PRIMARY KEY, o INT, v INT);
INSERT INTO t VALUES
  (1, 1, 10),
  (2, 1, 20),
  (3, 2, 30),
  (4, 3, 40);

-- Wrong in MatrixOne: omitted frame is expanded to the full partition.
SELECT id,
       LAST_VALUE(v) OVER (ORDER BY o) AS lv,
       NTH_VALUE(v, 3) OVER (ORDER BY o) AS nv
FROM t
ORDER BY id;

-- Control: correct in MatrixOne and MySQL.
SELECT id,
       LAST_VALUE(v) OVER (
         ORDER BY o RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
       ) AS lv,
       NTH_VALUE(v, 3) OVER (
         ORDER BY o RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
       ) AS nv
FROM t
ORDER BY id;

Stability, controls, and recovery

  • Reproduced 3/3 on the commit above.
  • MySQL returned the expected result 3/3.
  • The explicit-frame control returned the expected result 3/3 in both systems.
  • The query is read-only and leaves the connection usable.

Root-cause evidence

pkg/sql/plan/window_binder.go rewrites every value window function without an explicit frame to ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING. That override is appropriate for functions such as LAG/LEAD, which operate independently of a frame, but changes the semantics of frame-sensitive LAST_VALUE and NTH_VALUE.

Suggested regression placement

Add a BVT under test/distributed/cases/window/ covering omitted versus explicit default frames, peer rows, and both LAST_VALUE and NTH_VALUE.

Related issues

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-easy
    on Sep 4, 2026
  5. Ariznawlll commented on Sep 5, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • commit:fa829e5c505520e4bd429d283c78157781b4de72
    • 部署方式:129 TKE,3 CN / 1 TN / 3 LogService / 2 Proxy
    • 测试日期:2026-09-05

    原问题验证

    在 3 轮独立执行中复测 LAST_VALUE / NTH_VALUE 默认 window frame。原有 peer-group 语义、显式 RANGE frame 和完整分区对照均返回预期值。

    Happy Path 覆盖

    1. 默认 frame、显式 RANGE、完整分区和降序窗口均正确。
    2. NULL 排序、多个 partition、边界 NTH_VALUE 和 SQL PREPARE/EXECUTE 重用均正确。

    Unhappy Path 覆盖

    1. 非法列名与非法 frame 被拒绝;随后查询仍可用,数据未变化。

    白盒验证

    不适用:该问题为公开 SQL window 语义。

    回归测试

    已加入 MOTR:suites/14_issue_regression/t/issue_27341_default_value_frame.test。PR:https://github.com/matrixorigin/motr/pull/145 。该仓库当前没有可触发的 PR 回归 workflow,状态检查为空;回归文件已完成本地语法和相关 suite 校验。

    测试证据

    129 TKE 实测 3/3,覆盖 97 项断言;测试数据库已清理。

    测试结论

    测试通过。

  6. daviszhen commented on Oct 16, 2026

    @daviszhen
    Contributor

    v4.2.5 release-specific reproduction (arm64)

    Independent replay on the official matrixorigin/matrixone:latest image — arm64/linux, VERSION() = 8.0.30-MatrixOne-v4.2.5, single-node standalone, Apple M5 host, mysql 8.4 CLI. This comment does not assert anything about current main; it only records that the behaviour is still present in this release image.

    Still reproducible for LAST_VALUE with an omitted frame clause. Data (id, amt) = 1..4 → 10.50, 20.25, 30.75, 40.00:

    SELECT id, last_value(amt) OVER (ORDER BY id) lv FROM t ORDER BY id;
    -- MO v4.2.5:    10.50 -> 50.10, 20.25 -> 50.10, 30.75 -> 50.10, 40.00 -> 50.10   (whole-partition last value)
    -- MySQL 8.0.46: 10.50 -> 10.50, 20.25 -> 20.25, 30.75 -> 30.75, 40.00 -> 40.00   (current row)

    Confirmed working on this release, which narrows the scope to the default frame of LAST_VALUE only:

    • last_value(amt) OVER (ORDER BY id ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) → matches MySQL ✅
    • first_value / nth_value with explicit frames → matches MySQL ✅
    • max(amt) OVER (ORDER BY id) and count(*) OVER (ORDER BY id) without a frame clause → matches MySQL ✅

    Workaround: write the frame clause explicitly. Impact matters because "compare with the latest value in the group" is a very common reporting pattern and currently returns silently wrong values.

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

Metadata

Metadata

Assignees

Labels

ai-easykind/bug

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions