Skip to content

feat: support prepared and sortable discrete percentiles - #28540

Merged
XuPeng-SH merged 73 commits into
matrixorigin:mainfrom
daviszhen:codex/issue-25110-p1
Sep 17, 2026
Merged

XuPeng-SH merged 73 commits into
matrixorigin:mainfrom
daviszhen:codex/issue-25110-p1

Conversation

@daviszhen

@daviszhen daviszhen commented Sep 9, 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:

Follow-up for issue #25110.

What this PR does / why we need it:

This completes the two highest-priority ordered-set percentile follow-ups discussed in #25110:

  • allows a prepared-statement parameter for p in APPROX_PERCENTILE, PERCENTILE_CONT, and PERCENTILE_DISC; the parameter is fixed per EXECUTE, while column/per-row percentile expressions remain rejected;
  • prevents stale percentile reuse by rebuilding the physical compile for parameterized percentile aggregates on every execution;
  • extends PERCENTILE_DISC from numeric-only input to allocation-accounted SQL-sortable scalar types, including VARCHAR, DATE, UUID, and DECIMAL256, while retaining the numeric interpolation restrictions of PERCENTILE_CONT; JSON and array/vector inputs remain rejected until their O(N) sorter scratch is allocator-aware;
  • retains variable-width discrete inputs and string provenance in the accounted aggregate argument arena and supports unambiguous intermediate-state serialization and merge;
  • adds planner, compiler, executor, frontend cache, and distributed protocol regression coverage.

Ordered-set window functions remain outside this PR.

Compatibility and merge dependency

  • Extended discrete-percentile remote execution is admitted at MORPC v82.
  • The current compatibility prefix assigns v65 to signed INT results for ASCII, v66 to GROUP_CONCAT source-row diagnostics, v67 to named process timezone identity, v68 to complete GROUP_CONCAT cut reporting, v69 to bounded RANK, v70 to CONV bases, v71 to checked integer arithmetic, and v72 to corrected IP semantics.
  • Main owns v74 for numeric HEX, v75 for persistent data-branch identity, v76 for NaN-ordered percentiles, v77 for typed HLL keys, v78 for variable-length GROUP keys, v79 for canonical opaque DISTINCT keys, and v80 for string numeric result contracts, and v81 for opt-in table-owned AUTO_ID_CACHE. These gates remain unchanged; prepared APPROX_PERCENTILE preserves v76's explicit direction configuration.
  • PR fix: widen decimal SUM accumulation #28624 owns v73 for widened DECIMAL SUM partial state after the current v72 prefix; its full implementation is integrated in this head.
  • This PR depends on fix: widen decimal SUM accumulation #28624 and must merge after it. This head includes fix: widen decimal SUM accumulation #28624 through commit 44c60bd, so a v82 binary implements the complete monotonic protocol prefix.
  • Exact design revision for renewed approval: 7795d61a06, in docs/design/20260910-prepared-and-discrete-percentiles.md.

Validation

  • make build
  • go test ./pkg/sql/colexec/aggexec ./pkg/sql/colexec/group ./pkg/sql/colexec/window ./pkg/sql/colexec/partition ./pkg/sql/colexec/hashjoin ./pkg/sql/compile ./pkg/sql/plan ./pkg/sql/plan/function ./pkg/frontend ./pkg/vm/process -count=1
  • local MatrixOne SQL validation for two executions of the same prepared percentile statement with different p values, prepared PERCENTILE_CONT/APPROX_PERCENTILE, and VARCHAR/DATE/DECIMAL256 PERCENTILE_DISC

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

@daviszhen

Copy link
Copy Markdown
Contributor Author

Resolved the latest main conflict at 3147b5b after preserving the remote branch updates and merging upstream/main through 2ea4ec2.

The monotonic MORPC chain now keeps main at v69-v72, widened DECIMAL SUM at v73, and extended discrete-percentile inputs at v74. Constants, gates, validation errors, compatibility tests, rollout guidance, and the design document were updated consistently. The exact design revision for renewed approval is 3147b5b.

Local aggregate/group/window/compile/plan/frontend/process regression tests passed, followed by the complete native-provenance-aware make build.

@aunjgr aunjgr 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 3147b5b against base 2ea4ec2, including its integrated #28624 dependency. I approve the updated design at this exact revision and the implementation for its stated scope. Merge #28624 first, then this PR.

The previous protocol collision is resolved: main's complete prefix through v72 is retained, widened SUM uses v73, and extended discrete percentiles use v74. I verified the core SUM/group compatibility files match current #28624. The earlier independent design approval remains applicable to the retained ownership/typing model; the current rollout revision explicitly names the dependency and new allocations.

Inspected the full extension design, saved-string framing/provenance, generic discrete executor and cleanup, merge compatibility, allocation-accounted retained values/selectors, JSON/array exclusion at planner/executor admission, prepared ordinary/runtime compile-cache rejection, config preflight before child-scope construction, legacy numeric v17 and extended v74 gates, and their regression/BVT oracles. Raw metadata-magic lookalikes are unambiguous because all retained strings carry an envelope. Invalid/NULL then valid prepared-percentile executions have clean per-execution config.

No remaining confirmed blocker. Current-head CI is green. Reused earlier independent focused evidence and semantically unchanged review results; no fresh native suite, GPU, mixed-version deployment or benchmark run. JSON/array support and real release upgrade/downgrade qualification remain outside this approval's claims.

@daviszhen

Copy link
Copy Markdown
Contributor Author

Investigated the failing Ubuntu/x86 UT check. It contains no failed test assertion: the unsharded race suite reached its global timeout after 71m41s (make ut / exit 124). At termination, TestSpeedupAbortAllTxn was waiting for embedded-cluster admission while another package still held the cluster lease. The same test completed in about 19s in a contemporaneous main CI run once admitted, and it passes locally on this branch in 12s.

Merged current upstream/main without conflicts and pushed exact head b91902d436; this supersedes the timed-out check and starts a clean CI run.

New CI: https://github.com/matrixorigin/matrixone/actions/runs/34973047670
Validation after the merge:

  • go test ./pkg/frontend ./pkg/sql/colexec/aggexec ./pkg/sql/colexec/group ./pkg/sql/compile ./pkg/sql/plan -count=1
  • go test ./pkg/tests/issues/isolated -run '^TestSpeedupAbortAllTxn$' -count=1 -timeout=5m
  • go test -race ./pkg/sql/colexec/aggexec ./pkg/sql/compile -run 'Percentile|Prepared|RemoteExpr|Operator' -count=1

@daviszhen

Copy link
Copy Markdown
Contributor Author

Resolved the conflict with current main (091825aac2).

The conflict was an MORPC version allocation collision. main now owns v74 for corrected numeric HEX identities and v75 for persistent data-branch identity, so extended discrete-percentile inputs now use the next unique version, v76. The remote admission gate, diagnostic, boundary test, and MORPCLatestVersion were updated consistently.

New exact head: db43a5dff362ef3f0974e09b418b06de753eed07

Validation passed:

  • make config
  • go test ./pkg/sql/colexec/aggexec ./pkg/sql/compile -count=1

@daviszhen

Copy link
Copy Markdown
Contributor Author

Updated the PR head to 3d74413 by merging current main (26fa8c3).

Failure analysis:

  • Coverage was a downstream aggregation failure because UT Coverage did not publish a complete profile.
  • UT Coverage actual failure was a transient sql: no rows in result set in TestIssue26068DataBranchDatabaseIdentityLifecycle while resolving the just-created session through mo_sessions(). The test passes at the updated head locally.
  • Regular UT exhausted the outer 70-minute budget. Its diagnostics showed pkg/tests/arrowload taking 17.46m, including a 10.05m TestArrowLoadGateDisabled. Current main contains test(arrowload): share static gate cluster fixture #28961, which shares the static gate cluster fixture specifically to remove that duplicated startup cost. It also contains fix(shuffle): guard stop writing by pool ownership #28964 shuffle stop ownership guard.

Local verification:

  • go test ./pkg/sql/colexec/aggexec ./pkg/sql/compile ./pkg/sql/colexec/shuffle -count=1
  • go test ./pkg/tests/issues -run TestIssue26068DataBranchDatabaseIdentityLifecycle -count=1 -timeout 10m

Both passed. A new CI run has been triggered for the updated head.

@daviszhen

Copy link
Copy Markdown
Contributor Author

Updated again at head 762ac35 by merging current main (01d60e1).

Final state of the previous run:

  • UT Coverage: passed.
  • Coverage aggregation: passed.
  • Connector/J attempt 2: passed.
  • The only remaining root failure was Ubuntu UT; CI Required was downstream.

Ubuntu UT diagnostics:

  • The job hit the 70-minute outer budget.
  • TestDataBranchDiffAsFile completed every subtest successfully but its shared context expired during database cleanup after 160.85s.
  • TestCTASDivisionByZero exhausted its shared two-minute context under runner load, after which follow-up operations reported connection is already closed.
  • TestHandleRemoveReplica timed out while removing the replica and observed the old replica count.

Current main now includes #28936, which removes fixed lifecycle wait overhead, improves UT report execution, and runs the 20 independent DataBranchDiff scenarios with bounded parallelism. It landed after the failed run and its own full CI passed.

Local verification at the new head:

  • optools + aggexec + compile + shuffle: passed.
  • TestHandleRemoveReplica, count=3: passed.
  • TestCTASDivisionByZero: passed in 12.98s.
  • TestDataBranchDiffAsFile: passed in 15.37s (versus 160.85s in the failed CI run).

The MORPC v76 gate for this PR remains intact. A fresh CI run is now triggered.

@daviszhen

Copy link
Copy Markdown
Contributor Author

Updated head: 951ab6e.

Merged current main (8a4c84f) and resolved the aggregate-state/compiler/protocol conflicts. Extended PERCENTILE_DISC now uses MORPC v81 without reusing main's v76-v80 gates. Prepared APPROX_PERCENTILE keeps main's explicit DESC configuration and SQL NaN ordering. The PR compatibility description and design revision were updated accordingly.

The failed fulltext2_copy_alter.sql query was reproduced with two independent CN processes: the original case passes on the CDC writer CN but returns no rows for neutrino on the other CN (expected IDs 100 and 101). The test had warmed the replacement base before inserting new rows and assumed that a durable CDC tail also refreshed a remote CN's cache. That assumption contradicts the existing eventual-consistency cache contract.

The fix retains the original COPY ALTER/base/prewarm assertions and checks post-ALTER CDC on a separate replacement index that has not been searched. It also waits for cdc_tail chunk_id to advance beyond the pre-insert value and retains the expected 100/101 and 1/3/5 results, with an added base-table oracle. No fixed sleep, skipped assertion, forced rebuild, or production cache-consistency change was added.

Validation: make build; complete tests for aggexec, group, compile, plan, frontend, plan/function and fulltext2; cache/TVF regression tests repeated 3 times; corrected COPY ALTER BVT passed 4 runs across both CNs (47/47 assertions each); 4 percentile BVT scripts passed in the repository's CI mode (67/67 assertions).

GitHub reports mergeable=true. New CI run 35073833897 has been triggered and is not yet complete. Coverage and CI Required on the previous head were downstream failures of the Compose BVT, not a coverage percentage failure.

@mergify

mergify Bot commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-09-17 08:37 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • 🟠 Checks running · in-place
  • 🚫 Left the queue — 2026-09-17 08:43 UTC · at cc51fec0cc3292dbc6e56b4c64a16329119e9b22

This pull request spent 6 minutes 36 seconds 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 kind/feature kind/test-ci size/XL Denotes a PR that changes [1000, 1999] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants