Repository navigation
fix(query): accounts and other_accounts are sorted sets; other_accounts excludes only the current posting - #2489
Merged
Merged
Conversation
…nly the current posting On the default postings table `accounts` repeated an account a transaction posts to twice and kept posting order, and `other_accounts` dropped every posting sharing the current posting's account name. Both are now sorted, deduped sets, and `other_accounts` excludes only the current posting by index, matching bean-query. `#postings` had the same `other_accounts` fault; one helper now builds the set for every table. Closes #2483 Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Contributor
📊 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?
|
Contributor
✅ Compatibility Test Results
What's tested
Full results available in workflow artifacts |
Review follow-up. `#postings` rebuilt the account set for every posting, twice, so a transaction of n postings cost O(n^2) (slower than main on a 5,000-posting transaction). It now builds the sorted set and the repeated accounts once per transaction and derives `other_accounts` from them. The default table builds its sets with sort+dedup instead of a BTreeSet. Tests pin a second transaction (a stale per-transaction set would show) and an OPEN ON synthesized self-transfer. Refs #2483 Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
Member
Author
Review (final: 5 passes)Passes 1–3:
Pass 4 (after merging main, which now has #2492 and #2494; merge commit 2c3cdb3, CHANGELOG kept both entries):
Pass 5 (main merged again at c0606c3, after #2497, #2487, #2496 and #2488 landed; CHANGELOG kept both entries): nothing found.
|
…l time Review follow-up. The wall-clock figure was taken on a loaded machine; cachegrind instruction counts (mimalloc release, load cache off) are the reproducible measure: 14.9 to 0.27 billion for `SELECT count(*) FROM #postings` over one 5,000-posting transaction. Refs #2483 Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
This was referenced Oct 8, 2026
…ts column Review follow-up. The account set had two implementations: a per-row helper and the per-transaction struct `#postings` uses, whose doc claimed equivalence. There is now one, generic over borrowed names (per row, no copies until output) and owned ones (`#postings`, one per transaction, now keyed by directive index instead of an address). A unit test checks `others` against excluding the posting by index over several shapes. Refs #2483 Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
robcohen
force-pushed
the
fix/bql-accounts-set
branch
from
October 8, 2026 02:32
66e2001 to
c6a840f
Compare
# Conflicts: # CHANGELOG.md
…s them Review follow-up. docs/reference/bql.md still said `other_accounts` is "every account in the transaction except the current posting's own account", the name-based rule #2483 replaces; it now says the accounts of every other posting, sorted and deduped, so the current posting's account stays when another posting uses it. Refs #2483 Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
# Conflicts: # CHANGELOG.md
# Conflicts: # CHANGELOG.md
This was referenced Oct 8, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changed
On the default postings table:
accountsis now the transaction's posting accounts as a sorted, deduped set (bean-query:{p.account for p in entry.postings}). It repeated an account posted to twice and kept posting order.other_accountsnow excludes only the current posting, by index, then sorts and dedupes (bean-query:sorted({p.account for p in entry.postings if p is not context.posting})). It dropped every posting whose account name matched the current one.FROM #postingshad the sameother_accountsfault (name-based filter) and is fixed too. One helper,Executor::posting_account_set(txn, exclude)insystem_tables.rs, now builds the set for the default table,#postings,#entriesand#transactions; the three HashSet-then-sort copies are gone.Verification
Ledger with two
Expenses:Foodpostings and oneAssets:Bank, plus a second transaction with three distinct accounts.SELECT date, account, accounts, other_accountsand the sameFROM #postingsnow give exactly bean-query 0.2.0 / beancount 3.2.3's rows (e.g.Expenses:Foodrow:other_accounts = Assets:Bank, Expenses:Food;Assets:Bankrow:Expenses:Food).test_accounts_and_other_accounts_with_repeated_account(both tables). It fails without the fix (accountswas["Expenses:Food", "Expenses:Food", "Assets:Bank"]) and passes with it.cargo test -p rustledger-query --all-features, workspace clippy-D warnings,cargo doc -D warningsclean.Performance (review follow-up, commit 073a7f8)
The first version of this PR made
#postingsslower on a large transaction: it rebuilt the account set twice for every posting, so one transaction of n postings cost O(n²).#postingsnow builds the sorted set, plus the accounts used by more than one posting, once per transaction, and derivesother_accountsfrom them. The default table builds its per-row set with sort+dedup over borrowed names instead of aBTreeSet.Release builds, run interleaved, median times. The machine was under load (load average about 16 on 8 cores), so small differences are noise:
SELECT account, other_accounts... FROM #postingsSELECT count(*) FROM #postingsSELECT account, accounts, other_accounts... FROM #postingsThe default table got faster on the big transaction because each row now carries about 51 deduped names instead of 5,001.
Instruction counts (cachegrind)
Wall time on the loaded machine was noisy, so these are instruction counts (
valgrind --tool=cachegrind --cache-sim=no). All binaries are release builds with the shippingmimallocfeature, run withBEANCOUNT_DISABLE_LOAD_CACHE=1. Base is main 184e9d1; head is c6a840f.SELECT account, accounts, other_accounts... FROM #postingsSELECT count(*) FROM #postingsSELECT count(*) WHERE false)SELECT account, accounts, other_accounts... FROM #postingsSELECT count(*) FROM #postingsThe +0.6% on the default table is the price of a correct set: main collected the names unsorted and undeduped. One version I tried during review cost +2% there because it allocated an extra vector per call; deduping in place brought it down to +0.6%.
Follow-up, not in this PR: the default table is still quadratic within one transaction
On the default table, each row builds its transaction's set from every posting, so one transaction of n postings costs O(n²): 20.5 G instructions for 5,000 postings, against 0.28–1.3 G on
#postings. Main was quadratic too, with a 5× larger constant.txn_accounts: Option<Arc<TxnAccounts<String>>>toPostingContext(executor/types.rs). Fill it inscan_postings(executor/mod.rs, where eachPostingContextis built), once per transaction (postings of one transaction are contiguous in the scan, as the#postingsmemo already relies on), and only when the query referencesaccountsorother_accounts. That is a newScanNeedsflag viaquery_references_column, which includes the WHERE clause because WHERE evaluates against the context before filtering.evaluate_columnreads the field and falls back toTxnAccounts::ofwhen it isNone; the three dummy contexts inevaluation.rsand the one indual_eval_parity.rspassNone. About 40 lines plus a test.Executor: default-table rows are evaluated in parallel (par_iterinexecution.rs), so a shared cache needs a lock on the hot path. The column is also read from WHERE, GROUP BY, ORDER BY and aggregate paths, which only see the context. And a cache keyed by a transaction's address is unsafe onceFROM OPEN ONsynthesizes transactions whose memory can be reused.PostingContextis public (rustledger_query::executor::PostingContext, every fieldpub, not#[non_exhaustive]), so adding a field breaks any caller that builds one with a struct literal. That needs a### Breakingentry, or first making the struct#[non_exhaustive]. I did not want to slip an API break into a correctness fix.One implementation
A single type,
TxnAccounts, builds everyaccountsandother_accountsvalue: the default table,#postings,#entriesand#transactions. It is generic over borrowed names (the default table: one set per row, with no copies until output) and owned names (#postings: one set per transaction, keyed by directive index like the existinglast_entrymemo). A unit test checksothersagainst excluding the posting by index over four shapes, and fails when its repeated-account rule is mutated away.Docs
docs/reference/bql.mddescribedother_accountswith the old rule: "every account in the transaction except the current posting's own account". It now says the accounts of every other posting, sorted and deduped, so the current posting's account stays when another posting uses it. Theaccountsrow was updated to match.Synthesized postings
FROM OPEN ONsynthesizes a transaction whose two postings shareEquity:Opening-Balances(the opening-balances account summarized into itself). bean-query givesother_accounts = Equity:Opening-Balancesthere; main gave an empty set. This PR agrees with bean-query. On a ledger with apad,OPEN ON, andOPEN ON ... CLOSE ON ... CLEAR, every row matches bean-query. New testother_accounts_on_a_synthesized_self_transferfails on main. The two-transaction extension oftest_accounts_and_other_accounts_with_repeated_accountcatches a per-transaction set that is never refreshed: I checked by mutating the cache key, and the test failed.Closes #2483
🤖 Generated with Claude Code
https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr