Skip to content

fix(plan): allow GROUP BY and HAVING in recursive CTE scalar subqueries - #30082

Merged
mergify[bot] merged 2 commits into
matrixorigin:mainfrom
iamlinjunhong:fix/23077-recursive-cte-group-by
Oct 19, 2026
Merged

mergify[bot] merged 2 commits into
matrixorigin:mainfrom
iamlinjunhong:fix/23077-recursive-cte-group-by

Conversation

@iamlinjunhong

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR addresses:

What this PR does / why we need it:

A scalar subquery inside a recursive CTE inherits the recursive statement state even though it owns an independent SELECT query block. As a result, valid scalar subqueries over ordinary tables were rejected when they used GROUP BY or HAVING.

Scope those two clause restrictions to the owning recursive query block using the existing ownership check. Anchor grouping and independent scalar grouping/aggregation work, while direct recursive GROUP BY/aggregation and nested recursive-table scans remain rejected. This follows the selected MySQL compatibility scope: the original issue examples contain direct recursive aggregation, which MySQL also prohibits.

Extend the existing planner and SQL regression fixtures to cover grouped projection/predicate subqueries, standalone HAVING, correlation, nested queries, NULL/empty groups, prepared reuse, ordinary scalar cardinality errors, and preserved rejection boundaries. The change adds no allocation or execution mechanism.

Validation

  • Focused planner suite: 14 subtests passed.
  • Full pkg/sql/plan tests and go vet passed.
  • Incremental golangci-lint 2.14.0: 0 issues.
  • Public scalar-aggregate BVT: 44/44 statements passed on two same-instance runs; fixture database absent after each run.
  • Existing recursive subquery-scope BVT: 7/7 statements passed.
  • Local validation used Go 1.27.1 on macOS arm64 with repository-built native libraries. Public service build used the repository SIMD setting. CI results are pending.

Issue verification remains with the reporter after merge; this PR does not request automatic issue closure.

@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@matrix-meow matrix-meow added the size/M Denotes a PR that changes [100,499] lines label Oct 13, 2026
@mergify mergify Bot added the kind/bug Something isn't working label Oct 13, 2026
@XuPeng-SH
XuPeng-SH force-pushed the fix/23077-recursive-cte-group-by branch from 22566e4 to 838b815 Compare October 18, 2026 12:53
@XuPeng-SH

Copy link
Copy Markdown
Contributor

本轮维护更新:rebase 到 main 799523418e9f941afd65cd440d24e7fa0003a35e,新提交 838b815cdfc82634bce331df6a23311967a764be。

仅 rebase。原 CI 的 logservice 测试辅助函数调用签名不匹配已与 main 中兼容调用对齐;本地相关 planner 检查通过,不能据此宣称新 CI 已通过。

本地验证:受影响路径的普通 UT、单轮 race 和完整变更 Go 包范围的增量 SCA 均通过。未运行整套 CI/BVT、GPU 或性能负载;不将历史作者记录标作本轮新运行。

变更保留检查覆盖完整原 PR 与 main 的集成结果;仅保留本轮明确说明的维护增量。新提交的 GitHub CI 结果待运行,原 CI 中未定位或未归因的失败仍需单独跟踪。

@XuPeng-SH

XuPeng-SH commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

mo-bot review · mo-bot-xupeng · done

Commit: 95f7de570349c257b2d1c75fbdd4e6488431d6d5

Review completed. Read the assessment.

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mo-bot review · mo-bot-xupeng · review

Commit: 95f7de570349c257b2d1c75fbdd4e6488431d6d5

Assessment: needs_validation

The complete four-file review found no confirmed defect. The fix consistently reuses existing query-block ownership, and focused and full planner tests pass. Public SQL execution evidence remains necessary for approval.

Model: gpt-6-astra / xhigh

Session: 01a14d34-4e51-79a3-a62d-6c9c16279dc8 · slot 0 · generation 0

Validation:

  • All 14 aggregate/grouping scope subtests and six recursive-reference scope subtests passed with -count=1 and the supplied test-exec. planner-focused-native.log matches checks/1792334995824793681-3.json.

  • Full pkg/sql/plan passed in 28.443s with -count=1. planner-package.log matches checks/1792335049029068623-3.json.

  • Isolation preflight passed. Native prerequisites were freshly built through make cgo and checked against pinned input hashes; the initial native setup rejection was recovered.

  • Pinned head, unchanged source and git diff --check verified. Logs, receipt mappings and review record are retained under /home/xupeng/.local/state/mo-bot/jobs/3bfb4cbb86a35cb6d22f2473/attempt-1.

Limitations:

  • No traceable exact-candidate public BVT execution is supplied. Author claims lack matching revision and terminal artifacts; supplied CI does not exercise this SQL path. Close the gap with normal-comparison runs of recursive_cte_scalar_aggregate.sql, including same-instance rerun and teardown verification, proving NULL/empty groups, cardinality errors and prepared reuse.

  • The linked issue body is absent from the frozen evidence. Issue scope was assessed from the PR's explicit compatibility contract, which preserves rejection of direct recursive aggregation.

@mergify mergify Bot added the queued label Oct 19, 2026
@mergify

mergify Bot commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-10-19 00:38 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • ✅ Checks passed · in-place
  • ✅ Merged — 2026-10-19 02:04 UTC · at 75ff736835c3d00d40dc00a0a1d3b11129388e5b · squash

This pull request spent 1 hour 26 minutes 44 seconds in the queue, including 49 minutes 48 seconds running CI.

Required conditions to merge

This branch was previously deployed

1 inactive deployment
ci — 88a32794 Deployed Oct 19, 2026 by mergify[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/bug Something isn't working size/M Denotes a PR that changes [100,499] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants