Skip to content

fix(orm): serve multi-field orderBy from a compound index - #426

Merged
zbeyens merged 9 commits into
mainfrom
fix/orm-multi-field-orderby-index-pushdown
Aug 26, 2026
Merged

zbeyens merged 9 commits into
mainfrom
fix/orm-multi-field-orderby-index-pushdown

Conversation

@MikeyZhang75

@MikeyZhang75 MikeyZhang75 commented Aug 21, 2026 •

Copy link
Copy Markdown
Collaborator
  • Auto release

🐛 Fixes #416

🧭 Task plan: docs/plans/416-orm-multi-field-orderby-index-pushdown.md

🟢 95-100% confidence

Phase 🧪 Tests 🌐 Browser
Reproduced 🔴 A limit-5 query read 60 rows; a relation read 61; four exact-head reviews exposed distinct tied/top-k regressions ➖ N/A
Verified 🟢 Focused ORM 173 passed / 1 skipped; owner/compiler 52 passed; bun check green ➖ N/A

✅ Outcome

A multi-field orderBy walks a declared compound index only when that index produces the complete requested order without changing tied-row identity. Safe top-level and relation queries stay limit-bound instead of collecting the full candidate set.

⚠️ Caveat

Mixed directions, nullable fields, skipped keys, unsafe pinned-direction ties, and unrequested trailing index keys keep the post-fetch sort. Add createdAt in the moving direction when an explicit creation-time tie-break makes a pinned sort safe.

🏗️ Design

resolveIndexOrderPushdown owns whole-sort eligibility. Equality-pinned fields are absorbed without consuming a moving key; remaining fields must be contiguous, share one direction, and consume every moving declared key before the implicit creation-time suffix. Candidate scoring prefers fuller indexes only when their extra coverage is safe.

🧪 Verified

  • RED/green integration proof covers two table sizes, relation bounds, cursor direction, Convex UTF-8 and Float64 ordering, pinned-field ties, and trailing-key ties across limit.
  • Four exact-head P1 findings were reproduced and repaired: equality-pinned index scoring, Convex value ordering, opposite-direction implicit ties, and unrequested trailing-key ties.
  • Final immutable-head autoreview: no actionable P0/P1 findings at 0.89 confidence.
  • bun check: green across lint, typecheck, unit/integration suites, CLI and Concave smoke, fixture parity, package builds, and bare/Expo/Next/Start/Vite runtime lanes.
  • Docs, published kitcn skill guidance, generated mirror, and patch changeset are synchronized.

View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.

Any 2+ field `orderBy` collected the whole table and re-sorted in JS, even
when a declared compound index produced that exact order. Two independent
bails caused it: `resolveIndexOrderPushdown` refused anything but one spec,
and the planner re-decided on `orderSpecs.length` afterwards. Three of five
call sites truncated the spec list before the helper ever saw it.

The helper now walks the list: eq-pinned fields are absorbed at any position
and direction, the rest must be contiguous from the first unpinned key in one
shared direction, and `_creationTime` is legal only as the final spec. The
planner reads that answer instead of counting specs, and index selection
scores how much of the sort a candidate carries.

Sorts over a column that can be missing or null keep the post-fetch sort:
Convex orders those first ascending and `_compareByOrderSpecs` orders them
last, so only sorts the two agree on may move into the index.

Also fixes cursor pages ordering the wrong way when an eq-pinned field leads
the sort and the next spec points the other way.
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Repo admins can enable using credits for code reviews in their settings.

@changeset-bot

changeset-bot Bot commented Aug 21, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 1c1d8e8

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
kitcn Patch
@kitcn/resend Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@vercel

vercel Bot commented Aug 21, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
better-convex Ready Ready Preview Aug 26, 2026 4:54am

Request Review

@zbeyens

zbeyens commented Aug 26, 2026

Copy link
Copy Markdown
Member

Autoclosure receipt for exact head 1c1d8e86f3acfe1f44e6be640d3f72df67e6e30c.

  • Task evidence: docs/plans/416-orm-multi-field-orderby-index-pushdown.md exists at this head, owns PR fix(orm): serve multi-field orderBy from a compound index #426, and passes the completion checker.
  • Exact-head equality: local HEAD, live headRefOid, remote branch, and fetched refs/pull/426/head all equal the receipt OID.
  • P1 proof replay after the final push: 12/12 focused read-bound/order tests and 52/52 resolver/compiler tests passed. The four repaired P1s cover equality-pinned scoring, Convex value ordering, opposite-direction implicit ties, and unrequested trailing-key ties.
  • Final review: no actionable P0/P1 findings (0.89 confidence).
  • Local gate: bun check passed, including lint, typecheck, full tests, package builds, fixture parity, and runtime scenarios.
  • Hosted gates: CI passed; Vercel deployment is ready; auto-release sync passed.
  • Final feedback inventory before receipt: helper comments 2, raw issue comments 3, excluded raw comments 1, review bodies 0, review threads 0 resolved / 0 unresolved. Priority totals: P0 0, P1 0, P2 0, P3 3. Deferred items: none.

Feedback ledger:

Zero unresolved actionable P1-or-higher feedback remains.

This branch was successfully deployed

1 active deployment
Preview — 1c1d8e86 Deployed Aug 26, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ORM: any 2+ field orderBy forces a full collect() + JS sort, even when a compound index serves it exactly

2 participants