Skip to content

[Bug]: correlated ORDER BY LIMIT returns NULL for a matching row #32441

Done

Description

@XuPeng-SH

Is there an existing issue for the same bug?

  • I have checked the existing issues.

Branch Name

4.2-dev (the container reports HEAD).

Commit ID

71498157cc21f833b8e717f45d50d1f926be3134

Other Environment Information

  • MatrixOne SQL version: 8.0.30-MatrixOne-v4.2.6
  • Docker image: matrixorigin/matrixone@sha256:14a5c1e88bf1d60b78d47dcc550e724f65a81dc47c07331cf8b6286c12fdd535
  • Linux arm64 container on macOS arm64

Actual Behavior

A correlated scalar subquery with ORDER BY ... DESC LIMIT 1 silently returns NULL for a parent that has a matching child. The query succeeds but produces an incorrect result:

0    NULL
2    2

The result reproduced 3/3 times in a fresh database.

Expected Behavior

The first parent matches a child with event_idx = 1, so the result should be:

0    1
2    2

Steps to Reproduce

Run this in an empty test database:

CREATE TABLE parent (id INT, ceiling BIGINT);
CREATE TABLE child (unused INT, parent_id INT, event_idx BIGINT);
INSERT INTO parent VALUES (0, 2), (2, 2);
INSERT INTO child VALUES (0, 0, 1), (0, 2, 1), (0, 2, 2);

SELECT p.id,
       (SELECT c.event_idx
          FROM child AS c
         WHERE c.parent_id = p.id
           AND c.event_idx > 0
           AND c.event_idx <= p.ceiling
         ORDER BY c.event_idx DESC
         LIMIT 1)
FROM parent AS p
ORDER BY p.id;

Additional information

A non-correlated LEFT JOIN grouped by p.id with MAX(c.event_idx) returns the expected result on this fixture. No indexes are needed. All reproduction data is synthetic; the temporary database was removed after testing.

Related query shape: #32440 reports a panic for a correlated MAX subquery on the same build. A common underlying cause has not been established.

Activity

  1. added
    kind/bug
    on Nov 10, 2026
  2. added a commit that references this issue on Nov 10, 2026
    c0532d1
  3. Ariznawlll commented on Nov 30, 2026

    @Ariznawlll
    Collaborator

    结论

    测试通过。原始相关 ORDER BY DESC LIMIT 1 的错误 NULL 结果已不再复现,原始数据及扩展精确行集均通过。

    关键证据

    版本与环境

    • 本次验收分支:4.2-dev;本地和 Linux CI 实际 MO 完整 SHA 均为 df65fa5b21d2da5bbca119236bbf7829f88d78dd,实际连接两个 CN 核对 git_version()。这是 MO SHA,不是 MOTR commit。
    • 使用独立数据目录和端口,测试数据均为合成数据;没有修改产品实现。MOTR 回归只提交到 MOTR 4.2-dev,没有向 MatrixOne 主库提测试 PR。

    复现步骤、实际输出与扩展结果

    1. 原样建立工单中的 parent、child 表和五条合成记录,不增加索引,执行相关子查询 ORDER BY event_idx DESC LIMIT 1。两个 CN 各连续三轮均返回:
    parent.id  result
    0          1
    2          2
    

    与独立的 LEFT JOIN / MAX 对照一致,没有原来的 0 -> NULL 错误结果。

    1. 扩展为七个父行,覆盖父键 0、负数、无子行、ceiling 为 0/NULL、重复子值、NULL 子值、超上界子值及 NULL 关联键。使用 Python 独立计算合法候选集合的 min/max,核对 ASC/DESC 与 LIMIT 0/1 的完整行集。不能仅以查询不报错判定通过。
    2. 普通 SQL 在两个 CN、刷盘前后、增加 (parent_id,event_idx) 索引前后分别执行;SQL PREPARE/EXECUTE 的 LIMIT 1 同样核对完整结果。删除父键 0 的子行后事务内结果为 NULL,回滚后恢复原来的值,再从两个 CN 复核。
    3. 额外边界说明:预编译的相关子查询 LIMIT 0 当前明确不支持,实际报 20102 prepared correlated subquery with LIMIT 0 is not yet implemented,回归保留该特定拒绝断言。普通 SQL 的 LIMIT 0 正确返回 NULL。没有将这一额外形态写成“预编译支持已通过”;本 Issue 原始 LIMIT 1 错误结果已不再出现。
    4. 本地原始步骤两个 CN 各三轮通过;扩展脚本本地三轮通过;Linux MOTR 中同一扩展脚本三轮通过。独立对照和 SQL、预编译、刷盘、索引、回滚边界均使用精确结果断言。

    回归与 CI

    待解决点

    没有找到可确认与本工单一一对应的修复 PR,因此不猜测修复归因;依据实际 4.2-dev 运行版本和原始复现结果验收。预编译相关 LIMIT 0 属于上述明确拒绝的额外边界,不是本 Issue 报告的 LIMIT 1 错误结果,也不宣称它已经支持。

    本 Issue 原始验收范围无未通过项。回归已提交,按用户允许在回归 PR 合入前关闭的流程处理。

    最终结论:测试通过。原始复现、上述扩展与对应分支 MOTR/CI 已完成;本 Issue 原始验收范围内未通过项:无。按授权关闭 #32441。

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

Metadata

Metadata

Assignees

Labels

kind/bugneeds-triage

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions