Skip to content

[Bug]: Prepared window frame rejects parameter markers for PRECEDING and FOLLOWING #25993

Closed

Description

@Ariznawlll

Is there an existing issue for the same bug?

  • I have checked the existing issues.

Searched for prepared window-frame parameter markers and ? PRECEDING / ? FOLLOWING. No matching issue was found.

Branch Name

main

Commit ID

c0486a2446c84c6dd6bf142494fe85243877ddc5

Other Environment Information

  • MatrixOne: 8.0.30-MatrixOne-v1.3.0
  • MySQL comparison: 8.0.45
  • SQL-level PREPARE / EXECUTE through the mysql client

Actual Behavior

MatrixOne supports a prepared window query when the ROWS frame bounds are integer literals, but rejects the equivalent MySQL-compatible parameterized form during PREPARE:

ERROR 1105 (HY000): ... syntax error ... near " ? PRECEDING AND ? FOLLOWING) ..."

The literal control returns the expected running-window sums in both MatrixOne and MySQL:

1  30
2  60
3  50

MySQL 8.0.45 returns the same result for the parameterized frame. MatrixOne reproduced the prepare-time rejection 3/3 times.

Expected Behavior

Parameter markers should be accepted for ROWS frame bounds inside a prepared statement and bound at EXECUTE time, consistent with MySQL.

Steps to Reproduce

DROP DATABASE IF EXISTS ps_window_param;
CREATE DATABASE ps_window_param;
USE ps_window_param;
CREATE TABLE e(id INT PRIMARY KEY,n INT);
INSERT INTO e VALUES(1,10),(2,20),(3,30);

-- Literal prepared control succeeds.
PREPARE literal_frame FROM
  'SELECT id,SUM(n) OVER (ORDER BY id ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) FROM e ORDER BY id';
EXECUTE literal_frame;

-- Equivalent parameterized form is rejected by MatrixOne.
PREPARE parameter_frame FROM
  'SELECT id,SUM(n) OVER (ORDER BY id ROWS BETWEEN ? PRECEDING AND ? FOLLOWING) FROM e ORDER BY id';
SET @before=1,@after=1;
EXECUTE parameter_frame USING @before,@after;

Additional Information

The MySQL grammar in pkg/sql/parsers/dialect/mysql/mysql_sql.y restricts frame_bound and frame_bound_start to num_literal or interval_expr. A parameter marker is therefore rejected before it can be represented in the prepared plan. The window binder also currently evaluates finite frame bounds as constants while building the plan.

Activity

  1. added this to the MO-202607 milestone on Jul 22, 2026
  2. added
    kind/bug
    on Jul 22, 2026
  3. added theissue type on Jul 22, 2026
  4. self-assigned this
    on Jul 22, 2026
  5. added
    severity/s0
    and removed on Jul 22, 2026
  6. LeftHandCold commented on Jul 22, 2026

    @LeftHandCold
    Collaborator

    Confirmed on current-main baseline e15943929885c2c5c83daed90f0f8caeb9a1f0d0. This is a real compatibility gap, but it is not only a parser omission.

    The grammar rejects parameter markers in finite frame bounds; the planner then eagerly evaluates frame bounds as constants; the window executor assumes a literal unsigned-value carrier; and prepared-parameter collection/reset does not traverse the window specification. A grammar-only change would merely move the failure downstream and could make cached executions reuse the wrong bound.

    No partial PR proposed. Complete support needs parser acceptance, WindowSpec parameter discovery/reset, and once-per-EXECUTE frame evaluation and validation with a stable cache/reuse contract. Tests must include repeated executions with different bounds, zero/negative/NULL/fractional/overflow values, wrong argument counts, ROWS/RANGE, and remote execution.

  7. LeftHandCold commented on Jul 23, 2026

    @LeftHandCold
    Collaborator

    Implemented against current main (0d7eeb38b4) in ready-for-review PR #26094: #26094

    The fix covers parser/binder preservation, prepared-parameter traversal and alias-safe renumbering, per-Prepare immutable ROWS-bound materialization, Reset/Free lifecycle, invalid-value handling, process-codec coverage, and a distributed SQL regression. Dynamic RANGE and interval bounds remain explicitly unsupported. Local affected-package, race, build, and vet checks passed; CI has started and is not being waited on here.

  8. LeftHandCold commented on Jul 24, 2026

    @LeftHandCold
    Collaborator

    fixed

  9. Ariznawlll commented on Jul 27, 2026

    @Ariznawlll
    CollaboratorAuthor

    最新 main 验证结果:通过

    测试版本

    • MatrixOne main@8c9bb66898afa74f8b231e1915d99489ae8a0da7
    • 关联修复:PR #26094

    测试 SQL

    PREPARE s FROM
    'SELECT id,
            SUM(n) OVER(
              ORDER BY id
              ROWS BETWEEN ? PRECEDING AND ? FOLLOWING
            ) AS total
     FROM window_src
     ORDER BY id';

    在同一个 prepared statement 上依次使用 (1,1)、(0,2)、(2,0) 三组 frame bounds,检查重复执行是否会错误复用上一次参数。

    实际结果

    • (1,1) 返回窗口和:30, 60, 90, 70。
    • (0,2) 返回:60, 90, 70, 40。
    • (2,0) 返回:10, 30, 60, 90。
    • 三组结果均与对应 ROWS frame 语义一致,未出现旧参数缓存。
    • 负数 bound 返回正常错误:ERROR 20203: invalid argument cast to uint64。
    • NULL bound 返回正常错误:ERROR 20301: window frame bound cannot be NULL。
    • 完整正向黑盒流程独立执行 3 轮,3/3 通过。

    结论

    prepared ROWS frame 已支持参数标记、重复换参和非法值校验,原 prepare-time 语法拒绝问题已修复。

  10. Ariznawlll commented on Jul 27, 2026

    @Ariznawlll
    CollaboratorAuthor

    motr 黑盒回归

    该问题已加入 matrixorigin/motr#66。

    在 MatrixOne main@8c9bb66898afa74f8b231e1915d99489ae8a0da7 上连续执行 3 轮,结果稳定通过。

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

Metadata

Metadata

Assignees

Labels

kind/bugseverity/s0

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions