Repository navigation
fix(query): apply CLEAR and CLOSE's conversions entry, as beanquery does - #2412
Conversation
`FROM ... CLEAR` was parsed and ignored: the income statement stayed in the
income and expense accounts where bean-query moves it to
`account_current_earnings`, and `FROM CLEAR` alone failed as a table name.
`CLOSE` only truncated, missing beancount's conversions entry.
Both now follow beancount's `summarize`, after OPEN ON in beanquery's order:
- CLOSE (`summarize.close`): a `C` entry dated the day before the close
date, or at the last entry for a bare CLOSE, booking the period's
residual at cost to `account_current_conversions`, priced at zero in
`conversion_currency`, narrated with the balance sorted as beancount
sorts an inventory. The balance keeps beancount's insertion order with
zero-crossing keys dropped, so postings come out in its order.
- CLEAR (`summarize.clear`): one `T` transfer per income-statement account,
dated the last entry of any type, to `account_current_earnings`.
The parser now reads OPEN/CLOSE/CLEAR as FROM modifiers rather than table
names, takes a bare CLOSE (`FromClause::close`), and parses the separator,
optional ON and date of `CLOSE ON <date>` as one unit, so `CLOSE <date>` can
no longer leave the date to be read as a filter expression.
`Options::current_{earnings,conversions}_account` resolve an unset account
under the ledger's equity root; `SummaryAccounts` carries them and the
conversion currency (wasm cache v27).
Closes #2406
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! |
📊 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 |
Review of #2412. - `cargo mutants --in-diff` found the new loader methods (`current_*_account`, `conversion_currency_or_default`) untested in their crate, and two `OrderedBalance` rules unpinned: a zero position never enters (Python's `Inventory` behavior) and non-major currencies rank by code length. Tests now cover all three. - Four window mutants survived because the code was unobservable: seeding `CLEAR`'s and a bare `CLOSE`'s date from the summaries and pre-open Opens/Prices. A conversions entry or transfer needs a balance from the window's own transactions, which are dated on or after the open date, so an in-window date always wins. Removed, with the argument in a comment. `current_conversions_account`'s guard was redundant the same way (`Some` only when set) and is gone too. - bean-query 0.2.0 crashes on `OPEN ON <date>` with a bare `CLOSE` (its compiler compares the date with `True`); rledger's rows equal beancount's own `summarize` for it, now pinned. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
Deep review (head
|
What
The rest of beancount's
summarizefamily in BQL'sFROMclause, after #2401'sOPEN ON.CLEARwas parsed and ignored, andFROM CLEARon its own failed as a table name.SELECT account, sum(position) FROM CLOSE ON 2024-03-01 CLEAR GROUP BY account(the #2406 repro):Assets:BankEquity:Earnings:CurrentExpenses:FoodExpenses:RentIncome:SalaryLiabilities:CardCLOSEonly truncated. It was missing the conversions entry beancount books when@price conversions leave the balances unequal at cost.How
Both follow beancount's
summarize, applied afterOPEN ONin beanquery's order (_BeancountTable.prepare), inside #2401's single date window,Executor::window_transactions.SELECT,BALANCESandJOURNALtherefore all see them.CLOSE [ON <date>]issummarize.close. After truncating, it adds aCentry that books the period's residual at cost, negated, toaccount_current_conversions.CLOSE.conversion_currency(NOTHINGby default), as beancount prices it.Inventoryordering. Keys go in first-appearance order, a key is dropped when it reaches zero, and it comes back at the end. So the postings come out in bean-query's order, which is not the obvious one: EUR then USD withoutOPEN ON, USD then EUR with it.Conversion for (<balance>)renders the balance sorted by beancount'sPosition.sortkey. That is a currency rank (USD, EUR, JPY, … first), then cost, then units.CLEARissummarize.clearwith no date: oneTtransfer per income-statement account with a balance, in account order, moving the balance at cost toaccount_current_earnings. They are datedentries[-1].date, the last entry of any type left afterOPENandCLOSE, so a later price or balance directive counts. Balances are realized through the booking engine, as BQLFROM OPEN ONdrops the opening balance from posting rows,balanceandsum(position)#2401's summaries are.Options::current_{earnings,conversions}_accountandconversion_currency_or_defaultresolve unset options under the ledger's own equity root, like BQLFROM OPEN ONdrops the opening balance from posting rows,balanceandsum(position)#2401'sprevious_*.SummaryAccountscarries them (wasm cache v27). The component resolves an unsetaccount_current_conversionswithout changing its exportedNone, so hosts see the same options as before.Parser
OPEN,CLOSEandCLEARare FROM modifiers, not table names, as in beanquery's grammar.FROM CLEAR,FROM CLOSEandFROM CLOSE CLEARnow parse.FROM #postingsand user tables still parse as tables (tested).CLOSE:FromClause::closemarks it;close_onstays the date.ON, then the date. My first cut left the separator outside it. That silently turnedCLOSE 2024-02-01, a parse error on main, into a bareCLOSEwith the date read as a filter, which returned the whole ledger. Found while testing, fixed, and pinned in the parser test.CLOSE <date>withoutONis now a dated close; beanquery requiresON, and the old parser meant to accept both but never could.Verification
FROM OPEN ONdrops the opening balance from posting rows,balanceandsum(position)#2401 ledgers plus the newCLOSE/CLEAR/CLOSE CLEAR/OPEN … CLOSE … CLEAR/sum(weight)/JOURNAL … CLEARshapes give 140/153 identical.FROM <expr> OPEN ONnot parsing (BQL:FROM <expr> OPEN ON / CLOSE ON / CLEARfails to parse #2407), and 4 ×BALANCESrow order (BQLBALANCESorders rows by account name, beanquery by account_sortkey #2409).FROM OPEN ONdrops the opening balance from posting rows,balanceandsum(position)#2401 binary scores 88/153 on the same harness.CLOSEand renamed-root cases. So docount(*)forCLOSE ON,CLOSE,CLOSE … WHEREandCLOSE CLEAR.open_on_summarize_test.rs: three new tests (CLEARtransfers and totals, conversions residual with a bare and anOPEN ONvariant, renamed root withconversion_currency), 14 in the file.CLOSE,CLOSE <date>, and table names still being tables.CLEAR, dropping the conversions entry, leaving the narration unsorted, and removing the keyword guard each fail their tests.-D warningsare clean.CLOSE/CLEAR: byte-identical to merged main on the fifo, avg and investment 20k benches (BALANCES,sum(position), abalanceSELECT,JOURNAL). Callgrind onBALANCESfifo-20000: +0.01%.Upstream crash
bean-query 0.2.0 crashes on
OPEN ON <date>together with a bareCLOSE: its compiler evaluatesnode.open > node.closewithclosebeingTrue, aTypeError. rledger answers it, and its rows equal beancount's ownsummarize.open_opt→close_opt(None)→clear_optrun directly (pinned in a test).Reserved words
OPEN,CLOSEandCLEARare FROM keywords now, as in beanquery's grammar, so a user table with one of those names cannot be selected withFROM <name>.Known, not this PR
A zero-price posting's
weightrenders0 NOTHINGwhere Python keeps the product's sign and scale (-0.00 NOTHING). That is rledger's rendering of zero products on main too, for any@ 0posting (0 USDagainst-0 USD); the value is equal.API
Breaking (in the CHANGELOG):
FromClause::closeand threeSummaryAccountsfields, both exhaustive structs.FromClause::close()is a new builder.Closes #2406
🤖 Generated with Claude Code
https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr