Skip to content

perf: bound repeated work in analytic queries - #28752

Merged
XuPeng-SH merged 18 commits into
matrixorigin:mainfrom
aptend:perf/tpcds-timeout-heads-20260911
Sep 14, 2026
Merged

XuPeng-SH merged 18 commits into
matrixorigin:mainfrom
aptend:perf/tpcds-timeout-heads-20260911

Conversation

@aptend

@aptend aptend commented Sep 12, 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 #26768.

What this PR does / why we need it:

Large analytic plans can repeat the same CTE/grouping work, miss safe early pruning, project unique hash matches row by row, and retain a full partition for a small RANK bound. This PR addresses those mechanisms generically:

  • restores CTE and grouping-set sharing only with deterministic full-drain proofs, conservative materialization estimates, statement spill budgets, and legacy fallback;
  • keeps grouping-set aggregates on the raw input stream, while ignoring inactive rollup keys during group-cardinality estimation;
  • exposes safe common keys from mixed DNF predicates and delivers uncorrelated scalar predicates through joins only when the physical runtime-filter topology is provably local;
  • batches projection for unique residual-free inner hash joins;
  • extends bounded Partition Top-N to exact RANK <= N semantics by retaining boundary ties, with chunked output, accounted state, and an MORPC v68 compatibility gate.

No query ID is encoded in these rules. Statistics participate only in conservative cost/admission decisions, never in semantic legality. Unsupported, uncertain, mixed-version, or over-budget shapes keep the previous plan. The design and counterexample matrix are recorded in the stats-independent analytic rewrite RFC v11 (exact revision).

Existing direct performance evidence

The evidence is mechanism-local: the same SQL on base/candidate code, or old/new methods on the same benchmark fixture. Unrelated TPC queries are not treated as controls.

HashJoin projection was measured on the same 256-row/eight-column fixture with GOMAXPROCS=4 using:

mo-cgo-test -run '^$' -bench '^BenchmarkHashJoinUniqueProjection$' -benchmem -count=3 ./pkg/sql/colexec/hashjoin
same-source/scalar       84194  83924  83747 ns/op    0 B/op  0 allocs/op
same-source/batch        19466  19497  19586 ns/op    0 B/op  0 allocs/op
alternating/scalar       83787  83968  83606 ns/op    0 B/op  0 allocs/op
alternating/batch        51293  50597  50863 ns/op    0 B/op  0 allocs/op
PASS

This was run on production/test tree 71d4359cb4; current head 7b4041e2f4 changes only this RFC. The earlier SQL A/B used base e99ed4148f and hash-join candidate 45564cc78f; the latter has the same stable patch ID as rebased commit 5a644c3230:

TPC-DS Q67 100G Wall time Peak anonymous Result / ordinary plan
base 317.230s 15.169 GiB reference
candidate 260.300s 15.094 GiB identical

The planner admission changes also have same-query evidence. For Q2 1T, base statement 01a03cea-dd7f-74a1-98cc-033cfe1e66bd took 1980.838s and read 4,320,253,780 rows / 51,845,382,928 bytes; candidate statement 01a03d4f-43f5-7f24-a5cc-4823fb8792b3 took 855.284s and read 2,160,199,939 rows / 25,923,275,856 bytes. Both returned 2513 rows. The CTE change is carried unchanged by original commit 8dfcc396d5 and rebased commit 48fb4d2e29 (identical stable patch ID).

The consolidated candidate was also run on the isolated 1T TPC-DS lab with 128 MiB operator spill limits, merge disabled, a 20 GiB no-swap cgroup, and memory/OOM monitoring. Raw terminal summaries were:

Q2:   exit_code=0   elapsed_ms=914102
Q14a: exit_code=0   elapsed_ms=1059257
Q14b legacy candidate: exit_code=124 elapsed_ms=1800005
Q14b final candidate:  exit_code=0   elapsed_ms=994934

Q14a's ordinary plan reduced fact-table scans from 63 to 15. Q14b retained the original SINGLE cardinality check while moving its scalar predicate to the date input; the 1G base/candidate output SHA-256 was identical (a581011fcc880666be34f0dda56a8836443b0d71573701a7c7f9c3ca06228d8e). Focused rejected-shape tests, rather than unrelated neighboring queries, verify legacy fallback.

The latest revision removes decimal SUM(SUM(v)) prefix reuse. Checked fixed-width addition is not associative in error semantics: a finer partial sequence can overflow even when the raw coarser input succeeds through cancellation. Dynamic grouping-set expansion remains available because each grouping set consumes raw rows in their original order.

After rebasing onto current main, MORPC v67 was already allocated to named process timezone identity, so bounded RANK moved to v68 and the protobufs were regenerated. Focused v67/v68 fallback and recursive remote validation tests pass. The unique HashJoin projection correctness/reuse/allocation tests and the benchmark above also pass. The pre-rebase exact-head CI was green; CI for the rebased head is pending.

@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/XXL Denotes a PR that changes 2000+ lines label Sep 12, 2026
@aptend
aptend force-pushed the perf/tpcds-timeout-heads-20260911 branch from 5222ef2 to 231ec9a Compare September 12, 2026 12:56

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

Re-reviewed exact head 9dbdf6ff03fdcb87bec4a98b9aa30a158b200e86. P2 — provide the focused, reproducible performance evidence required by RFC v10 (lines 568–582). The PR gives three TPC-DS elapsed times, but I could not find the base/candidate revisions, raw comparable outputs, and nearest unchanged control needed to evaluate the changed hash-join projection hot path and expanded planner admission. A representative focused benchmark is sufficient; alternatively show the applicable paths were unchanged and reuse valid measurements. This does not require fixed percentage targets, p50/p95, blanket telemetry, or the full TPC-H corpus. I found no remaining concrete semantic blocker, and I found no ~45s single-test or material racing-UT regression: exact-head race UT passed in 48m01s and rollup.sql took 2.446s (227 pass, 1 ignored).

@aptend

aptend commented Sep 14, 2026

Copy link
Copy Markdown
Contributor Author

Addressed on rebased head 7b4041e2f4.

I reused the measurements already produced during this iteration and added their raw summaries, tested revisions, statement IDs, and output/plan checks to the PR body. In particular:

  • current-tree HashJoin scalar-vs-batch benchmark on the same fixture: same-source ~84us -> ~19.5us and alternating-source ~83.8us -> ~50.9us, all 0 B/op and 0 allocs/op;
  • Q67 100G on the same SQL/config: 317.230s -> 260.300s, with identical result and ordinary plan;
  • Q2 1T: 1980.838s -> 855.284s, with rows/bytes read approximately halved and the same 2513-row result;
  • consolidated 1T runs: Q2 914.102s, Q14a 1059.257s, and Q14b changed from the 1800s timeout to 994.934s.

The original and rebased HashJoin/CTE commits have identical stable patch IDs, so those earlier measurements remain applicable; the current benchmark was rerun after the rebase. RFC v11 now makes the acceptance rule explicit: use the same SQL or the same fixture's legacy path as the comparison. An unrelated or merely adjacent TPC query is not a meaningful control. Rejected shapes remain covered by focused fallback/correctness tests.

The branch is also rebased onto current main. Since main allocated MORPC v67 to timezone identity, bounded RANK now uses v68; protobuf generation and focused v67/v68 fallback/remote validation tests pass.

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

Reviewed exact head 7b4041e2f4f3afea39b318775f9227daf56c8db2. The new evidence closes my prior P2: it provides same-fixture scalar/batch raw benchmark results with revisions plus same-query planner A/B timings, read counts and output evidence. I verified the CTE original/replay stable patch IDs and that the only relevant implementation delta since the earlier review consistently gates on MORPC v68. No correctness, lifecycle or resource blocker found. Exact-head CI run 34808745097 passed (race UT 51m42s); no newly added ~45s test or major racing-UT regression mechanism found.

@mergify

mergify Bot commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-09-14 07:04 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • 🚫 Left the queue — 2026-09-14 07:11 UTC · at 7b4041e2f4f3afea39b318775f9227daf56c8db2

This pull request spent 7 minutes 23 seconds in the queue, with no time running CI.

Reason

The pull request can't be updated

merge conflict between base and head

Hint

You should update or rebase your pull request manually. If you do, this pull request will automatically be requeued once the queue conditions match again.
If you think this was a flaky issue, you can requeue the pull request, without updating it, by posting a @mergifyio queue comment.

Requeued — the merge queue status continues in this comment ↓.

@mergify

mergify Bot commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-09-14 08:15 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • 🟠 Checks running · in-place
  • 🚫 Left the queue — 2026-09-14 09:09 UTC · at 15359f780b63bece309a24699b3dcede22330465

This pull request spent 54 minutes in the queue, with no time running CI.

Reason

The pull request can't be updated

This pull request seems to come from a fork, and Mergify needs the author's permission to update its branch.
The author needs to enable "Allow edits from maintainers" on this pull request, or update the branch manually.

Hint

You should update or rebase your pull request manually. If you do, this pull request will automatically be requeued once the queue conditions match again.
If you think this was a flaky issue, you can requeue the pull request, without updating it, by posting a @mergifyio queue comment.

Tick the box to put this pull request back in the merge queue (same as @mergifyio queue).

  • Requeue this pull request

This branch was previously deployed

1 inactive deployment
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.

4 participants