Skip to content

[Bug]: Recursive CTE rejects UNION DISTINCT with a single self-reference #25955

Closed

Description

@Ariznawlll

MatrixOne version

Latest main at 3046f14d94b3f1a25839eeeb65729b64f85ac647.

What happened?

A recursive CTE with a single recursive-table reference is rejected when the set operator is UNION or UNION DISTINCT. The error claims that the recursive table is referenced more than once, although it appears exactly once.

The equivalent UNION ALL statement succeeds, and MySQL 8.0.45 accepts both UNION and UNION DISTINCT variants.

How to reproduce

WITH RECURSIVE r(n) AS (
    SELECT 1
    UNION DISTINCT
    SELECT n + 1 FROM r WHERE n < 10
)
SELECT COUNT(*), SUM(n), MAX(n) FROM r;

Actual result

ERROR 1064 (HY000): SQL parser error: In recursive query block of Recursive Common Table Expression r, the recursive table must be referenced only once, and not in any subquery

Expected result

The statement should succeed and return:

10  55  10

Additional notes

  • Replacing UNION DISTINCT with UNION produces the same failure.
  • Replacing it with UNION ALL succeeds.
  • The MySQL grammar in MatrixOne parses UNION DISTINCT; the failure occurs later while binding the recursive CTE.

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. added
    severity/s0
    and removed on Jul 31, 2026
  5. assigned and unassigned on Jul 31, 2026
  6. Ariznawlll commented on Aug 5, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • commit:e31ee06042bc708ac5620a579715e07ec31c21b6
    • 部署方式:本地单 CN,默认配置
    • 测试日期:2026-08-05

    原问题验证

    递归 CTE 使用 UNION DISTINCT 的原始能力已恢复。数列用例正确返回 10 行、SUM=55, MAX=10,没有重复或提前终止。

    Happy Path 覆盖

    1. UNION DISTINCT 递归数列返回 10 行、SUM=55, MAX=10。
    2. UNION 同义写法得到相同结果。
    3. 可产生稳定重复值的递归在去重后终止,结果仅为 1,未发生死循环。
    4. UNION ALL 对照路径保持原有语义。

    Unhappy Path 覆盖

    1. 同一个递归 CTE 中非法混用不一致的 UNION 模式被正确拒绝;错误后服务和会话可继续使用,无 hang 或 panic。

    白盒验证

    Focused planner UT TestRecursiveUnionDistinctPlan 与 TestRecursiveUnionRejectsMixedModes 均通过。

    回归测试

    测试证据

    • motr commit:b770136206c88435a51599e4b317e522eddfd085。
    • 修复提交:15a2d07c687a62a0984cf62846b092912a28c0f1。

    测试结论

    测试通过。

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