Skip to content

fix(plan): support complex non-equality scalar aggregates - #31275

Merged
XuPeng-SH merged 13 commits into
matrixorigin:mainfrom
ck89119:issue-28298-main
Oct 28, 2026
Merged

XuPeng-SH merged 13 commits into
matrixorigin:mainfrom
ck89119:issue-28298-main

Conversation

@ck89119

@ck89119 ck89119 commented Oct 26, 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:

Fixes #28298

What this PR does / why we need it:

Select existing dependent execution before destructive predicate pull-up when legacy decorrelation cannot preserve a range-correlated scalar subquery's complete relation. Share existing identity and aggregate-input checks, retaining native-table, equality (including deep correlation), and transparent-CTE fast paths.

Support aggregate expressions, HAVING, DISTINCT, grouped Top-1, derived/UNION inputs, and multiple outer bindings while preserving duplicate rows, empty-input semantics, scalar cardinality errors, and conditional evaluation of row-comparison operands. Keep SQL EXECUTE's bare-parameter TEXT presentation at the client output; internal programs retain semantic types. Update obsolete rejection expectations to assert values and multiplicities, retaining real error and prepared-reuse controls.

Common direct aggregate-result expressions such as MAX(v)+1, COALESCE, CASE, and combinations of aggregates now reuse the existing set-based reaggregation and reproject its outputs. Guarded demand, volatile/correlated results, grouping, HAVING, pagination, and unproven identities retain complete-relation execution. Complex shapes may still execute the inner relation per demanded outer row; no new runtime mechanism, protocol, or cross-row cache is added.

Validation (Go 1.27.1, Darwin arm64):

  • Full plan/compile tests passed in normal and coverage modes; all three affected cross-package regression functions passed. Changed-line coverage: 91.7% for the PR, 97.2% for the expression-path fix.
  • Eight mo-tester files passed on a clean single-CN service; the aggregate case passed 93/93 twice, with independent teardown checks. SQL oracles and client tests covered NULL/empty/bag semantics, SQL and binary prepared reuse, conditional errors, cardinality errors, and connection recovery.
  • Formatting, vet, and incremental lint passed (0 issues). Existing-route planner allocation counts were unchanged against the pre-fix baseline.

Equivalent-result SQL scaling comparison, using optools/range_scalar_bench.py:

Outer × inner rows Inner expression before (ms) Inner expression after (ms) Equivalent outer expression after (ms)
100 × 2000 3091.0 11.1 11.0
400 × 2000 12728.0 48.2 67.2
100 × 8000 11777.3 45.0 41.8
400 × 8000 47559.4 175.9 168.6

Single CN, GOMAXPROCS=2, GOMEMLIMIT=1GiB; three samples after warmup, interleaved equivalent queries, with every result checked independently. These shared-host measurements are not production latency guarantees. The existing range join can still do outer × inner work; the change removes repeated dependent execution for the admitted expressions.

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

The new dependent route has a substantial performance problem on a small, ordinary correlated aggregate. Please address this before merging.

On head ab8fe1f8a7f8a767c9ef34b7deb533df4e21d45a, I ran the following two result-equivalent queries locally on the same single-CN instance, with 100 outer rows and 2,000 inner rows, checking every result:

SELECT o.id, (SELECT MAX(i.v) FROM perf_inner i WHERE i.k <= o.id) + 1
FROM perf_outer o WHERE o.id <= 100 ORDER BY o.id;

SELECT o.id, (SELECT MAX(i.v) + 1 FROM perf_inner i WHERE i.k <= o.id)
FROM perf_outer o WHERE o.id <= 100 ORDER BY o.id;

The existing set-based route took 15.3/11.2 ms; the newly supported inner-expression route took 1.951/1.939 s. All 100 result rows matched the independent expected values in both runs. A separate run under concurrent compilation took 8.4–10.1 s for 100 outer rows and 20.0–24.6 s for 400; these timings are not controlled production benchmarks, but corroborate the scaling concern.

The second shape previously failed with NYI, so this is not a claimed performance regression of the same previously supported SQL. The concern is that a common, result-equivalent expression is now admitted into a single-CN dependent path that repeatedly executes the inner relation and is already orders of magnitude slower on a small input.

Please provide a practical execution path for common aggregate-result expressions such as this example, while preserving empty-input, row identity, cardinality, and conditional-evaluation semantics. Reuse the existing set-based route where safe, or otherwise reduce repeated inner work. Add an equivalent-result SQL performance/scaling comparison and retain correctness/negative cases; also verify previously supported fast paths remain unchanged.

@XuPeng-SH

XuPeng-SH commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

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

Commit: 1534cf20c485f3295665a9cb630dacf9c6b23567

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: 1534cf20c485f3295665a9cb630dacf9c6b23567

Assessment: approve

Reviewed the complete delta, design, linked issue and discussion. The prior performance blocker is resolved: common aggregate expressions now use set-based execution with comparable SQL timings to the established route. No actionable findings remain.

Model: gpt-6-astra / xhigh

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

Validation:

  • Full planner and compiler package suites passed, along with all three affected integration tests.

  • Public SQL probes passed 32 result cases and six additional nullability cases, plus cardinality-error recovery, views, CTAS and prepared reuse.

  • Four bounded workloads produced inside-expression medians of 10–142 ms versus 10–137 ms for equivalent outside-expression controls; every result was checked.

  • Merge-base production-overlay planner benchmarks confirmed bounded admission costs across five controls.

  • Isolation preflight and native provenance verification passed. Seven qualified checks were reconciled with source fingerprints, exit receipts and retained logs in attempt-2/validation-audit.json.

  • Saved discussion-assessment.json covering all six nonempty discussion entries and reconciling the earlier approval and performance finding.

Limitations:

  • Local SQL and performance validation used one CN; supplied multi-CN CI successes lack retained raw logs.

  • Complex fallback shapes retain per-demand execution costs. Measurements do not establish general throughput guarantees.

  • Planner baseline measurements used exact merge-base production files with current dependencies.

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

mergify Bot commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

🚫 Left the queue

1 h 16 min in the queue, with no time running CI.

So far

  • ✅ Entered the queue Wed 28 Oct, 06:41 · rule main · triggered by rule Automatic queue on approval for main
  • ⏹️ Testing stopped · in place
  • 🚫 Left the queue 07:58 · 9652149

Reason

Pull request #31275 has been dequeued

Pull request from fork cannot be queued. This pull request comes 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.

Hint

You should look at the reason for the failure and decide if the pull request needs to be fixed or if you want to requeue it.
If you do update this pull request, it will automatically be requeued once the queue conditions match again.
If you think this was a flaky issue instead, you can requeue the pull request, without updating it, by posting a @mergifyio queue comment.

Merge conditions: 2 of 3 met

This pull request

Condition Required by
✅ #review-threads-unresolved = 0 GitHub branch protection
✅ github-review-approved GitHub branch protection

The batch, tested in place

Condition Required by
❌ any of: check-neutral = CI Required, check-skipped = CI Required, check-success = CI Required GitHub branch protection

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

  • Requeue this pull request

Times in UTC · dashboard · how the merge queue works

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

dequeued kind/bug Something isn't working size/XL Denotes a PR that changes [1000, 1999] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Compatibility]: non-equality correlated scalar aggregates reject common query shapes

4 participants