Repository navigation
fix(query): order BALANCES rows by account_sortkey - #2492
Conversation
BALANCES sorted rows by account name, so Expenses came before Liabilities. bean-query's BALANCES is GROUP BY account ORDER BY account_sortkey(account): account type first (honoring name_* renames), then name. The compat corpus now compares balances-by-account in order. Closes #2409 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?
|
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
✅ Compatibility Test Results
What's tested
Full results available in workflow artifacts |
Review (final: 3 passes)Corrected (pass 2): on the full corpus, main fails order-sensitive Fixtures (4462c96):
rustfava (8 failed / 671 passed) is exactly the 5 known plus 3 expected.
Code (pass 3): no change needed. The sort uses the same config-aware For @robcohen: regenerate those 3 rustfava snapshots after merge. Integration (final heads, plus #2487, #2490 and #2491, merged onto main 184e9d1 in a scratch tree with its own target dir): the only conflicts are in
Merge order: #2494 → #2489 → #2492 → #2493; #2487, #2490 and #2491 can go anywhere. Nothing is order-dependent. |
Review follow-up. Only 2 of 35 corpus files distinguished name order from account_sortkey order, so the order-sensitive balances-by-account check barely bit. Add two in-tree fixtures under tests/compatibility/files/bql/: one with all five account types, one with renamed roots via name_* options. main's binary fails balances-by-account on both; this branch passes. Parser and CST manifests gain the two entries. Refs #2409 Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
# Conflicts: # CHANGELOG.md
What changed
BALANCESrows were ordered by account name. bean-query'sBALANCESis sugar forSELECT account, sum(position) ... GROUP BY account ORDER BY account_sortkey(account), so it orders by account type first (Assets, Liabilities, Equity, Income, Expenses, honoringname_*renames), then name.execute_balancesnow sorts by(account_type_index, name), the same config-aware classifierACCOUNT_SORTKEYuses. Values are unchanged; only row order moves.tests/compatibility/bql-queries.toml:balances-by-accountnow haspreserve_order = true, so the differential compares the order too (it comparedBALANCESas a set, which is why this went unregistered). NoKNOWN_*registry entry needed changing.Verification
option "name_income" "Revenue"and accounts in all five types:Assets:Bank, Liabilities:Card, Equity:Opening, Revenue:Salary, Expenses:Food, Expenses:Rent, with and withoutFROM ... WHERE. rledger now gives the same.crates/rustledger-query/tests/balances_account_order_test.rs(2 tests) fails without the fix (got plain name order) and passes with it.scripts/compat-bql-test.py, 36 files x 19 queries) with the order-sensitivebalances-by-account: 0 real mismatches,balances-by-account35/35.cargo test -p rustledger-query --all-features, the CLI's BALANCES tests (report_display_context_test,query_report_realization_parity_test,decimal_overflow_test,report_capgains_test), workspace clippy-D warnings,cargo doc -D warningsclean.rustfava check
rustfava tests against PR componentgoes from the known 5 failures to 8 (671 passed). The 3 new ones are rustfava snapshots that pinned the old name order ofBALANCESrows:test_core_query_shell.py::test_query_balances,::test_query_to_file, andtest_json_api.py::test_api_query_result[balances from year = 2014-balances]. I rebuilt both sides of all three diffs from the CI log. Each holds the same set of rows (the same accounts and amounts) in a new order.Liabilities:*moves ahead ofEquity:*, andIncome:*ahead ofExpenses:*, which is bean-query's order. The JSON test's diff also shows130vs130.0, but Python compares those as equal; only the order fails it. Those snapshots need regenerating in rustfava once this lands.Harness self-check and new fixtures (commit 4462c96)
Correction to my first review comment. I said only 2 of 35 files could tell the two orders apart. That came from the partial corpus I had locally. Re-run on the full fetched corpus (740 files, CI's file order, 150-file budget), with
balances-by-accountorder-sensitive:balances-by-accountSo the check already catches the bug broadly in CI, and the harness can report dirty. The two in-tree fixtures added under
tests/compatibility/files/bql/(with a.gitignoreexception likecosts/andinterpolation/) still pull their weight: they do not depend on the downloaded corpus (which changes upstream and is capped at 150 files per run), and they pin both the default roots and renamed roots in files built for this check:balances_account_order.beancount: all five account types, every one reachable fromBALANCES FROM has_account('Assets').balances_renamed_roots.beancount:name_liabilities "Debts",name_equity "Capital",name_income "Revenue",name_expenses "Spending". Plain name order (CapitalbeforeDebts) and an order with the roots hardcoded both differ from bean-query's.Main fails
balances-by-accounton both fixtures and this branch passes. Both passbean-check,rledger check, the format idempotence check and the bean-format roundtrip. I added their two lines to the parser and CST corpus manifests, inserted where a full regen puts them, and left everything else in each manifest untouched.STRICT_BASELINE=1passes on both.Other surfaces
balances(), the FFI component and MCP all runBALANCESthrough the executor, and each callsset_account_types, so they follow the new order, includingname_*renames.rledger report balancesstays in name order. That is beancount v2bean-report balancesorder (checked:Assets, Equity, Expenses, Liabilities, Revenue). It is not BQL and has no bean-query counterpart, so it does not change.balsheetgroups by type itself.JOURNALis ordered by date. PIVOT follows ORDER BY or sorts by key (BQL: PIVOT BY orders its columns by row order (ORDER BY), bean-query sorts them by value #2440). ASELECT ... GROUP BY accountwith noORDER BYmakes no order promise. None of them should follow this change.Closes #2409
🤖 Generated with Claude Code
https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr