Skip to content

refactor(query): route posting cost-resolve through Position::from_posting (BR8 step 1) - #1555

Merged
robcohen merged 2 commits into
mainfrom
refactor/posting-context-leaves
Jun 23, 2026
Merged

robcohen merged 2 commits into
mainfrom
refactor/posting-context-leaves

Conversation

@robcohen

@robcohen robcohen commented Jun 23, 2026 •

Copy link
Copy Markdown
Member

What

Architecture-roadmap [XL/BR8] — step 1 of unifying the four copies of "iterate postings, resolve cost→Position, accumulate Inventory, derive columns" onto one stream. This first slice single-sources the cost-resolve leaf, the most-duplicated piece.

The idiom if cost_spec.resolve(units, date) { Position::with_cost } else { Position::simple } (also guarding the no-cost case) was hand-written at 5 Position-building sites across the query executor. The canonical version already exists as rustledger_core::Position::from_posting (extracted in the L/7 work) — these sites just hadn't been routed through it.

How

Routes all 6 query-executor Position-building cost-resolves through Position::from_posting(units, posting.cost.as_ref(), date):

  • executor/mod.rs — build_balances_with_filter accumulator + the resolve_position closure (running balance/account_balance)
  • executor/system_tables.rs — #postings running-balance pos + the position column
  • executor/evaluation.rs — the position posting-function

Position::from_posting is byte-for-byte equivalent to each replaced block (cs.resolve(...) → with_cost, else simple; outer None → simple), so query results are unchanged. The cost-field-extraction at system_tables (which reads the resolved cost's number/currency/date/label) and the units×cost weight in mod.rs are different idioms and are left alone.

Why

Removes the "running balance / account_balance need to match so lot details aren't dropped" convention-only invariant (its comment is replaced by the shared call). A future CostNumber variant or cost-resolve fix now lands in one place.

Tests

Full rustledger-query suite passes (these are result-producing paths); clippy --all-features --all-targets -D warnings + fmt clean. Follow-ups: the source-location-columns leaf (fixes bug #4's unchecked as i64), then the unified posting iterator.

🤖 Generated with Claude Code

…sting (BR8 step 1)

The cost-resolve idiom (cost_spec.resolve -> with_cost else simple, plus the
no-cost guard) was hand-written at 5 Position-building sites in the query
executor. Route them all through the canonical rustledger_core::Position::from_posting
(build_balances_with_filter + resolve_position closure in mod.rs, the #postings
running balance + position column in system_tables.rs, the position function in
evaluation.rs). Byte-for-byte equivalent, so query results are unchanged; removes
the convention-only must-match invariant. First slice of the PostingContext-stream
unification.
Copilot AI review requested due to automatic review settings June 23, 2026 18:22
@robcohen
robcohen requested a review from a team as a code owner June 23, 2026 18:22
@codecov

codecov Bot commented Jun 23, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

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

Refactors rustledger-query’s executor to single-source posting cost→Position resolution by routing several inlined CostSpec::resolve blocks through rustledger_core::Position::from_posting, reducing duplication and the risk of cost-handling drift across query result paths.

Changes:

  • Replaced multiple hand-rolled “resolve cost spec → build Position” blocks with Position::from_posting(...) in query execution paths.
  • Updated the shared resolve_position closure used for running-balance computations to delegate to Position::from_posting.
  • Adjusted nearby explanatory comments to reflect the new single-source approach.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
crates/rustledger-query/src/executor/mod.rs Uses Position::from_posting for BALANCES accumulation and for the shared resolve_position closure used by running balance/account_balance state.
crates/rustledger-query/src/executor/system_tables.rs Uses Position::from_posting for #postings running balances and the position column value.
crates/rustledger-query/src/executor/evaluation.rs Uses Position::from_posting for the position column evaluation in expression evaluation.

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

@github-actions

github-actions Bot commented Jun 23, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Tool Time Memory
rustledger 35.3ms ± 1.2ms 24 MB
beancount 787.4ms ± 3.7ms 50.6 MB
Comparison 22.3x faster 2.1x less

vs main branch (2026-06-23)

Metric main this PR Change
Time 35ms 35.3ms +0.9% ✅
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

@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

…sition::from_posting

Copilot: execution.rs still had an inlined cost_spec.resolve -> with_cost/simple
for the JOURNAL running balance/position; route it too so all Position-building
cost-resolves in the executor share one path.
@robcohen

Copy link
Copy Markdown
Member Author

Good catch — fixed rather than reworded: routed the JOURNAL running-balance/position cost-resolve in execution.rs through Position::from_posting too, so the claim now holds for every Position-building site in the executor. (The AtMode::Cost resolve nearby computes units × cost — a weight, not a Position — so it is left alone, same as the units×cost site in mod.rs.)

@robcohen
robcohen merged commit a62385e into main Jun 23, 2026
46 checks passed
@robcohen
robcohen deleted the refactor/posting-context-leaves branch June 23, 2026 18:56
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