Skip to content

fix(query): resolve lineno/filename/location per posting, not per directive - #1510

Merged
robcohen merged 1 commit into
mainfrom
fix/bql-per-posting-lineno
Jun 22, 2026
Merged

robcohen merged 1 commit into
mainfrom
fix/bql-per-posting-lineno

Conversation

@robcohen

Copy link
Copy Markdown
Member

What

The lineno / filename / location columns reported the enclosing transaction's line for every posting, instead of each posting's own line. bean-query reports the posting's line:

SELECT lineno, account     -- ledger: txn on line 3, postings on lines 4 & 5
rledger (before):  3, 3     bean-query: 4, 5

Found in round-4 BQL dogfounding. Affected both the default SELECT path and the explicit FROM #postings path.

Root cause

source_locations is keyed per-directive, and the Executor kept only those precomputed locations — not the SourceMap — so per-posting lines couldn't be resolved. Both row paths looked up the directive's location.

How

  • Keep the SourceMap in the Executor (new_with_sources).
  • New span_source_location(file_id, span_start) resolves a location from a span; synthesized spans (pad/booking-generated) → None.
  • Default path (evaluation.rs): filename/lineno/location resolve via resolved_source_location(ctx) — the posting's own span, falling back to the directive's location.
  • #postings path: per-posting location computed in the builder loop, falling back to the txn-level location.

Synthesized postings (no real span) and source-map-less executors fall back to the prior behavior, so nothing regresses.

Tests

test_per_posting_source_location: postings on lines 2 & 3 report lineno 2 & 3 on both the default and #postings paths. Existing source-location tests (synthesized postings → directive line) unchanged. Oracle-verified against bean-query. Full rustledger-query suite passes; clippy + fmt clean.

🤖 Generated with Claude Code

…ective

The lineno/filename/location columns reported the enclosing transaction line for
every posting; beanquery reports each posting own line. Keep the SourceMap in
the Executor and resolve these columns from the posting span (default and
#postings paths), falling back to the directive location for synthesized
postings. Updates the #1506 CLI test, which pinned the old txn-line behavior.
Found via BQL dogfooding.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@robcohen
robcohen requested a review from a team as a code owner June 22, 2026 03:19
Copilot AI review requested due to automatic review settings June 22, 2026 03:19
@codecov

codecov Bot commented Jun 22, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@github-actions

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Tool Time Memory
rustledger 34.8ms ± 0.7ms 23.6 MB
beancount 901.6ms ± 22.8ms 50.7 MB
Comparison 25.9x faster 2.1x less

vs main branch (2026-06-22)

Metric main this PR Change
Time 34.9ms 34.8ms -0.3% ✅
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
robcohen merged commit 8b46c77 into main Jun 22, 2026
45 checks passed
@robcohen
robcohen deleted the fix/bql-per-posting-lineno branch June 22, 2026 03:23

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

Fixes BQL source-location reporting so filename / lineno / location resolve per posting (matching bean-query), rather than always using the enclosing directive’s location. This impacts both the default (implicit postings) query path and the FROM #postings table path.

Changes:

  • Keep SourceMap in Executor and add span-based location resolution with fallback behavior for synthesized/no-source-map cases.
  • Update default column evaluation (evaluation.rs) to resolve posting-level source locations.
  • Update #postings table building to compute per-posting filename/lineno/location, and extend tests to validate per-posting behavior.

Reviewed changes

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

File Description
crates/rustledger/tests/integration_test.rs Adjusts CLI integration assertion to reflect per-posting line numbers and uses unique tempfile handling.
crates/rustledger-query/src/executor/mod.rs Stores SourceMap, adds span/posting location resolution helpers, applies per-posting source location to #postings, and adds unit tests.
crates/rustledger-query/src/executor/evaluation.rs Switches filename/lineno/location column evaluation to use per-posting resolved source locations with fallback.

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

Comment on lines +2624 to +2632
let (filename, lineno, location) =
match self.span_source_location(posting.file_id, posting.span.start) {
Some(loc) => (
Value::String(loc.filename.clone()),
Value::Integer(loc.lineno as i64),
Value::String(format!("{}:{}", loc.filename, loc.lineno)),
),
None => (filename.clone(), lineno.clone(), location.clone()),
};
Comment on lines +384 to +396
// Source location columns — resolved from the posting's own span
// (falling back to the enclosing directive's location).
"filename" => Ok(self
.resolved_source_location(ctx)
.map_or(Value::Null, |loc| Value::String(loc.filename))),
"lineno" => Ok(self
.resolved_source_location(ctx)
.map_or(Value::Null, |loc| Value::Integer(loc.lineno as i64))),
"location" => Ok(self
.resolved_source_location(ctx)
.map_or(Value::Null, |loc| {
Value::String(format!("{}:{}", loc.filename, loc.lineno))
})),
Comment on lines +4089 to +4093
Spanned {
value: Posting::new("Expenses:Food", Amount::new(dec!(-100), "USD")),
span: Span { start: 43, end: 67 },
file_id,
},
Comment on lines +4097 to +4101
let spanned = vec![Spanned {
value: Directive::Transaction(txn),
span: Span { start: 0, end: 67 },
file_id,
}];
Comment on lines 166 to 169
pub fn new_with_sources(
spanned_directives: &'a [Spanned<Directive>],
source_map: &SourceMap,
source_map: &'a SourceMap,
) -> Self {
Comment on lines 779 to 783
// filename resolves to the real path (was empty/NULL before the fix).
assert!(
stdout.contains("query-filename-lineno-test.beancount"),
stdout.contains(".beancount"),
"expected the source filename in output, got:\n{stdout}"
);
@github-actions

Copy link
Copy Markdown
Contributor

⚠️ Compatibility Test Results

Metric Result
Check Compatibility 100%
BQL Compatibility 85%
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

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