Skip to content

fix(plan): reuse bounded nested scalar CTEs on 4.2-dev - #32541

Merged
XuPeng-SH merged 4 commits into
matrixorigin:4.2-devfrom
ck89119:issue-21333-4.2-dev
Nov 12, 2026
Merged

XuPeng-SH merged 4 commits into
matrixorigin:4.2-devfrom
ck89119:issue-21333-4.2-dev

Conversation

@ck89119

@ck89119 ck89119 commented Nov 11, 2026

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:

Related to matrixorigin/matrixflow#21333.

Branch-specific adaptation of #32522 for 4.2-dev, not a mechanical cherry-pick. The mainline nested-CTE consumer-proof dependencies are intentionally not imported.

What this PR does / why we need it:

The two reported CTAS queries repeat an expensive aggregate through nested CTEs and scalar UNION ALL branches. On this branch, nested references and any consumer LIMIT prevent reuse; moreover, the existing reuse pass only runs at the SELECT entry point, not for CTAS's internal INSERT SELECT.

  • Admit one fixed-width scalar-aggregate producer only when its bound occurrences have identical semantics, every consumer executes, all published columns are genuinely demanded, and literal limits cannot truncate the structural row bound.
  • Preserve internal expression types and scan snapshots when comparing producers. Keep computed outer CTEs inline; reject joins, existing shared sources, unsupported windows, local filters, internal pagination, and uncertain evaluation domains in the new proof.
  • Trace live projection positions rather than counting references in dead/conditional expressions. Recognize the non-NULL numeric-literal-to-VARCHAR cast inserted by FORMAT without evaluating arbitrary constants.
  • Apply this restricted reuse to ordinary INSERT SELECT sources before adding target conversions or write nodes. Leave IGNORE, ODKU, REPLACE, RETURNING, and legacy fallback behavior unchanged. Preserve CTAS's HaveDDL/single-CN behavior, existing profitability/memory checks, and the materialized runtime.

Validation

  • Go 1.26.4, native libraries built in the target worktree; full pkg/sql/plan tests and 18 targeted compiler/materialized-source tests pass.

  • Public planner regressions cover 2/4/8 branches, N−1/N/N+1 limits, live/dead column demand, unsafe shapes, producer identity, formatted metrics, and the exact CreateAsSelectSql emitted by CTAS.

  • New helper statement coverage: 80.75%; changed profile-line coverage: 75.38%. Matching-toolchain gofmt/vet and incremental golangci-lint 2.6.2 pass.

  • mo-tester-generated results were checked against the clean branch baseline: values, metadata, and expected errors are identical. New BVT passes twice (35/35 each); the CTE directory passes 86/86, with teardown checked.

  • Using the complete original SQL/DDL and 12 local synthetic rows:

    Query Baseline base-table scans Patched base-table scans
    Eight-row chart 8 1
    Four-row comparison 4 1

    Both source SELECT and executing CTAS-shaped INSERT SELECT plans have these counts. Actual CTAS outputs remain 8/4 rows, with business values and table metadata matching the branch baseline. Unordered row-number-to-metric assignment is not treated as deterministic.

No production deployment or production-scale CTAS latency measurement was performed. This PR does not claim that the production incident is already resolved.

@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 →

@mergify

mergify Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

This pull request is merged.

@XuPeng-SH

XuPeng-SH commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

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

Commit: afe6b990b7b84dd2cd6f264eeb5bf79101d18091

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

Commit: afe6b990b7b84dd2cd6f264eeb5bf79101d18091

Assessment: approve

The bounded scalar-CTE proof safely extends the existing reuse mechanism to nested UNION ALL consumers and CTAS-generated INSERT SELECT. Producer identity, live demand and non-truncating limits protect evaluation semantics; existing profitability, memory and materialized-runtime owners remain intact. The added proof complexity is justified without adding another execution mechanism. No actionable defect remains.

Model: gpt-6.1-sol / max

Session: 01a1a004-90c3-73b1-acc9-3d4636452003 · slot 0 · generation 0

Validation:

  • Reviewed the complete six-file delta, linked matrixflow issue 21333 and its comment, and all three discussion entries. discussion-assessment.json accounts for every key; the discussion contains no substantive prior finding or disputed design decision.

  • Traced SELECT and ordinary INSERT SELECT admission, numeric binding contexts, producer identity/types/snapshots, live/conditional demand, structural row bounds, excluded DML/fallback modes and exact CTAS-generated SQL. Reviewed existing sink/reader registration, generation/reset, cancellation/error propagation, cleanup and memory/spill owners.

  • Frozen exact-head evidence reports successful Ubuntu UT, SCA on arm64, shared build, coverage and both active compose BVT jobs (14 success, 13 skipped, 2 neutral). The repository UT owner includes planner/compiler/materialized packages. Skipped and neutral jobs are not counted as passes.

  • Author reports full planner tests, 18 compiler/materialized tests, vet/lint, the new BVT twice (35/35), the CTE directory (86/86), baseline-equivalent values/metadata/errors and synthetic original-query scan reductions from 8/4 to 1. These are author-reported results; raw local-validation records were not supplied.

  • Supplied isolation preflight passed. Local bounded delivery check passed: clean frozen head and full patch identity, git diff --check, gofmt for four changed Go files, all 35 SQL/result statement pairs, explicit database/prepare teardown, independent four-metric decimal arithmetic and exact discussion-key coverage. validation-audit.json reconciles both executor receipts with source, commands, exits and retained stdout/stderr.

Limitations:

  • No local Go product/race suite or live mo-tester run was repeated. CI statuses are supplied, but raw CI/author test logs are absent; local pairing/arithmetic checks do not claim SQL execution.

  • The original production SQL attachment, production deployment and production-scale CTAS latency were not independently validated. This scoped branch optimization does not establish that the linked production incident is resolved; unordered row-number-to-metric assignment remains unspecified.

This branch was successfully deployed

No deployments
ci — 7abad011 Deployed Nov 12, 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.

3 participants