min_max_order_by_property_test compared MAX with the LAST value ORDER BY
gives. The position order ties values that are not equal, as
beancount's Position.sortkey does for two lots differing only in date;
the stable sort keeps them in input order, so its last value was the
second lot while MAX (strict comparison) keeps the first. It failed
whenever proptest drew such a tie, at random on unrelated PRs.
The property now says MIN is the first value under ORDER BY and MAX the
first under ORDER BY ... DESC, which is exact with ties. The two CI
seeds are committed in the proptest-regressions file. A 3,000-case run
finds no disagreement on untied values. Docs state the tie rule; an
explicit test pins it. Cases go from 64 to 32 because each case now
runs both sort directions.
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
What failed
min_max_order_by_property_test::min_max_equal_order_by_ends, added in #2493, failed intermittently in CI on unrelated PRs (#2526, #2527); both passed on rerun. The two shrunk cases from the CI logs:Assets:G0 8 X {18 USD}on 2024-02-02 andAssets:G1 8 X {18 USD}on 2024-02-03: the per-account inventories from a subquery, thenmax(s).Assets:G0 4 X {14 USD}, andAssets:G1holding4 X {14 USD}plus USD postings that net to zero.Diagnosis: a flaw in the property; MIN/MAX and ORDER BY are correct
In both cases the two inventories tie under the order without being equal. The position order is beancount's
Position.sortkey(currency rank, cost number, cost currency, units number). It ignores a lot's date and label, so the two lots compare equal. Checked in beancount 3.2.3:ORDER BY v. With a tie that is the second lot. That holds only when the top value is unique.Not a MIN/MAX/ORDER BY disagreement on untied values. With the corrected property, a 3,000-case release run finds none.
Should lots differing only in date tie? Yes. This is beancount's own
Position.sortkey, which bean-query's ORDER BY uses, and ties already keep input order, which is deterministic. Breaking ties by date would reorder rows away from bean-query for no correctness gain. Unchanged.bean-query on the same tie (
8 X {18 USD, 2024-01-01}then8 X {18 USD, 2024-02-03}):>falls back to plain tuple comparison, which compares the cost date. This is part of the already documented MAX divergence (§15). rustledger returns the first lot for both.Fix
ORDER BY v, and MAX the first underORDER BY v DESC. Because ORDER BY is stable and the aggregates use strict comparisons, this holds exactly, ties included. The test is renamedmin_max_equal_first_value_of_each_sort_direction.min_max_order_by_property_test.proptest-regressions, next to the test, as the booking and core crates do. They re-run deterministically before any random cases.min_max_return_the_first_of_tied_lots: MIN and MAX both return the first of two date-only-different lots, and that lot is the first row of ORDER BY in both directions.bql.md:max(x)is "the first valueORDER BY x DESCwould give".compatibility.md§15: states the tie rule and bean-query's MAX tie behavior.value_less_thancomment and the CHANGELOG wording for BQL: MIN/MAX over amounts and positions fail with 'cannot compare values'; they should follow ORDER BY's order #2447 now say "first value ascending/descending" instead of "first and last".No query output changes.
Verification
cargo test -p rustledger-query --all-features: 810 passed across 38 binaries. Workspace clippy-D warnings, fmt andcargo doc -D warningsare clean.Refs #2447
🤖 Generated with Claude Code
https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr