You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Bug]: Recursive CTE self-reference in a subquery returns an internal planner error #25958
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 (
SELECT1UNION ALLSELECT n +1FROM r
WHERE EXISTS (SELECT1FROM 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.
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;
Activity
Ariznawlll commentedon Aug 15, 2026
测试版本
maina2f89230085a0f6650ec7bb7017ba6b8a97c9efc原问题验证
在递归成员的顶层
FROM已经引用递归表的情况下,再把第二次自引用藏进标量子查询,语句没有被解析拒绝,也不返回结果,直到客户端超时:测试证据
使用 JDBC
Statement#setQueryTimeout(5),同一隔离实例连续 3 次均为:这不是客户端或数据 fixture 问题:普通递归控制
稳定返回
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。测试结论
测试不通过。
Ariznawlll commentedon Aug 15, 2026
追加边界验证
在同一 main commit
a2f89230085a0f6650ec7bb7017ba6b8a97c9efc上,继续验证了第二种隐藏重复引用:该形态也连续 3/3 在 3 秒 JDBC 超时后才返回
Statement cancelled due to timeout or client request。标量子查询版本同样 3/3 超时。对照结果:
FROM (SELECT n FROM q)派生表负例:立即返回同一解析错误;q引用的合法递归 CTE:稳定返回1;2;3;V3 深探针连续 3 轮均为
pass=18 fail=0 expected_errors=4;其中两个隐藏重复引用形态每轮各执行一次超时验证。Ariznawlll commentedon Aug 23, 2026
已在最新 main(6ff9be9646a6ac43e05ba3063de27b2248bbac62)验证修复。
验证内容:
已补充 MOTR 回归:matrixorigin/motr#117(issue_25958_recursive_cte_subquery_rejection),本地连续 3 轮执行通过。