Skip to content

fix(sql): preserve inline CTE identity for correlated aggregates - #30834

Merged
XuPeng-SH merged 3 commits into
mainfrom
codex/issue-23083-correlated-subqueries
Oct 22, 2026
Merged

XuPeng-SH merged 3 commits into
mainfrom
codex/issue-23083-correlated-subqueries

Conversation

@XuPeng-SH

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 fixes:

Fixes #23083

What this PR does / why we need it:

A correlated range aggregate over a transparent inline CTE currently fails with error 20102 because the CTE projection hides the native row identity needed by scalar decorrelation. This also rejects a SELECT combining correlated EXISTS and MAX over the same CTE.

Forward the existing hidden Row_ID through eligible raw-column CTE projections, keeping it outside the visible binding and result schema. Reuse the existing LEFT JOIN, scalar reaggregation, empty-input reconstruction and column-alias owner. Stage projection changes until the complete rewrite is admitted. CTE occurrences whose private identity exceeds the captured transport width stay inline, so later CTE sharing cannot discard that identity. No new execution operator or identity enumeration is introduced.

The proof is deliberately limited to deterministic, row-preserving projections of one native table. Computed projections, duplicate-producing UNION ALL, DISTINCT/GROUP BY, multiplying joins and pagination retain their guarded rejection. General derived-table decorrelation is outside this change.

The issue's original two queries already pass the frozen main baseline, including the complete indexed schema and an ANALYZE control. This PR preserves those regressions and fixes the remaining inline-CTE boundary; it does not attribute the historical queries' existing success to this patch.

Validation on macOS arm64 / Go 1.27.1, with the supported CGo runner and fresh matching native build:

  • Entire pkg/sql/plan in normal and race modes, -count=1: passed.
  • Real frontend SQL regression in normal and race modes: passed. Covers original DDL/queries, both EXISTS/scalar orders, duplicate rows without a primary key, NULL/empty inputs, consecutive MAX/COUNT aggregates, public columns, aliases, and prepared execution after empty/error results and inserted data. MySQL 8.4.8 independently agrees with the result bags.
  • New scalar_cte_row_identity BVT: 9/9 twice against the same instance, with explicit database teardown checks. Golden results were manually authored and checked against the independent oracle.
  • Formatting, go vet, configured incremental golangci-lint: passed; no new molint diagnostics against the base. The four existing molint diagnostics are unchanged.
  • Two existing neighboring BVT scripts: base and head both have 133/143 passing statements and the same 10 pre-existing metadata mismatches; all returned result blocks and mismatch diagnostics match exactly. These scripts are not reported as fully green.

Performance: original base-table paths keep the existing row-identity fast path and execution operators. Three paired planning controls retain the same allocation counts. Eight longer alternating pairs for a range-planning control show mixed timing differences (mean paired change +0.88%; separate revision medians +2.3%, overlapping ranges); this is not an execution-throughput measurement or proof of zero overhead. Newly supported CTE queries carry one private Row_ID through affected projections and forgo sharing of that widened CTE. No global CTE-reuse switch or per-row window is added.

Design and final overall review passed using explicitly selected gpt-6.1-sol / xhigh; final review found no unresolved material blockers. Remote CI has not yet run for this branch.

@XuPeng-SH
XuPeng-SH requested a review from aunjgr as a code owner October 22, 2026 10:00
@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 →

@XuPeng-SH

XuPeng-SH commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor Author

mo-bot review · mo-bot-local · done

Commit: c36a4424fc9e92bdba43d9129885ac7bd2348c34

Review completed. Read the assessment.

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor Author

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-local · review

Commit: c36a4424fc9e92bdba43d9129885ac7bd2348c34

Assessment: approve

Reviewed the complete seven-file delta and its binding, decorrelation, CTE reuse, alias and column-remapping consumers. The design preserves native row identity privately, stages projection changes until admission, and reuses existing aggregation and cleanup owners. The transport-width guard prevents later CTE sharing from discarding that identity. No actionable defects remain within the stated scope.

Model: gpt-6-astra / xhigh

Session: 01a13dcf-1517-7f90-b3f9-c5f9f9e7031b · slot 0 · generation 1

Validation:

  • Isolation preflight passed. Independently verified 192 current native-build inputs and 46 retained output hashes against the successful build-owner receipt.

  • All three new planner tests and the frontend regression passed, covering duplicate rows, NULL/empty inputs, aliases, EXISTS ordering and prepared reuse after errors and data changes. Receipt: 1792665472229439364-4.json.

  • The entire planner package passed: 4,091 top-level tests. Receipt: 1792665525526888198-3.json.

  • Independent SQL probes passed twice, covering nested sorted CTEs, repeated projected columns, predicate composition, DISTINCT/LIMIT and empty outer input. The exact BVT passed 9/9 statements twice on one instance, with database removal verified between runs. Receipt: 1792665638907495187-3.json.

  • The merge-base production-file control preserved success for both original issue queries and reproduced the intended identity errors for the newly supported CTE cases. Mixed equality/range LEFT JOIN composition remained rejected on both revisions. Receipt: 1792665649731793442-4.json.

  • go vet passed for both changed Go packages. Formatting, clean pinned source, complete patch and receipt/log reconciliation passed. Evidence and review mapping are retained under /home/xupeng/.local/state/mo-bot/jobs/e5e7f000bd7b9dc5cd436cbc/attempt-1/review-evidence.json and review-map.json.

Limitations:

  • Runtime CI was pending in the frozen snapshot. Linked issue bodies and retained author-validation artifacts were absent; author race, MySQL-oracle and performance claims were not counted as verified checks.

  • Local validation used Linux amd64, Go 1.27.1 and the canonical one-CN fixture. No distributed-topology or race run was required by the unchanged execution and concurrency contracts.

  • No throughput benchmark was run. Newly supported CTE queries carry a private Row_ID and forgo sharing of widened occurrences; existing native-table paths retain their fast path. No measured performance improvement is claimed.

This branch was previously deployed

1 inactive deployment
ci — 29c75fd6 Deployed Oct 22, 2026 by XuPeng-SH
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/L Denotes a PR that changes [500,999] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MySQL Compatibility: JOIN column reference remapping error in nested correlated subqueries

2 participants