Repository navigation
fix(query): realize sum(position) of an AVERAGE account through booking (#2394) - #2529
Conversation
…ng (#2394) `SELECT account, sum(position) ... GROUP BY account` summed an AVERAGE account's postings and pooled every cost-bearing lot of the commodity. A sale is a negative lot in that sum and so is a short, so an account holding both netted one into the other: `-2 X {101}` beside `3 X {102}` came out as `1 X {104}`, a lot nobody held, while BALANCES was right. The group is now replayed through `BookingEngine::replay_transaction`, the walk BALANCES uses, one subset transaction per source transaction, and presented as one weighted-average pool per side. `Inventory::merge_average` now pools the long lots and the short lots apart, since its input is realized and a negative lot is a short. The AVERAGE check also reads the ledger's default method, so an account that books AVERAGE through `option "booking_method"` is realized the same way. A subset booking cannot realize keeps the plain sum. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
📊 Benchmark Results
vs base, measured in this run
A difference counts only when it clears the combined spread of the two measurements, per shape (2σ: What do these numbers mean?
|
✅ Compatibility Test Results
What's tested
Full results available in workflow artifacts |
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
A property test generates ledgers with AVERAGE accounts (on the open and through the ledger default), FIFO and STRICT accounts, two commodities, two cost currencies, and buys, sales, shorts and a short beside a long, keeping only what booking accepts. For every FROM filter, and the same filter as a WHERE, an AVERAGE account's sum must equal its BALANCES inventory pooled per side, and any other account's must equal BALANCES exactly. A companion test asserts the generator reaches realized and mixed AVERAGE accounts, so the property cannot pass by skipping. Documents the AVERAGE `sum(position)` semantics in bql.md, including the plain-sum fallback for a subset booking cannot realize, and amends ADR-0007, whose last amendment still said `merge_average` pooled both sides and that the mixed case was open. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
Adversarial review (author self-review)1. Property:
2. Should the fallback stay silent? Decision: keep the plain sum, and document it.
3. The no-sale divergence.
4. The three inventory walks.
Pushed in ddeb5d4: the property test, the bql.md section, the ADR amendment, and a corrected Second pass: other docs on AVERAGE pooling and
|
The #2393 CHANGELOG entry still said `sum(position)` nets the two sides, and ADR-0007's Decision still described `merge_average` pooling every lot of a commodity, with nothing pointing at the amendments. The Breaking entry now tells a library caller what `merge_average` does differently and how to get a realized input. bql.md names the ledger default method. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
#2529 changed `group_is_single_average_account` in aggregation.rs to resolve an account's method the way booking does: the `open`'s method, else the ledger's. This branch had moved that check into `account_is_average`, which the row-filtered BALANCES path also calls. The helper now carries #2529's resolution, so the partial-BALANCES path also sees AVERAGE under a global `option "booking_method"`. Tests added on top of #2529's: - sum_position_balances_agreement now also runs the posting-level filters `FROM number > 0` and `FROM number < 0`, which select part of an account. That is the case where BALANCES sums the selected postings instead of reading booking's inventory. 3,000 cases pass. - a targeted test pins BALANCES and sum(position) to the same lots on a partly selected AVERAGE account holding two longs, a short and a sale. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
What changed
sum(position)over a group drawn from one AVERAGE account no longer sums the postings and then pools them. It replays the group's postings throughBookingEngine::replay_transaction, the canonical walk thatBALANCESandaccount_balanceuse, with one subset transaction per source transaction. It then presents the result as one weighted-average pool per side.Inventory::merge_averagepools the long lots and the short lots of a commodity separately. Its input is now a realized inventory, where a negative lot is a short. (This changes the behavior of a public function, so the CHANGELOG entry is under Breaking.)register_account_methods/method_for: theopen's method if it parses, otherwise the ledger's. An account that books AVERAGE throughoption "booking_method"is now realized as well. Previously it listed the sale's lot beside the lots it sold from (10 {100},10 {110},-14 {105}).Why
The issue shows that a sum of postings cannot tell a sale (a negative lot at the pool's cost) from a short, so no rule inside
merge_averagecan fix this. #2393 tried "leave mixed lots unmerged" and reverted it. Booking can tell them apart. Once the group has been realized, a negative lot can only be a short, and pooling each side separately is correct. That is the direction the triage suggested. The stopgap (leave the pool unmerged) is used only as the fallback when the subset cannot be realized.Grouping postings by transaction keeps
replay_transaction's booking order inside each transaction (#2380). Each subset transaction holds only the group's postings, which gives #1985's filtered-subset semantics. On aWHERE year = 2021subset the result matchesBALANCES FROM year = 2021.Verification
Issue ledger (
Assets:Stock, AVERAGE, a short at 101 and a long at 102, then a sale of 2):sum(position) GROUP BY account1 X {104 USD}3 X {102 USD} -2 X {101 USD}BALANCES3 X {102 USD} -2 X {101 USD}Other shapes, main vs branch:
8 X {111}8 X {111}option "booking_method"10 {110} 10 {100} 2 {130} -14 {105}8 X {111}WHERE year = 2021subset-12 X {101}-12 X {105}BALANCES FROM year = 2021:-12 X {105}10 X {110} 2 X {130}Oracle: beancount 3 refuses this ledger (
bean-check: "AVERAGE method is not supported"), so for AVERAGE the reference is rledger's ownBALANCESand booking. The new tests assert agreement withBALANCES. For the non-AVERAGE shapes,bean-querygives the same output as main and the branch.crates/rustledger-query/tests/sum_position_average_realized.rs, 4 tests. On main, 3 fail: the long/short case ([(1, 104)]), the ledger-default case (dangling lots), and the unrealizable subset (-12 {103.48…}). The single-sided regression guard passes on main.merge_average_pools_each_side_apart(core) anda_mixed_sign_pool_keeps_each_side_at_its_own_cost(booking) fail whenmerge_average's side key is reverted to currency-only.merge_average_collapses_…with a sale lot,merge_average_net_zero_removes_lots,a_mixed_sign_pool_is_correctly_rounded_through_the_escalation). After AVERAGE pools long and short lots together: wrong basis and a vanished short on a mixed account #2393 and this change, no path averages a long and a short together, so the mixed-sign escalation regime is unreachable. Same-sign escalation stays covered byaverage_cost_properties.cargo test -p rustledger-core -p rustledger-booking -p rustledger-query --all-features: 1707 passed, 0 failed. Workspace clippy, fmt, andcargo doc -D warningsare clean.No CACHE_VERSION bump: this is query-time only, and booking output is unchanged.
Known remaining divergence (pre-existing, unchanged): for an AVERAGE account with several buys and no sale yet,
sum(position)shows the pool (20 X {160}, pinned bytest_average_account_sum_position_merges_to_single_pool).BALANCESlists the lots, because booking merges only when it sells.Closes #2394
Refs #2388 (designed together, shipped separately).
🤖 Generated with Claude Code
https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr