Skip to content

[Bug]: Recursive CTE self-reference in a subquery returns an internal planner error #25958

Closed

Description

@Ariznawlll

MatrixOne version

Latest main at 3046f14d94b3f1a25839eeeb65729b64f85ac647.

What happened?

An invalid recursive CTE that references the recursive table again inside a subquery returns an internal Go index-out-of-range error from the query planner instead of a normal SQL validation error.

The failure is deterministic. It was reproduced three consecutive times, and the service remained responsive afterward.

How to reproduce

WITH RECURSIVE r(n) AS (
    SELECT 1
    UNION ALL
    SELECT n + 1
    FROM r
    WHERE EXISTS (SELECT 1 FROM r x)
      AND n < 3
)
SELECT * FROM r;

Actual result

ERROR 20101 (HY000): internal error: panic runtime error: index out of range [0] with length 0
...
github.com/matrixorigin/matrixone/pkg/sql/plan.(*QueryBuilder).remapAllColRefs
    pkg/sql/plan/query_builder.go:1947

Expected result

The statement should be rejected with a regular SQL validation error explaining that the recursive table may not be referenced from a subquery. MySQL 8.0.45 reports error 3577 for this statement and does not expose an internal runtime stack.

Activity

  1. added this to the MO-202607 milestone on Jul 21, 2026
  2. added
    kind/bug
    on Jul 21, 2026
  3. added theissue type on Jul 21, 2026
  4. Ariznawlll commented on Aug 15, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • commit:a2f89230085a0f6650ec7bb7017ba6b8a97c9efc
    • 部署方式:本地单节点 LOG/TN/CN,临时隔离目录,未涉及权限或租户边界
    • 测试日期:2026-08-15

    原问题验证

    在递归成员的顶层 FROM 已经引用递归表的情况下,再把第二次自引用藏进标量子查询,语句没有被解析拒绝,也不返回结果,直到客户端超时:

    WITH RECURSIVE q(n) AS (
        SELECT 1
        UNION ALL
        SELECT (
            SELECT q2.n + 1
            FROM q q2
            WHERE q2.n = q.n
        )
        FROM q
        WHERE n < 3
    )
    SELECT n FROM q;
    

    测试证据

    使用 JDBC Statement#setQueryTimeout(5),同一隔离实例连续 3 次均为:

    ERR Statement cancelled due to timeout or client request
    ERR Statement cancelled due to timeout or client request
    ERR Statement cancelled due to timeout or client request
    

    这不是客户端或数据 fixture 问题:普通递归控制

    WITH RECURSIVE q(n) AS (
        SELECT 1 UNION ALL SELECT n + 1 FROM q WHERE n < 3
    )
    SELECT n FROM q;
    

    稳定返回 1;2;3;取消异常语句后,控制查询仍可执行。

    官方已有的递归子查询负例(递归表只出现在子查询中)返回正常解析错误;本例只是将第二个引用放进标量子查询,表现为不受控等待。

    本地终端证据:3 次最小复现均输出 ERR Statement cancelled due to timeout or client request;控制查询输出 BASELINE 1;2;3;完整探针 3 轮均输出 SUMMARY pass=66 fail=0 expected_errors=7。

    Happy Path 覆盖

    作用域遮蔽、嵌套 CTE、相关子查询、多次引用一致性、LIMIT、窗口、嵌套聚合、NULL/空集、PreparedStatement、结果元数据、CTE + INSERT/UPDATE/DELETE、重复键失败原子性、递归 UNION DISTINCT/ALL、重复边、多分支、NULL 可空性、25 层 CTE、20,000 行表函数、300 层递归均通过。

    完整探针在同一 main 构建上连续 3 轮均为 pass=66 fail=0 expected_errors=7;并发 CTE UPDATE 每轮 10 次,3 轮共 30 次,均无丢失更新。

    Unhappy Path 覆盖

    递归循环深度限制、递归成员 ORDER BY、外连接、子查询自引用、外层 FOR UPDATE、混合 UNION 模式和重复键插入均已执行;除本例外均得到预期错误且无部分写入。

    白盒验证

    pkg/sql/plan/query_builder.go 的递归检查只沿递归成员顶层 FROM 计数(checkRecursiveCTE / checkRecursiveTable);后续绑定阶段又把标量子查询中的 q2 映射成同一个递归扫描节点。这里是源码定位,根因仍需修复后由回归测试确认。

    回归测试

    建议在本 issue 的回归用例中追加本 SQL,要求:在有限时间内返回普通 SQL 校验错误,且控制递归查询仍返回 1;2;3。该场景属于稳定公共 SQL,回归路由为 motr 的 issue regression suite。

    测试结论

    测试不通过。

  5. Ariznawlll commented on Aug 15, 2026

    @Ariznawlll
    CollaboratorAuthor

    追加边界验证

    在同一 main commit a2f89230085a0f6650ec7bb7017ba6b8a97c9efc 上,继续验证了第二种隐藏重复引用:

    WITH RECURSIVE q(n) AS (
        SELECT 1
        UNION ALL
        SELECT n + 1
        FROM q
        WHERE EXISTS (SELECT 1 FROM q x)
          AND n < 3
    )
    SELECT n FROM q;
    

    该形态也连续 3/3 在 3 秒 JDBC 超时后才返回 Statement cancelled due to timeout or client request。标量子查询版本同样 3/3 超时。

    对照结果:

    • 递归表只出现在子查询中的官方负例:立即返回“recursive table must be referenced only once, and not in any subquery”;
    • FROM (SELECT n FROM q) 派生表负例:立即返回同一解析错误;
    • 只包含相关表达式、没有第二个 q 引用的合法递归 CTE:稳定返回 1;2;3;
    • 上述超时查询取消后,事务可见性、回滚和普通 CTE 查询仍正常。

    V3 深探针连续 3 轮均为 pass=18 fail=0 expected_errors=4;其中两个隐藏重复引用形态每轮各执行一次超时验证。

  6. added
    severity/s0
    and removed on Aug 19, 2026
  7. Ariznawlll commented on Aug 23, 2026

    @Ariznawlll
    CollaboratorAuthor

    已在最新 main(6ff9be9646a6ac43e05ba3063de27b2248bbac62)验证修复。

    验证内容:

    • 递归 CTE 在 EXISTS 子查询中再次引用递归表,返回明确的 1064 parser error,不再出现内部 planner error。
    • 递归 CTE 在标量子查询中再次引用递归表,同样返回预期错误。
    • 正常的顶层递归、仅相关但不引入第二个递归源的子查询均可返回 1,2,3;错误后会话仍可正常执行。

    已补充 MOTR 回归:matrixorigin/motr#117(issue_25958_recursive_cte_subquery_rejection),本地连续 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