Repository navigation
fix(query): a posting column in FROM filters rows, and is an error in PRINT - #2523
Conversation
… PRINT A FROM filter expression was evaluated once per transaction against its first posting, so `FROM account ~ 'Bank'` kept the transactions whose first posting was the bank's, with every posting of them. - SELECT, BALANCES and JOURNAL: a FROM filter that reads a posting column is a row filter, joined to WHERE with AND after OPEN/CLOSE/CLEAR rewrote the stream, as beanquery 0.2 compiles it. BALANCES keeps an account's booked total when all of its postings match and sums the matching ones otherwise, as SUM(position) does. - PRINT: a posting column in FROM is an error pointing at has_account(), as both bean-query versions reject it. - Entry filters keep or drop whole transactions as before. `FROM year > N` and `FROM month < N` no longer read as `!=`, `FROM id` sees the directive index, and a transaction without postings no longer panics. Closes #2414 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 |
FROM_ENTRY_COLUMNS decides whether a FROM filter is read once per transaction or per posting, and POSTING_ONLY_COLUMNS which columns PRINT rejects. Both are kept by hand. A posting column added to the first list would bring #2414 back. The new test reads the row evaluator's own match arms (via include_str!) and the executor's #postings and #entries schemas. It evaluates every column on both postings of a transaction whose postings differ in everything, then asserts: - a column is an entry column exactly when its values agree (one exception, `filename`, which is only slower per posting); - POSTING_ONLY_COLUMNS is exactly the posting-row columns that #entries lacks. Checked by mutation: adding `number` to the entry list, dropping `payee` from it, or dropping `units` from the posting-only list each fails the test. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
Review passThe review looked at the column-list drift hazard, performance at two ledger sizes, and the load-pipeline diagnostics. Fixed here (8901bcf). The two hand-kept lists,
Mutation-checked in three directions: adding Performance. Callgrind instruction counts, release builds, cache off. Each figure is the query's cost above the load floor (
Found elsewhere, fixed separately in #2534. Review pass 2
Review pass 3Looked at my Tested with
BALANCES with a partial |
…column # Conflicts: # CHANGELOG.md
The guard reads evaluate_column's arms with a pattern on its source. If the `match` were reshaped (`"a" | "b" =>`, a helper, a table), the pattern would quietly stop seeing those arms and their columns would go unchecked. It now requires every #postings schema column and every name on either list to be among the arms it found, and names the column when one is missing. Checked by rewriting the `units` arm as `"units" | "units_alias" =>`, which fails it. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
…column # Conflicts: # CHANGELOG.md
#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
…column # Conflicts: # CHANGELOG.md
…column # Conflicts: # CHANGELOG.md
What
A posting column in a BQL
FROMfilter now filters rows onSELECT,BALANCESandJOURNAL, and is an error inPRINT, per the decision on #2414. Entry-level filters keep or drop whole transactions, as before.evaluate_from_filterbuilt its context withposting_index: 0, so the filter was evaluated once per transaction against its first posting.Behavior
On the decision's ledger (the issue repro plus a second rent payment on
Liabilities:Card):SELECT date, account, position FROM account ~ 'Bank'Assets:BankrowsIncome:SalaryAssets:BankrowsSELECT sum(position) FROM account ~ 'Bank'1674.25 USD1674.25 USDBALANCES FROM account ~ 'Bank'Assets:Bank 1674.25 USDAssets:Bank 3000.00,Income:Salary -3000.00Assets:Bank 1674.25 USDPRINT FROM account ~ 'Bank'column "account" not found in table "entries"openand 2 transactionshas_account()JOURNAL 'Bank' FROM account ~ 'Bank'1674.251674.25FROM account ~ 'Bank' OPEN ON 2024-02-01FROM has_account('Bank'),narration ~ ...How
from_filter_reads_postings(allowlist of entry columns) decides up front whether a FROM filter is entry-level. An entry-level filter is evaluated once per transaction, as before. Any other filter is evaluated on each posting, with that posting's index and the directive index. It is joined to WHERE with AND after OPEN/CLOSE/CLEAR have rewritten the stream (beanquerycompiler.py,_select:c_where = EvalAnd([c_from_expr, c_where])). The allowlist fails safe: an entry column missing from it only costs one evaluation per posting, with the same answer. A drift-guard test (from_filter_column_lists_match_the_executor) pins both hand-kept lists. It takes the row evaluator's own match arms plus the#postingsand#entriesschemas, evaluates every column on two postings that differ in everything, and requires two things: a column is on the entry list exactly when its values agree, andPOSTING_ONLY_COLUMNSis exactly the posting-row columns that#entrieslacks. I mutation-checked it in three directions.scan_postingsstill replays a transaction through the booking engine when any of its postings passes, soaccount_balancestays the account's own running balance. Only the passing postings become rows, and only they add tobalance.SELECT account, SUM(position) ... GROUP BY account. An account whose postings all match keeps its booked total (AVERAGE pools stay realized as booking realizes them, [Bug]: BQL BALANCES shows unmerged lots and a dangling negative for AVERAGE-booked accounts #1985). An account with only some matching sums those postings, asSUM(position)does, AVERAGE merge included. That code now sharesaccount_is_averagewithSUM. A test pins the two as equal.postingscolumns that itsentriestable lacks, plus rledger'sunits,cost,account_balance,has_cost). The check runs before anything else, so it also fires on an empty ledger, as beanquery's compiler does.Found and fixed on the way (same function)
FROM year > 2014/FROM month < 3: the YEAR/MONTH fast arms returned!matchesfor every operator other than=, so>read as!=. Onexample.beancount,SELECT count(*) FROM year > 2014gave 2327; beanquery gives 1193, and so does this PR. The arms now take only=/!=.FROM idcounted 0, because the context had no directive index. It now counts every row, as beanquery does.index out of bounds) inevaluate_columnandMETA(), e.g.PRINT FROM narration ~ 'empty'. A posting column now reads an empty posting there. Both bean-query versions print the entry.Verification
SELECT count(*) FROM <expr>onexample.beancountagainst beanquery 0.2. All of these agree:payee narration date year flag tags account position number cost_number id meta type description,number > 0,currency = 'USD',cost_number > 100,year = 2014,price IS NOT NULL. This closes the fix(query): a table name can end a subquery or a statement #2437 gap:number3549 → 3513,cost_number421 → 238,id0 → 3549.linksis the adjudicated empty-aggregate quirk (beanquery#1055).uv run --with beancount --with beanquery bean-query,uv run --python 3.11 --with beancount==2.3.6 bean-query). Also checked:OPEN ON,CLOSE ON ... CLEAR,BALANCES ... OPEN ON(with the summary Equity accounts),BALANCES FROM number < 0,SELECT balance FROM number < 0,meta('pk')as posting metadata,JOURNAL FROM number > 0, bareFROM number, a subqueryFROM (SELECT account FROM number), and the empty ledger (SELECT/BALANCESempty,PRINTerror).crates/rustledger-query/tests/from_posting_column_filters_rows.rs, 9 tests with expected values from beanquery. All 9 fail with the source changes reverted.cargo test -p rustledger-query --all-features: 818 passed, 0 failed.cargo clippy --workspace --all-features --all-targets -D warnings: clean.cargo doc -D warnings -p rustledger-query: clean.Divergences
None registered. beancount 2.3.6 rejects every posting column in
FROM, and on SELECT/BALANCES/JOURNAL this PR follows beanquery 0.2, which rledger targets.scripts/compat-bql-test.pyhas no entries aboutFROMposting columns orPRINT. The AVERAGEBALANCEStest is rledger's own: beancount does not book AVERAGE.Not in this PR
PRINTignoresFROM ... OPEN ON/CLOSE ON#2411) and formatter (BQL PRINT drops costs, prices, metadata and the booking method: it re-derives the formatter instead of using rustledger_core::format #2426) are the next PR. That PR also covers PRINT's FROM over non-transaction entries: beanquery filtersopen/price/... with the same expression (narrationis NULL on them), while rledger passes every non-transaction through. It also coversmeta/META()in PRINT's FROM, which beanquery reads as entry metadata.Closes #2414
🤖 Generated with Claude Code
https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr