Skip to content

[Bug]: Recursive CTE with multiple recursive members omits later-generation rows #32612

Closed

Description

@Ariznawlll

Description

A valid recursive CTE with multiple recursive UNION ALL members silently omits a row when a later recursive member consumes a row produced in the preceding recursive round. The query succeeds, but MatrixOne returns an incomplete result compared with MySQL 8.0.45.

Environment

  • Branch: main
  • Commit: bbfc12bab4f410c26dbf06aa694acba51aba1734
  • Deployment: isolated local MatrixOne service with one LogService, one TN, and one CN listener (127.0.0.1:16111). SELECT git_version() returned bbfc12bab4.
  • Oracle: MySQL 8.0.45.

Steps to reproduce

Run this SQL; it needs no tables or fixture data:

WITH RECURSIVE r(step, v) AS (
  SELECT 0, 10
  UNION ALL
  SELECT 1, v + 1 FROM r WHERE step = 0
  UNION ALL
  SELECT 2, v + 3 FROM r WHERE step = 1
)
SELECT step, v FROM r ORDER BY step;

Actual behavior

MatrixOne returns only:

(0, 10)
(1, 11)

The statement succeeds, but (2, 14) is missing. This was reproduced 3/3 on the commit above. Swapping the order of the two recursive members still reproduces the missing row 3/3.

Expected behavior

MySQL 8.0.45 returns all three rows, on 3/3 executions:

(0, 10)
(1, 11)
(2, 14)

MatrixOne should also return (2, 14), because the second recursive member matches the row (1, 11) produced by the prior recursive round.

Stability and controls

  • The multi-member reproducer: MySQL 3/3 returns 3 rows; MatrixOne 3/3 returns 2 rows.
  • A logically equivalent single-recursive-member chain returns all 3 rows on both engines in 3/3 executions.
  • A two-member control where both members consume the anchor/frontier returns all expected rows on both engines in 3/3 executions.
  • No table or persistent data is changed by these read-only queries.

Evidence

The row sets above are from direct executions against MySQL 8.0.45 and the isolated service reporting git_version() = bbfc12bab4. The wrong result was stable with the original and member-order-swapped forms; the controls remained correct.

Code analysis

The current planner creates a recursive source step for each member in pkg/sql/plan/query_builder.go:6063-6157. Runtime frontier scheduling is implemented in pkg/sql/colexec/dependent/recursive.go:135-179, with recursive-member selection at :157 and the next-round reset at :179; MergeCTE consumes and caches recursive batches in pkg/sql/colexec/mergecte/mergecte.go:111-166. These are the first paths to inspect for the missing later-generation row. The SQL-level result mismatch is confirmed; the precise root cause is not asserted here.

Regression coverage

Add a runtime SQL regression with two recursive members where the later member can only produce a row from the preceding recursive round. Keep controls for the equivalent single-member chain and for two members that both consume the same frontier; assert the full ordered row set.

Related

This is distinct from #32600 and #32601: those report invalid recursive CTE shapes that terminate the service, while this valid anchored CTE succeeds and returns incomplete data.

Activity

  1. added this to the MO-202611 milestone on Nov 12, 2026
  2. added
    kind/bug
    on Nov 12, 2026
  3. added theissue type on Nov 12, 2026
  4. added a commit that references this issue on Nov 17, 2026
    43addc1
  5. XuPeng-SH commented on Nov 17, 2026

    @XuPeng-SH
    Contributor

    修复已合入 main,转交 QA 验证,不代表验收通过或关闭。

    关联 PR:#32828;merge SHA:43addc16ba30c3e5a0feb11447078092a5fc3953(2026-11-17 08:23 UTC),PR head:a05ccabc2682f69c5343b61af82f2e6bcf3e9c4c。

    修复范围:recursive member 即使当前 frontier 为空也继续转发 round marker,由共享 recursive sink 在全局 post-DISTINCT frontier 为空时终止;包含 recursive scan 的 join 避开可能让 marker 越过缓冲数据的 shuffle,保留 broadcast 路径。

    已有验证证据:PR 作者报告 dispatch/planner 全量 race、嵌入集群取消及 prepared statement 复用回归、2 CN 上 mo-tester 分布式用例 11/11 两轮通过,以及 vet/molint/增量 lint。review 对 74545ab8a1e547bc251d1d1ea95028b89cb7c684 的记录为 approve:6 个定向回归、5 个相关包普通测试,dispatch 回归各 100 次,取消/复用 2 次;两个 CN coordinator 各执行 11 条维护 SQL 并断言 6 组精确结果,base-dispatch 负对照按预期失败。首次 planner race 超过 240 秒,未改包后在 600 秒界限内通过。这些是已记录的历史验证,本次仅核对正文、完整评论/review 与补丁,未重新跑测试,不能据此声称最终 merge SHA 已独立复测。

    QA 边界:复验原始用例完整返回 (0,10),(1,11),(2,14),并验证成员换序、单成员链/共同消费 frontier 对照、多轮空成员、UNION DISTINCT 收敛;同时覆盖 #32799 跨代消费不挂起、recursive table join、LIMIT、取消和 prepared statement 复用。递归 join 使用 broadcast 可能增加 build-side 内存,不具备 shuffle-only spill/retry;大 build、内存压力与性能未由上述测试证明,应单独验证,不作性能提升承诺。无已核实当前反例或未完成开发 follow-up,交接 Ariznawlll 继续验收。

  6. Ariznawlll commented on Nov 17, 2026

    @Ariznawlll
    CollaboratorAuthor

    Still reproducing on latest main tip (919c9fc32f) — re-verified 2026-11-17.

    Ran the exact repro on a self-built main@919c9fc32f instance (isolated ports; git_version() confirmed):

    WITH RECURSIVE r(step, v) AS (
      SELECT 0, 10
      UNION ALL SELECT 1, v + 1 FROM r WHERE step = 0
      UNION ALL SELECT 2, v + 3 FROM r WHERE step = 1
    )
    SELECT step, v FROM r ORDER BY step;
    

    Actual (3/3): returns only (0,10), (1,11) — row (2,14) is still missing. Member-order swapped variant also still loses the row (3/3).

    Also re-verified on the 4.2-dev branch tip (e8fd84b331f): identical behavior — (2,14) missing (3/3). So the multi-member later-generation consumption defect is present on both current branch tips; the fix has not landed on either branch yet.

  7. Ariznawlll commented on Nov 18, 2026

    @Ariznawlll
    CollaboratorAuthor

    验收结论:main 修复有效;4.2-dev 仍缺后续代行,本 issue 保持 OPEN

    本轮使用包含 #32828 的新 main 实例重新验证,纠正此前基于旧 main 实例的“仍缺行”结论。版本由实际 SQL 查询确认,并检查了修复 commit 的祖先关系。

    1. main:原 issue 及换序场景已通过

    原始 SQL 和交换两个 recursive member 顺序的 SQL 均完整返回:

    (0,10), (1,11), (2,14)
    

    相关扩展场景也实际执行通过:

    2. 取消、prepared 与代码路径:实际 PASS

    以下测试均先用 -test.list 确认存在,再实际运行:

    • TestRecursiveCTEMultiMemberCancellationAndPreparedReuse:PASS (4.28s)。真实 embedded 1-CN 集群、fault point、第二连接 KILL QUERY、中断错误及 SQLSTATE 70100、同 prepared statement 复用得到 5、同 connection id 和 SELECT 1 恢复均通过。
    • TestDispatchRecursiveCTEMemberForwardsEmptyRoundMarker、TestDispatchIntermediateForwardsRecursiveRoundMarker、TestDispatchRecursiveSinkStopsOnlyAfterEmptyGlobalFrontier:3/3 PASS。
    • TestCompileRecursiveProbeJoinUsesUnshuffledBroadcast:PASS。
    • TestDetermineShuffleForJoinSkipsRecursiveSubtrees:PASS,含 remap 前后及左右子树的 4 个子场景。

    3. 4.2-dev:原问题仍然可复现,验收失败

    运行实例:git_version() = 7400cae392e90fa94b8cf54dc8520c3c13647884,version() = 8.0.30-MatrixOne-v4.2.9。

    同一原始查询独立执行时只返回:

    (0,10), (1,11)
    

    缺少 (2,14)。本轮又连续执行两次,均得到相同的两行。该失败是结果集不完整,不是超时,也不是 crash。同一 4.2 实例上的 #32799 原始跨代查询另有客户端 20 秒超时且未返回结果的反例,服务随后仍可连接。

    远端 4.2-dev 当前为 1920dc5c25758003315af6617b6ff6b37bf3fbf7,不包含 #32828;7400cae392..1920dc5c 对相关 dispatch.go / shuffle.go 没有差异。此处没有把源码检查冒充为对 1920dc5c 实例的运行验证。请完成 4.2-dev 的适配/移植,再复验原始及换序场景。

    4. motr 回归

    5. 未覆盖边界及交接

    本轮没有重新执行真实多 CN BVT、重启、race、大 build 内存压力、shuffle-only spill/retry 或性能测试,PR 作者的 2-CN/race 历史记录不计为本轮实测。MySQL 对照端口本轮无服务,未重新执行 MySQL;行集依据原始确定性递归语义。

    最终结论:main 的 #32612 原始及扩展回归通过,但 4.2-dev 仍明确缺 (2,14),所以双分支验收不通过、不能关闭。请研发处理 4.2-dev 修复后转回 QA。

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

Metadata

Metadata

Labels

kind/bugneeds-triage

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions