Skip to content

perf(plan): reuse eligible multi-reference CTEs - #26295

Closed
VioletQwQ-0 wants to merge 4 commits into
matrixorigin:mainfrom
VioletQwQ-0:codex/issue-26131-cte-reuse-v3
Closed

VioletQwQ-0 wants to merge 4 commits into
matrixorigin:mainfrom
VioletQwQ-0:codex/issue-26131-cte-reuse-v3

Conversation

@VioletQwQ-0

@VioletQwQ-0 VioletQwQ-0 commented Jul 28, 2026 •

Copy link
Copy Markdown
Collaborator

What type of PR is this?

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

Which issue(s) this PR fixes:

issue #26131

What this PR does / why we need it:

Reuse only deterministic, profitable, fixed-width, bounded multi-reference CTEs through one local materialized producer and independent full-drain readers. Unsupported or uncertain shapes remain inline.

The materialized source keeps the first 64 MiB in the query MPool and spills later batches to an anonymous query-scoped local file, so underestimated planner statistics cannot turn a formerly successful inline query into a runtime-limit error. Each reader owns its decoded spill batch until it advances or releases; the final producer/reader release reclaims the shared batches, per-reader batches, and spill file.

The branch is composed onto current main. Its superseded pipeline-terminal commit was dropped because #26250 now provides the durable terminal-state implementation; only the materialized Dispatch reset branch is composed onto that upstream behavior.

Validation on exact head aa815b68971e29daff1ea86587ec4cac1e7b4eef:

  • planner/materialized/merge/compile focused package tests
  • Source and Merge race tests
  • embedded TPCH Q15-shaped execution with a real grouped producer, join consumer, scalar MAX consumer, two Sink Scan nodes, expected result, EOF, rows.Err() and Close() checks
  • targeted mutation: restoring the old 64 MiB hard error makes the overflow regression fail
  • affected-package vet and git diff --check
  • schema v4 preflight: PASS review=PASS validation=PENDING, diff hash 8cc8631ac675171d883041af41d62493a9a0b52bfbec949dfd3bf99990f1c75c

Prior-head CI classification:

  • SCA copyloopvar was PR-caused and is fixed on this head.
  • Ubuntu UT failed in unrelated TAE TestCkpLeak with panic: ExpectedEOB; this PR's planner tests passed in that run.
  • Fresh exact-head CI is pending.

Performance status:

  • Current-head SF10 A/B/A is PENDING because both available hosts failed the idle/ownership gate (local user mo-service; mo-55 long-running MatrixOne container).
  • A superseded-base strict A/B/A showed about 49.9% median improvement and the expected one-producer/two-consumer plan, but that result is supporting evidence only and is not claimed as current-head proof.

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

Deep-reviewed exact head aa815b6. The materialized Source producer/reader ownership, cancellation, prepared-reuse generation, and spill cleanup paths are otherwise coherent. One P1 semantic blocker remains.

[P1] SAMPLE is treated as deterministic and can be shared (pkg/sql/plan/cte_reuse.go, cteSubtreeIsDeterministic).

The node-type denylist excludes volatile functions but not plan.Node_SAMPLE, whose executor uses random selection. I reproduced this on the exact head with an expensive fixed-width producer:

with c as (
  select sample(total, 2 rows) as n from (
    select l_suppkey, sum(l_extendedprice) as total
    from lineitem group by l_suppkey
  ) grouped
) select a.n from c a join c b on a.n = b.n;

Before this optimization the plan has two independent SAMPLE nodes. On this head the cost guard wins and rewrites it to one materialized SINK plus two SINK_SCAN consumers. The two independent random samples therefore become the same sample; joins, intersections, aggregates, and returned rows can change.

Please classify Node_SAMPLE as nondeterministic and add this profitable shape to TestCTEMultiReferenceReuseGuards (asserting zero reachable SINK_SCAN). Focused CTE race tests and the materialized/dispatch/merge/compile race packages passed; the full plan race package was killed by the local resource limit, while its focused CTE race suite passed.

@VioletQwQ-0

Copy link
Copy Markdown
Collaborator Author

Superseded by #26331, which is now the canonical PR for #26131. The successor removes the disk-spill experiment, keeps bounded query-MPool materialization, and includes the reviewer-reported profitable SAMPLE SQL as a production planner guard. The guard fails on this reviewed head with two reachable SINK_SCAN nodes and passes on #26331 with zero, preserving independent sampling semantics. Closing this duplicate; the branch is retained.

This branch was previously deployed

1 inactive deployment
ci — aa815b68 Deployed Jul 29, 2026 by VioletQwQ-0 via Matrixone Utils CI / Coverage #43178
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/enhancement size/XL Denotes a PR that changes [1000, 1999] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants