Skip to content

refactor(query): build_postings_table consumes the shared scan_postings stream (BR8 step 3 final) - #1559

Merged
robcohen merged 2 commits into
mainfrom
refactor/posting-iterator-merge
Jun 23, 2026
Merged

robcohen merged 2 commits into
mainfrom
refactor/posting-iterator-merge

Conversation

@robcohen

Copy link
Copy Markdown
Member

What

Architecture-roadmap [XL/BR8] — step 3, final: the iterator merge. The two posting-source iterations are now one. build_postings_table (the #postings table) consumes the same scan_postings stream the default SELECT path uses, instead of re-implementing the iterate-postings + accumulate-balances loop.

This is the structural dedup the audit's step 3 calls for — and it eliminates the duplicated balance-accumulation loop that was the source of bug #2 and the type/cost_date drift, replacing "two loops that must be kept in sync (and a test that catches it when they aren't)" with "one loop."

How

  1. Extracted scan_postings(from, where, needs_balance, needs_account_balance, where_reads_balance) from collect_postings — the single posting scan: iterate resolved directives, apply optional FROM/WHERE, accumulate cumulative balance + per-account account_balance, yield one PostingContext per posting. collect_postings is now a thin wrapper that computes the WASM hangs when reading accounts from Assets #1080 gates from the query and calls it.
  2. build_postings_table now calls scan_postings(None, None, true, true, false) — every posting, no filter, both balances tracked — and reads balance/account_balance straight from each PostingContext. Its own account_balances/cumulative_balance accumulators are gone.
  3. Removed build_postings_table's redundant sort_by_key(date) — the loader already sorts directives, and the other two scanners (collect_postings, build_balances_with_filter) rely on that pre-sorted order without re-sorting. This also makes the three consumers consistent.

Safety

  • No perf regression: build_postings_table already paid the per-posting Inventory clone (the table materializes balance/account_balance); collect_postings's WASM hangs when reading accounts from Assets #1080 gating is preserved (passed as explicit params).
  • Identical output: the #postings parity test (every projectable column, including the accumulated balances) passes against the rewritten table, and the full rustledger-query suite is unchanged — scan_postings with no filter produces exactly the values the old inline loop did.

Tests

#postings parity harness + full rustledger-query suite pass; clippy --all-features --all-targets -D warnings + fmt clean.

🤖 Generated with Claude Code

…gs stream (BR8 step 3 final)

Extract scan_postings from collect_postings (the single posting scan: iterate +
optional FROM/WHERE + accumulate balance/account_balance -> PostingContext);
collect_postings becomes a thin wrapper computing the #1080 gates. build_postings_table
now drives scan_postings(None, None, all-gates) and reads balance/account_balance
from each context instead of its own accumulator loop, and its redundant date-sort
is removed (the loader pre-sorts; the other scanners do not re-sort). One iteration,
not two -- eliminating the duplicated accumulation that was the source of bug #2.
No perf regression; #postings parity test + full query suite unchanged.
@robcohen
robcohen requested a review from a team as a code owner June 23, 2026 23:17
Copilot AI review requested due to automatic review settings June 23, 2026 23:17
@codecov

codecov Bot commented Jun 23, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 99.08257% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...tes/rustledger-query/src/executor/system_tables.rs 98.93% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@github-actions

github-actions Bot commented Jun 23, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Tool Time Memory
rustledger 37.8ms ± 0.2ms 23.7 MB
beancount 773.9ms ± 4.4ms 50.6 MB
Comparison 20.5x faster 2.1x less

vs main branch (2026-06-23)

Metric main this PR Change
Time 35ms 37.8ms +8.0% 🟡
10K transactions · 5 runs, 2 warmup · hyperfine
What do these numbers mean?
  • Time: Wall-clock time to parse and validate the ledger (± std dev)
  • Memory: Peak resident set size (RSS)
  • vs main: Compared to latest nightly benchmark on main branch

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors the query executor to centralize posting iteration/balance accumulation into a single shared scan (scan_postings), and updates the #postings system table builder to consume that shared scan instead of maintaining its own parallel posting loop. This reduces duplicated logic and keeps computed columns like running balances aligned across query paths.

Changes:

  • Introduces scan_postings(...) as the shared posting-iteration + running-balance accumulator used by collect_postings.
  • Rewrites build_postings_table to consume scan_postings(None, None, true, true, false) and remove its redundant per-account/cumulative accumulators.
  • Removes the local transaction sorting in build_postings_table, relying on directive order.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
crates/rustledger-query/src/executor/system_tables.rs #postings now consumes scan_postings output and drops duplicated balance accumulation/sorting logic.
crates/rustledger-query/src/executor/mod.rs Factors shared posting scan logic behind scan_postings and routes collect_postings through it.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@github-actions

Copy link
Copy Markdown
Contributor

✅ Compatibility Test Results

Metric Result
Check Compatibility 100%
BQL Compatibility 100%
Full AST Match 100%
Total Files 792
What's tested
  • Check: Exit codes and error counts match bean-check
  • BQL: Query results match bean-query
  • Files: ~800 test files downloaded from 10+ beancount repositories

Full results available in workflow artifacts

…scan invariants with expect

Copilot: (1) account_balance is independent of WHERE, not FROM (the FROM clause is
honored) — fix the comment; (2)/(3) scan_postings(None,None,..) is infallible and
always sets directive_index, so use expect to assert those invariants instead of
silently returning an empty table / attributing rows to directive 0.
@robcohen

Copy link
Copy Markdown
Member Author

All three addressed: (1) corrected the comment — account_balance is independent of the WHERE filter, not FROM (the FROM clause is honored; only the pre-open_on carry-in is added past it). (2) build_postings_table's scan call is now .expect(...) — with no FROM/WHERE there are no predicates to evaluate, so failure is a real invariant violation, not something to swallow into an empty table. (3) directive_index is now .expect(...) since scan_postings always records it.

@robcohen
robcohen merged commit 40464a7 into main Jun 23, 2026
45 checks passed
@robcohen
robcohen deleted the refactor/posting-iterator-merge branch June 23, 2026 23:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants