Skip to content

[Bug]: Prepared LAG/LEAD offset parameter silently returns NULL for every row #26865

Closed

Description

@Ariznawlll

Is there an existing issue for the same bug?

  • I have checked open and closed issues and pull requests for prepared LAG/LEAD offsets, parameter markers, and all-NULL window results. No exact duplicate was found.

Branch Name

main

Commit ID

1d00cca77012a2fac20c94bbbd170579e715d217

Other Environment Information

  • MatrixOne: 8.0.30-MatrixOne-v1.3.0
  • Deployment: local mo-service, 2 CN / 1 TN / 1 Log service / Proxy
  • Both SQL PREPARE and MySQL binary-protocol prepared statements were tested

Actual Behavior

When the second argument of LAG or LEAD is a bare prepared parameter marker, MatrixOne accepts and executes the query but silently returns NULL for every row.

With this input:

id:g:v
1:1:10
2:1:20
3:1:30
4:2:40

The parameterized queries both return:

1:NULL
2:NULL
3:NULL
4:NULL

The equivalent literal and explicitly typed parameter controls return the correct results:

LAG(v, 1) / LAG(v, CAST(? AS SIGNED)):
1:NULL
2:10
3:20
4:NULL

LEAD(v, 1):
1:20
2:30
3:NULL
4:NULL

The wrong result reproduced 3/3 times with binary-protocol LAG and LEAD, and 3/3 times with SQL PREPARE/EXECUTE. Literal and cast controls passed in every run. The full matrix was rerun after main advanced from ec06a68b8 to the commit above and still reproduced 3/3.

Expected Behavior

Binding offset 1 through a positional parameter marker should produce the same result as literal offset 1, without requiring an explicit cast. MySQL documents a positional parameter marker as a valid form for the LAG/LEAD offset.

Steps to Reproduce

DROP DATABASE IF EXISTS ps_lag_offset;
CREATE DATABASE ps_lag_offset;
USE ps_lag_offset;

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

-- Correct literal control.
SELECT id, LAG(v,1) OVER(PARTITION BY g ORDER BY id)
FROM t ORDER BY id;

-- Incorrect: every LAG result is NULL.
PREPARE lag_param FROM
  'SELECT id,LAG(v,?) OVER(PARTITION BY g ORDER BY id) FROM t ORDER BY id';
SET @offset=1;
EXECUTE lag_param USING @offset;

-- Incorrect: every LEAD result is NULL.
PREPARE lead_param FROM
  'SELECT id,LEAD(v,?) OVER(PARTITION BY g ORDER BY id) FROM t ORDER BY id';
EXECUTE lead_param USING @offset;

-- Correct typed-parameter control.
PREPARE lag_cast FROM
  'SELECT id,LAG(v,CAST(? AS SIGNED)) OVER(PARTITION BY g ORDER BY id) FROM t ORDER BY id';
EXECUTE lag_cast USING @offset;

The same behavior is observable with protocol prepared statements such as Go database/sql using db.Prepare followed by stmt.Query(1).

Additional information

This appears to be missing numeric type inference for the offset argument:

  • pkg/sql/plan/function/list_window.go accepts one to three arguments for LAG/LEAD without constraining the second argument to an integer type.
  • pkg/sql/plan/base_binder.go has special handling for LAG/LEAD, but only casts the third/default argument to the first/value argument's type.
  • pkg/sql/colexec/window/window.go evaluates the offset and getInt64FromVec accepts only integer vectors. The bare marker reaches this path with an unsupported type, so (offset, ok) becomes false and the executor appends the default/NULL for every row.
  • CAST(? AS SIGNED) and ? + 0 both work, further isolating the problem to offset parameter type inference rather than window partitioning or offset execution.

Related but not duplicate: #25993 covered parameter markers in window frame bounds; #26019 covered numeric inference for prepared SUM/AVG; #23969 covered literal LAG/LEAD execution.

MySQL reference: https://dev.mysql.com/doc/refman/8.0/en/window-function-descriptions.html

Activity

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

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    最新 main:484e1969531e7fbf7219b20e34d7ef23e110560b;本地隔离 DISK-V2 单 CN + TN + LogService。

    原问题验证

    原问题是预处理语句中的 LAG/LEAD(?, ...) offset 推断错误,导致结果全部为 NULL。最新 main 上同一窗口的 literal、SQL PREPARE 和真实 MySQL binary protocol PREPARE 均返回正确结果。

    Happy Path 覆盖

    • LAG(v,1):1:NULL, 2:10, 3:20, 4:NULL。
    • LEAD(v,1):1:20, 2:30, 3:NULL, 4:NULL。
    • SQL PREPARE 的 ? offset、CAST(? AS SIGNED) offset。
    • go-sql-driver/mysql 真实 COM_STMT_PREPARE 的 LAG/LEAD offset=1。

    Unhappy Path 覆盖

    覆盖分区边界 NULL 结果、同一连接在预处理执行后的 SELECT 1;未发现错误结果、连接失效或源数据变化。非法 offset 不属于该 issue 的原始关闭门槛。

    白盒验证

    修复提交 484e196 已包含在测试版本中。

    回归测试

    已加入 MOTR PR #113,提交 69242f8:SQL case issue_26865_prepared_lag_lead 3 轮 54 条语句通过,binary case issue_26865_prepared_lag_lead.sh 通过。

    测试证据

    SQL case 每轮 literal/prepared/cast 三组结果一致;binary 脚本输出 PASS: literal and binary prepared LAG/LEAD offsets match 和 SUMMARY: all assertions passed。

    测试结论

    测试通过。

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

Metadata

Metadata

Assignees

Labels

ai-easykind/bugphase/testing

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions