Skip to content

fix(sql): enable leading-key pruning for distributed Top-K scans - #32242

Merged
XuPeng-SH merged 8 commits into
matrixorigin:4.2-devfrom
ck89119:issue-32219-4.2-dev
Nov 9, 2026
Merged

XuPeng-SH merged 8 commits into
matrixorigin:4.2-devfrom
ck89119:issue-32219-4.2-dev

Conversation

@ck89119

@ck89119 ck89119 commented Nov 5, 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 #32219.

What this PR does / why we need it:

Multi-column ORDER BY ... LIMIT skipped Top-to-scan dynamic filtering, and remote execution lost the Top message tag and runtime scan ordering. Consequently, a small result limit did not bound scan work.

  • Enable leading-key block pruning while retaining the complete Top-K comparator, equal-key candidates, and conservative NULL/type/name-mapping fallbacks. Do not introduce a leading-key-only static row limit. Spatial leading keys now keep the full-scan fallback because storage zonemap comparisons do not support them.
  • Preserve remote Top tags and scan ordering; implement reader-owned remote block ordering and filtering without mutating shared block lists.
  • Detach zonemaps retained by both local and remote ordered scans, so slice views do not prevent complete object metadata from being reclaimed.
  • Preserve InputBlocks through remote statistics serialization.

Validation (Go 1.26.4, Darwin/arm64):

  • Focused regressions and consumer checks, six owning-package suites with coverage (unchanged suites reused), and incremental vet/lint on the updated 4.2-dev base.
  • Local-reader race: 100 focused repetitions plus the disttae package. Prior remote-reader race evidence (100 repetitions plus the readutil package) is reused.
  • Both local and remote metadata-ownership regressions fail before their respective fixes and pass afterward: initialized and unknown zonemaps retain identical values with independent, fixed-size backing storage. Local tests also preserve ASC/DESC ordering and threshold boundaries.
  • Rebuilt the service and reran mo-tester BVT on one/two CNs, twice per topology (57/57 each). Two persisted GEOMETRY objects cover TP/AP, ASC/DESC and optimization-disabled controls; geometry/geometry32 planner tests verify that no ordered scan or dynamic message is installed.
  • Changed-line coverage: 93.33%; statement-weighted coverage of touched blocks: 88.98%. The spatial eligibility fix and both metadata-retention fixes are covered.
  • On a synthetic 131,072-row/16-block layout, scans drop to 1 block on one CN and 4 blocks on two-CN AP execution, versus 16 with filtering disabled. Equal-leading-key cases retain all four blocks; complete results remain correct.

The original 30-second stability timeout was not reproduced. These measurements validate the scan optimization, not a unique production root cause or a guaranteed resolution of the incident.

Preserve multi-key Top-K semantics while enabling leading-key block filtering and restoring remote Top/scan metadata. Implement private remote block ordering and preserve remote InputBlocks statistics.

Related to matrixorigin#32219.
@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 commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

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

Commit: e956471de326eae0f1a813e6f99b65fa8b952a1f

Review completed. Read the assessment.

@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 the full change at e956471, including planner eligibility, Top/remote codecs, reader-local block ordering, threshold application and scan-counter serialization. Leading-key pruning retains equal-key candidates and the full Top-K comparator, does not install a multi-key static row limit, and falls back for nullable or ambiguous mappings. Reader ordering does not mutate shared RelData; cancellation/error publication and reset paths are covered. No blocking findings.

Reused exact-head successful CI and assessed the focused component/race and one-/two-CN SQL evidence. These results support the optimization, not a claim that the original production timeout was uniquely reproduced or resolved.

@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: e956471de326eae0f1a813e6f99b65fa8b952a1f

Assessment: request_changes

The existing Top/reader design is coherent, but the P2 metadata-retention defect remains. The new approval confirms correctness and cleanup without addressing the whole-object buffers pinned by the remote permutation.

Model: gpt-6.1-sol / max

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

  • P2 pkg/vm/engine/readutil/datasource.go:301: A remote ordered Top-K scan spans many wide objects, even when LIMIT is small or only a few blocks per object survive range filtering.
    Impact: The permutation retains every object's complete uncompressed metadata until reader close, defeating metadata-cache eviction and causing a potentially multi-GB heap increase before the first block is returned.
    Evidence: GetBlockMeta (objectio/meta.go:66), GetColumnMeta (objectio/column.go:40), and ZoneMap (column.go:93) all return views into the same allocation. Storing these views in orderedBlocks pins metadata for all columns and blocks; writer.go:417 builds that metadata in one buffer. For example, 1,000 objects with 128 blocks and 100 metadata columns retain at least 1.59 GB, although one selected zonemap per object needs only 64 KB. The previous remote path retained no such list. Clone/detach each zonemap before storing it, using the existing ZoneMap.Clone capability as SetFilterZM already does.

Validation:

  • Recovered the complete 15-file review and successful static/descriptor/17-query reference checks from attempt 1, matching retained commands, source hashes, exits and logs.

  • Recovery audit passed (checks/1793935844639769841-2.json; recovery-check-final.log). Frozen source, patch, PR body, linked issue and successful CI are unchanged.

  • Assessed all three discussion items, including approval 5790146917, against the retained source evidence.

Limitations:

  • No local Go, MatrixOne or memory-profile execution; CI and author reports lack supplied raw execution logs.

  • Metadata amplification is established by source ownership and layout, not an observed OOM. The original incident timeout remains unverified.

Clone retained zonemaps so remote block ordering cannot prevent full object metadata from being reclaimed after cache eviction. Add a regression using the existing persisted-block fixture to verify equal values and independent fixed-size backing storage.

Addresses the metadata-retention review in matrixorigin#32242. Related to matrixorigin#32219.
@XuPeng-SH

XuPeng-SH commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

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

Commit: 92bde96fb753adfb2a345ac9a0036e26131d17f6

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: 92bde96fb753adfb2a345ac9a0036e26131d17f6

Assessment: request_changes

The design reuses existing Top and reader mechanisms, and both prior metadata-retention findings are resolved. Newly eligible GEOMETRY leading keys still expose an unsupported-type panic.

Model: gpt-6.1-sol / max

Session: 01a1a004-9884-72c0-ad39-f096c4209c4a · slot 1 · generation 0

  • P2 pkg/sql/plan/message.go:79: A query such as SELECT id FROM t ORDER BY g DESC, id DESC LIMIT 1 uses a GEOMETRY NOT NULL column, and an ordered reader receives at least two persisted blocks with initialized g zonemaps.
    Impact: The previously supported multi-key query reaches an unsupported-type panic instead of completing.
    Evidence: GEOMETRY passes encodedOrderMatchesSQLOrder, which only rejects floats. Its persisted zonemaps are initialized. Remote block sorting at readutil/datasource.go:317/319 and local sorting call ZM.CompareMax/CompareMin, reaching compute.Compare, which lacks GEOMETRY support and panics at pkg/vm/engine/tae/compute/compare.go:140. Baseline rejects multi-key message installation, while ordinary Top supports GEOMETRY. Keep unsupported comparison types on the full-scan fallback or implement matching comparator support, and add a persisted multi-key regression with an optimization-disabled control. Source proof is retained in geometry-source-trace.json.

Validation:

  • Reviewed all 17 changed files, affected consumers, all 9 discussion entries and linked issue context; saved review-plan.md and discussion-assessment.json.

  • Source audit passed: clean frozen head, diff whitespace, gofmt on 14 Go files, and generated protobuf descriptor/codec consistency.

  • Independent SQLite reference matched all 17 expected row sets.

  • Static GEOMETRY call-path and baseline assertions passed; no runtime reproduction is claimed.

  • Private /tmp and loopback preflight passed. All five check receipts, including the superseded probe error, match terminal exits, source fingerprints and retained logs in validation-review.json.

  • Reused frozen current-head successful build, UT, SCA, coverage and proxy/multi-CN BVT statuses.

Limitations:

  • No local Go tests or MatrixOne SQL execution; the finding is established by source reasoning.

  • CI and author execution evidence was supplied as summaries without raw logs. The prior approval body names another head and was not treated as current-head local execution proof.

  • The original production timeout remains unreproduced; scoped pruning evidence does not establish its root cause.

Reject geometry and geometry32 in the shared encoded-order eligibility check because storage zonemap comparators do not support spatial types. Retain the full logical Top comparator and full-scan fallback instead of installing dynamic messages or ordered reader limits.

Add planner family regressions and persisted GEOMETRY TP/AP SQL controls with the optimization enabled and disabled.

Related to matrixorigin#32219. Addresses the spatial-key review in matrixorigin#32242.
@XuPeng-SH

XuPeng-SH commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

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

Commit: 6810d5f04e27b91d808fc0be0c96dcb928b44b39

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: 6810d5f04e27b91d808fc0be0c96dcb928b44b39

Assessment: approve

Leading-key pruning preserves full Top-K ordering and conservative fallbacks. Reader-owned permutations and detached zonemaps fit existing lifecycle owners; prior metadata-retention and spatial-panic findings are resolved.

Model: gpt-6.1-sol / max

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

Validation:

  • Reviewed all 18 changed files, affected consumers, linked issue context and all 11 discussion entries; saved complete discussion dispositions.

  • Source/descriptor audit passed: frozen patch, clean source, whitespace, gofmt on 15 Go files and protobuf field/codec consistency. Receipt: checks/1794034649670401398-3.json; source-descriptor-audit.log.

  • All 25 expected SQL row sets matched an independent relational reference. Receipt: checks/1794034649369237908-3.json; sql-reference-audit.log.

  • Isolation preflight passed. Receipt: checks/1794034507655361414-3.json; preflight.log.

  • Reused frozen-head successful CI statuses and attributed focused, race and distributed BVT reports at their supplied scope.

Limitations:

  • Native testing stopped before compilation because cgo/libmo.so was missing. Receipt: checks/1794034517265568015-3.json; native-readutil.log. No fresh linked Go/race or MatrixOne execution is claimed.

  • External CI and author results were supplied as summaries without raw logs.

  • The original stability timeout remains unreproduced; scoped pruning evidence establishes no production latency guarantee.

This branch was successfully deployed

No deployments
ci — 54cb0942 Deployed Nov 9, 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.

4 participants