Skip to content

refactor(query): single-source source-location columns; fix unchecked lineno cast (BR8 step 2) - #1556

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

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

Conversation

@robcohen

Copy link
Copy Markdown
Member

What

Architecture-roadmap [XL/BR8] — step 2 of the PostingContext-stream unification. Single-sources the source-location column leaf (filename/lineno/location), and fixes bug #4 along the way.

These three synthetic columns were built in ~3 places, two of which used an unchecked loc.lineno as i64 cast (lineno is usize) — bug #4: on a hypothetical line number exceeding i64, the cast wraps to a negative lineno. The META path already guarded this (source_location_meta_key uses try_from); the column paths did not.

How

Adds three shared helpers next to the existing source_location_meta_key:

  • source_filename_value, source_lineno_value, source_location_value — each Option<&SourceLocation> -> Value.
  • source_lineno_value is the single overflow-checked conversion (i64::try_from(...).unwrap_or(i64::MAX)), replacing every as i64 cast.

Routes all column sites through them:

  • executor/evaluation.rs — the filename/lineno/location posting-function arms
  • executor/system_tables.rs — the standalone filename/lineno pair and the (filename, lineno, location) #postings tuple

Behavior is unchanged for every real ledger (linenos fit in i64); only the pathological overflow now saturates instead of wrapping.

Tests

Added unit tests: source_lineno_value saturates usize::MAX to i64::MAX on 64-bit (bug #4 pin), basic lineno/filename/location values, and None handling. Full rustledger-query suite passes; clippy --all-features --all-targets -D warnings + fmt clean.

Follow-up (step 3): fold these leaves + the cost-resolve (step 1, #1555) into one posting-source iterator consumed by build_postings_table, build_balances_with_filter, and aggregation.

🤖 Generated with Claude Code

@robcohen
robcohen requested a review from a team as a code owner June 23, 2026 20:30
Copilot AI review requested due to automatic review settings June 23, 2026 20:30
@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

This PR refactors rustledger-query’s executor to single-source the synthetic source-location columns (filename, lineno, location) and fixes an unchecked usize -> i64 cast that could wrap to a negative line number on overflow.

Changes:

  • Added shared helpers on Executor for computing filename/lineno/location column Values, with overflow-checked lineno conversion.
  • Routed #postings table construction and posting-function column evaluation through the shared helpers (removing duplicated logic).
  • Added unit tests covering basic behavior, None handling, and 64-bit overflow saturation for lineno.

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/system_tables.rs Routes synthetic source-location columns in #entries/#postings through shared helpers.
crates/rustledger-query/src/executor/functions/util.rs Introduces shared source_*_value helpers and adds unit tests (including overflow saturation for lineno).
crates/rustledger-query/src/executor/evaluation.rs Routes filename/lineno/location posting-function arms through the shared helpers.

💡 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 38ms ± 1ms 23.9 MB
beancount 918.5ms ± 6.1ms 50.7 MB
Comparison 24.2x faster 2.1x less

vs main branch (2026-06-23)

Metric main this PR Change
Time 35ms 38ms +8.6% 🟡
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

@robcohen

Copy link
Copy Markdown
Member Author

Good catch — fixed: extracted lineno_i64 (the saturating try_from) and both source_lineno_value and source_location_value now use it, so the location and lineno columns can never disagree on the same posting. Added an assertion to the overflow test pinning that consistency.

@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

robcohen added 2 commits June 23, 2026 21:54
…cked lineno cast (BR8 step 2)

The filename/lineno/location synthetic columns were built ~3 ways, two using an
unchecked loc.lineno as i64 cast (lineno is usize) -- bug #4. Add three shared
helpers (source_filename_value/source_lineno_value/source_location_value) next to
source_location_meta_key; source_lineno_value is the single overflow-checked
conversion (try_from, saturating to i64::MAX). Route the evaluation.rs posting
functions and the system_tables.rs standalone pair + #postings tuple through them.
Behavior unchanged for real ledgers; the pathological overflow now saturates
instead of wrapping negative. Adds unit tests pinning the saturation.
…he lineno column

Copilot: source_location_value formatted the raw usize while source_lineno_value
saturates to i64::MAX, so on overflow location could disagree with the lineno
column. Extract lineno_i64 (the saturating conversion) and use it for both.
@robcohen
robcohen force-pushed the refactor/posting-context-location branch from 0459d6f to 2c8eab6 Compare June 23, 2026 21:54
@robcohen
robcohen merged commit ef9b010 into main Jun 23, 2026
45 checks passed
@robcohen
robcohen deleted the refactor/posting-context-location branch June 23, 2026 22:09
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