You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
compareStats in pkg/sql/plan/stats.go is not a transitive (strict weak) ordering, so it cannot be used as a slices.SortFunc comparator and blocks migrating its two call sites in pkg/sql/plan/join_order.go off sort.Slice.
The 0.01 selectivity tolerance makes it intransitive. Counter-example (from review): A=(sel=0.000, outcnt=3), B=(sel=0.009, outcnt=2), C=(sel=0.018, outcnt=1):
B < A — |0.009-0.000| ≤ 0.01 → compare outcnt: 2 < 3 ✓
C < B — |0.018-0.009| ≤ 0.01 → compare outcnt: 1 < 2 ✓
A < C — |0.000-0.018| > 0.01 → compare selectivity: 0.000 < 0.018 ✓
So B < A < C < B — a cycle. sort.Slice tolerates this (produces some order); slices.SortFunc requires a strict weak ordering and its result is undefined for a comparator like this.
No known incorrect result today (it's a heuristic ordering), but the comparator is a latent contract violation.
Proposed fix
Refactor compareStats into a genuinely transitive ordering key, e.g. bucket the selectivity onto a fixed 0.01 grid and sort lexicographically by (bucket, outcnt), so equal-bucket selectivities compare by outcnt and the relation is transitive. Note this changes tie behavior at bucket edges, so it may shift some join-order plans and require EXPLAIN golden updates — hence it is tracked separately from the mechanical sort→slices migration.
Once transitive, migrate the two join_order.go sites to slices.SortFunc.
Related: #25646 (the same class of non-strict/undefined comparator in the filter-cost sort).
Fixed in PR #25616 (commit cfd173f): compareStats now buckets selectivity onto a fixed 0.01 grid (floor(sel/0.01)) and compares lexicographically by (bucket, outcnt) — a transitive strict weak ordering — returning a 3-way int. The two join_order sort sites are migrated to slices.SortFunc. Behavior-neutral for the test suite (swept optimizer/join/cte/subquery/recursive_cte/view/distinct/window/… — no plan or result changes; the grid only differs from the old sliding window at 0.01 boundaries). Added TestCompareStatsIsStrictWeakOrdering (the review counter-example is now cycle-free + a brute-force transitivity check). Not set to auto-close per your request.
Activity
fix(review): validate distance bounds, dedupe literal decoder, revert…
fix(plan): make compareStats transitive via 0.01 selectivity bucketing
fengttt commentedon Jul 14, 2026
Fixed in PR #25616 (commit cfd173f): compareStats now buckets selectivity onto a fixed 0.01 grid (
floor(sel/0.01)) and compares lexicographically by(bucket, outcnt)— a transitive strict weak ordering — returning a 3-way int. The two join_order sort sites are migrated toslices.SortFunc. Behavior-neutral for the test suite (swept optimizer/join/cte/subquery/recursive_cte/view/distinct/window/… — no plan or result changes; the grid only differs from the old sliding window at 0.01 boundaries). AddedTestCompareStatsIsStrictWeakOrdering(the review counter-example is now cycle-free + a brute-force transitivity check). Not set to auto-close per your request.perf: replace stdlib sort.Slice with slices pkg to eliminate heap esc…
perf: replace stdlib sort.Slice with slices pkg to eliminate heap esc…
perf: replace stdlib sort.Slice with slices pkg to eliminate heap esc…
perf: replace stdlib sort.Slice with slices pkg to eliminate heap esc…