beads/internal/storage/sqlbuild
Steve Yegge 96388ea019
fix(search): order Go-side-sort pages before every trim; honor sortDesc for id (#5639)
* fix(search): order Go-side-sort pages before every trim; honor sortDesc for id

Two live defects, both named-but-deferred in #5587's commit message and
both reachable from bd query '<expr>' --sort id (DefaultQueryLimit means a
bound is always set):

1. sqlbuild.Less ignored sortDesc for "id" — the one Go-side sort key —
   so every merge sort feeding a reversed id page kept the byte-FIRST
   rows. idSrcPage.sortGoSide's doc named this sibling bug explicitly.

2. #5587's fix (a page bound is only ever pushed under an order the query
   can express) covered the union seam only. The per-table seams
   (searchTable/scanFilterIDs, and the counts twin's
   finishSearchCountsPage) received the full matching set — searchWindowFor
   already withholds the page bound — but trimmed it with no sort anywhere:
   measured, 10 rows sort id desc limit 3 answered [000 001 002] where
   [009 008 007] was owed. The store-backed seam (issueops.searchTableInTxT)
   was worse off: it still pushed LIMIT into the leg SQL with no ORDER BY,
   so each leg contributed an arbitrary engine-ordered subset.

Fixes, one per seam, all converging on the same rule:

- sqlbuild.Less: sortDesc now flips the id comparison (byte order, matching
  the SQL the key would render if it could).
- domain/db: new sortRowsGoSide helper (the per-table copy of
  idSrcPage.sortGoSide) runs before finishWindow at searchTable's wide
  path, scanFilterIDs, and finishSearchCountsPage.
- issueops.searchTableInTxT: a Go-side sort leg no longer carries a SQL
  LIMIT; the leg scans the complete matching set, orders it in Go, and
  applies the same EffectiveSearchLimit bound after the order exists
  (goSideSortAndTrim), so cap accounting is unchanged and Pattern B
  hydrates only surviving rows.
- idProjection gains a comparator for the one key its payload can order
  ("id"), removing the merged-id-search landmine the old nil documented.

Tests: red-before-green verified on all three seams. sqlbuild unit test
pins Less; sqlmock tests pin the store-backed leg shape (no LIMIT under a
Go-side sort, bound still pushed under a SQL sort) and the ordered+trimmed
page; the domain/db suite adds per-table membership cases (asc, the
measured desc repro, and a four-route parity check: wisps-empty probe,
SkipWisps, wide path, counts twin must answer one page) plus a reversed
union page fence.

(bd-jao3t, bd-69c1a)

* fix(search): extend the Go-side-sort bound rule to the store-backed counts seam; one LessID comparator

Review round (adversarial pass on PR #5639):

F1 — runFilterSearchQueryInTx (issueops/search_counts.go) had the identical
defect the first commit fixed in searchTableInTxT: LIMIT eff pushed
unconditionally while sqlbuild.OrderBy renders no ORDER BY for "id". This
is the seam bd query '<expr>' --sort id actually reaches on the
store-shaped backends (storequerier → SearchIssuesWithCounts →
SearchIssuesWithCountsInTx), where BuildQueryPlan always pushes a bound —
so the first commit's repro claim held only for the uow/domain-db bodies.
Now the counts leg scans the complete matching set under a Go-side sort
and applies the same eff bound after ordering (goSideSortAndTrim),
leaving merge/trim/cap accounting exactly what the SQL LIMIT handed them.
bd list was already safe here (workapi.SQLLimit zeroes the pushed limit
for --sort id); only the querier path pushed a bound.

F2 — the byte-order id comparison existed in five hand-rolled copies
(Less, idProjection.less, goSideSortAndTrim, sortRowsGoSide,
idSrcPage.sortGoSide) — the exact duplication that let Less drift from
sortGoSide in the first place, and that hid F1's sixth site. All five now
call the one exported comparator, sqlbuild.LessID.

F3 — the ascending per-table suite case is annotated as the weak witness
it is (engine order for a PK-clustered prefix scan tends to be
byte-ascending already); the DESC cases are the red-before-green fences.

New shape test pins the counts legs: under --sort id neither the issues
nor the wisps counts query may carry a LIMIT (red on the previous commit:
the leg query carried LIMIT 3).

(bd-jao3t, bd-69c1a)
2026-08-11 00:24:28 -07:00
..
counts.go feat(workapi,storage): opt-in Brief projection for the list and ready pages (#5554) 2026-08-10 10:45:23 -07:00
filter.go perf(search): filter the main table before the counts aggregate joins (#4480) 2026-07-26 13:04:52 -07:00
filter_test.go fix(filters): Dolt label-filter regression fix — LabelPattern + LabelRegex + JSON truncation hint suppression (#3971) 2026-07-24 00:17:02 -07:00
imports_boundary_test.go refactor(storage): extract shared SQL builders into internal/storage/sqlbuild (bd-6dnrw.46) 2026-06-12 15:56:23 -04:00
keyset_test.go feat(storage): keyset reads — comments page, issue-list cursor, IsBlocked filter (#4922) 2026-07-21 22:29:09 -07:00
labels.go refactor(storage): extract shared SQL builders into internal/storage/sqlbuild (bd-6dnrw.46) 2026-06-12 15:56:23 -04:00
ready.go perf(search): filter the main table before the counts aggregate joins (#4480) 2026-07-26 13:04:52 -07:00
reclaim.go feat(reclaim): scope filters --label/--label-any/--exclude-label/--assignee/--id (wy-jpd3.3) 2026-07-24 05:31:23 -07:00
reclaim_test.go feat(reclaim): scope filters --label/--label-any/--exclude-label/--assignee/--id (wy-jpd3.3) 2026-07-24 05:31:23 -07:00
sort.go fix(search): order Go-side-sort pages before every trim; honor sortDesc for id (#5639) 2026-08-11 00:24:28 -07:00
sqlbuild.go feat(workapi,storage): opt-in Brief projection for the list and ready pages (#5554) 2026-08-10 10:45:23 -07:00
sqlbuild_test.go fix(search): order Go-side-sort pages before every trim; honor sortDesc for id (#5639) 2026-08-11 00:24:28 -07:00