Repository navigation
fix(query): cost(position) reads the posting's booked cost - #2428
Conversation
`cost(position)` multiplied the Position value's per-unit cost back out,
and a lot bought as `3 X {{500 USD}}` has a per-unit cost of 500/3,
rounded: it gave 500.0000000000000000000000001 where `weight` gives 500,
and so did the sale that empties such a lot, so an emptied account's
`sum(cost(position))` printed `-0 USD`.
Over the position column it now takes the posting's booked cost through
the canonical `cost_number_weight`, the route `weight(position)` already
takes (#1966): exact for a total-carrying cost, and the same
`units x per-unit` for every per-unit one. The posting table's `cost`
column did the same multiplication with an unchecked `*`; it uses the
same rule now, unsigned as before. `cost()` of any other argument still
works from the per-unit cost.
Closes #2425
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
Mutation testing left the route's guard unpinned: forcing it true, or `&&` to `||`, sent every `COST(x)` to the posting's cost, and the only non-column case tested was `cost(sum(position))`, which is aggregated elsewhere. `cost(units(position))` per row is the units themselves. 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 |
VitePress parses `{{ ... }}` as a Vue expression even inside inline code,
so the docs build failed on `3 X {{500 USD}}`. No other docs page writes
the syntax; say it in words instead.
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
`Inventory::at_cost` multiplied each lot's per-unit cost back out, so a
held `{{500 USD}}` lot of 3 showed 500.0000000000000000000000001 under
`BALANCES AT COST`. It now reads the lot's total where the inventory
keeps one (#2427). BQL's balances come from the booking engine's
replay, whose inventories keep those totals; an inventory built without
them values its lots as before.
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
Deep review (three passes)Fixed in 3823b70: Fixed in 8aaeed0:
Pass 3: compat corpus and raw scale (docs only, 3d427c5). I fetched the BQL compat corpus and ran all 19 queries on its 29 ledgers with a total or compound cost, against bean-query 0.2.0, on this branch and on main.
Pass 2, 80e3d8e (no behavior change):
Probed, correct on this PR:
Found, not fixed here (pre-existing, same class), filed as #2429 and #2430:
Both are documented in |
Each row's position AT COST was `units x per-unit` (with a bare `*`),
and its balance was `at_cost` of the running balance, which multiplies
the rounded per-unit cost back out: a `{{500 USD}}` lot showed
500.0000000000000000000000001 on its row and in the balance. The
running balance uses the shared backing (#1086), which keeps no lot
totals, so `at_cost` cannot be made exact there.
The row now shows the posting's booked cost (`compute_posting_cost`, as
`cost(position)` gives it), and the balance is the running sum of those
row costs, which is what `at_cost` of the cumulative balance is: a
sale's cost nets its buy's, and cost-less amounts pass as units.
Unchanged for per-unit lots, including a sale that spans lots.
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
The arm's guard called `compute_posting_cost` and the body called it again, so every `cost(position)` row paid for the canonical weight twice. The arm now matches on one call and falls back to the value path, which moves into `evaluate_on_argument_values` so the default arm and the fallback share it. JOURNAL AT COST also stops building the running balance it no longer reads; its balance is `cost_balance`. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
…scale
Running the BQL compat corpus's 29 total/compound-cost ledgers through
both builds: all 551 (file, query) pairs keep their status, and the
rendered text of `JOURNAL ... AT cost` is identical, but the raw JSON
changes on 19 pairs. `cost(position)` now carries the booked total's
scale, the weight column's: 300.00 where it was
300.00000000000000000000000000, and 305 for `2 HOOL {150 # 5 USD}`
where the re-multiplied per-unit gave 305.00. Say so for JSON and
embedder consumers.
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
The rest of #2425. #2427 made lots keep their exact totals; this makes BQL's
cost()agree with them.Problem
cost(position)read aValue::Position, which holds only the resolved per-unit cost. A lot bought as3 X {{500 USD}}has a per-unit cost of 500/3, rounded, so:cost(position)of the buycost(position)of a sale that takes 1 of those 3weightof eithercost(position)sum(cost(position))of an AVERAGE pool (1 at 100 and 2 at 200) sold whole-0 USD(-1E-26)Fix
COST(position)over the position column now takes the posting's booked cost through the canonicalrustledger_booking::cost_number_weight. It's the same routeWEIGHT(position)has taken since WEIGHT() ignores the price annotation, returning units in the wrong currency #1966, viais_position_column.PerUnitFromTotal: a{{T}}buy, or the sale that empties such a lot).units × per-unit).The posting table's
costcolumn did the same multiplication, with an unchecked*that panicked on overflow. It uses the same rule now, unsigned as before.BALANCES AT COST(Inventory::at_cost) now reads a lot's exact total where the inventory keeps one. The balances come from the booking engine's replay, which keeps those totals, so a held{{500 USD}}lot shows 500 there, not 500.00…01.JOURNAL … AT COST:compute_posting_cost). Before, it wasunits × per-unitwith a bare*.at_costof the cumulative balance is, but exact. The running balance uses the shared backing (perf(query): JOURNAL clones growing cumulative_balance per matching posting (same shape as #1080, separate path) #1086), which keeps no lot totals, soat_costcouldn't be made exact there.Still per-unit (the same limit
WEIGHTaccepts, documented indocs/reference/bql.md):cost()of any other argument, for examplecost(sum(position)).sum()builds its inventory from barePositionvalues, which have no totals. Tracked in BQL: cost(sum(position)) multiplies the rounded per-unit cost back out (500.00…01 for a {{500 USD}} lot) #2430.cost(position)overFROM #postings. That table's rows carry no posting.WEIGHT(position)has the same gap there, and it predates this PR: a priced10 EUR @ 1.10 USDgives10 EURthere, on main too, whereweightgives11.00 USD. That's WEIGHT() ignores the price annotation, returning units in the wrong currency #1966's fix not covering the table path. Tracked in BQL: weight(position) and cost(position) over FROM #postings skip the posting route (10 EUR for a priced posting; 500.00…01 for a {{T}} lot) #2429.Scale of the raw value.
cost(position), thecostcolumn andJOURNAL … AT costnow carry the booked total's scale, theweightcolumn's, instead of the re-multiplied product's:{{300.00 USD}}gives 300.00, where it gave 300.00000000000000000000000000.2 HOOL {150 # 5 USD}gives 305, where 152.50 × 2 gave 305.00.Text output rounds for display and is unchanged. JSON and embedders see the new scale. Across the compat corpus's 29 total- and compound-cost ledgers, all 551 (file, query) pairs keep their bean-query status and the rendered text is byte-identical, while the raw JSON differs on 19 pairs.
A deliberate divergence from bean-query, which gives the rounded product. It's recorded in
bql.mdand the CHANGELOG.Tests
crates/rustledger-query/tests/cost_function_matches_weight.rsbooks a ledger the way the loader does, then checks every cost-bearing posting: a{{T}}lot sold whole, a{{T}}lot sold down, a per-unit lot and an AVERAGE pool.cost(position)equalsweighton every row.costcolumn is|weight|.sum(cost(position))is exactly zero.cost(sum(position))still gives the per-unit product.JOURNAL … AT COSTshows 500 / -500 with balances 500 / 0. The test fails on main'sexecution.rs.BALANCES AT COSTof a held{{500 USD}}lot is exactly 500. The test fails with the oldat_cost.cost(units(position))per row is the units, not the posting's cost, which pins the route's guard.cargo clippy --workspace --all-features --all-targets -D warningsis clean.Closes #2425
🤖 Generated with Claude Code
https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr