Skip to content

fix(query): expose filename/lineno as synthetic metadata keys - #1511

Merged
robcohen merged 1 commit into
mainfrom
fix/bql-meta-source-keys
Jun 22, 2026
Merged

robcohen merged 1 commit into
mainfrom
fix/bql-meta-source-keys

Conversation

@robcohen

Copy link
Copy Markdown
Member

What

The meta column rendered {} for postings with no user metadata and omitted beanquery's filename/lineno keys; meta('filename')/meta('lineno') and getitem(meta, …) couldn't see them either. beanquery's parser injects filename/lineno into every posting's/entry's metadata, so SELECT meta shows {'filename': …, 'lineno': N}.

Found in round-4 BQL dogfounding (#5b). Builds on the per-posting source-location resolution from #1510.

Design (why it's a shared view, not a 2-site patch)

rledger deliberately keeps source location in spans (file_id + offset), not in the meta map (beancount's per-entry filename-string model is exactly the memory cost rledger avoids). So filename/lineno-in-meta is a compatibility view synthesized at the BQL boundary, from a single resolver — not injected at load time, and not patched ad-hoc at each Value::Metadata site (which would leave meta the column augmented but meta()/entry_meta() reading raw maps — a split-brain).

One shared helper set, scope-aware:

  • source_location_meta_key(loc, key) → the synthetic filename/lineno for a location.
  • meta_lookup(raw, loc, key) — single-key access; user keys win (raw map consulted first).
  • augmented_meta(raw, loc) — materializes the full meta column (user keys win).

Routed through every accessor with the correct scope:

  • META/POSTING_META/ANY_META → posting location (the posting's own line).
  • ENTRY_META → the entry/directive location (the transaction's line).
  • meta column (default path and #postings) → augmented_meta.
  • getitem(meta, …) rides on the (now augmented) column value — no separate change.

All derive from #1510's single location resolver, so values agree with the filename/lineno columns.

Notes / deliberate choices

  • lineno via the meta surface is a Number (there is no integer MetaValue); the dedicated lineno column stays Integer. Number(Decimal) renders 4 and compares numerically — beanquery-compatible.
  • User-defined filename/lineno keys take precedence (raw map first).

Tests

test_meta_includes_source_location: meta('filename')/meta('lineno') per-posting; entry_meta('lineno') = entry line (scope); user key wins; getitem(meta,'lineno') augmented over #postings. Values verified against bean-query's SELECT meta. Full rustledger-query suite passes; clippy + fmt clean.

🤖 Generated with Claude Code

beanquery injects filename/lineno into every posting/entry meta dict; rledger
keeps source location in spans. Synthesize the two keys at the BQL boundary via
one shared, scope-aware view (meta_lookup / augmented_meta), routed through META/
POSTING_META/ANY_META (posting scope), ENTRY_META (entry scope), and the meta
column on both the default and #postings paths. getitem rides on the column
value. User keys win. 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:42
Copilot AI review requested due to automatic review settings June 22, 2026 03:42
@codecov

codecov Bot commented Jun 22, 2026

Copy link
Copy Markdown

Codecov Report

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

Files with missing lines Patch % Lines
...es/rustledger-query/src/executor/functions/util.rs 97.05% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@github-actions

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Tool Time Memory
rustledger 32.3ms ± 0.6ms 23.7 MB
beancount 844.1ms ± 4ms 50.4 MB
Comparison 26.1x faster 2.1x less

vs main branch (2026-06-22)

Metric main this PR Change
Time 34.9ms 32.3ms -7.4% 🟢
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 54da955 into main Jun 22, 2026
45 checks passed
@robcohen
robcohen deleted the fix/bql-meta-source-keys branch June 22, 2026 03:47

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 updates rustledger-query’s BQL executor to expose beanquery-compatible filename/lineno as synthetic metadata keys (in addition to the existing dedicated columns), so SELECT meta, meta('filename') / meta('lineno'), and getitem(meta, ...) can see source location even when the user didn’t define any metadata.

Changes:

  • Materialize an augmented meta dictionary that includes synthetic filename/lineno keys derived from resolved source locations (user keys still take precedence).
  • Route META / POSTING_META / ENTRY_META / ANY_META through a shared lookup path that can synthesize the source-location keys.
  • Add an executor test covering per-posting synthetic metadata and getitem(meta, ...) behavior.

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 Augments the #postings table’s meta column and adds tests for synthetic source-location metadata.
crates/rustledger-query/src/executor/functions/util.rs Adds shared helpers to synthesize and look up filename/lineno metadata keys by scope/location.
crates/rustledger-query/src/executor/evaluation.rs Ensures the default-row meta column is augmented using the resolved posting location.
Comments suppressed due to low confidence (1)

crates/rustledger-query/src/executor/mod.rs:2763

  • In the #postings table path, META/ENTRY_META/ANY_META functions read from the hidden _posting_meta / _entry_meta columns (see eval_meta_on_table_row), but those columns are still built from the raw metadata maps. This means meta('filename') / meta('lineno') will still return NULL when querying FROM #postings, even though the meta column itself is now augmented (split-brain between meta() and getitem(meta, ...)). To keep all metadata accessors consistent, augment the hidden meta columns with the same synthetic filename/lineno keys (scoped to posting vs entry location).
                    // Hidden metadata columns
                    Self::metadata_to_value(&txn.meta),
                    Self::metadata_to_value(&posting.meta),

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

Comment on lines +4204 to +4210
// The full meta column is augmented; getitem over it (and via #postings)
// sees the synthetic keys.
let r = executor
.execute(&parse("SELECT getitem(meta, 'lineno') FROM #postings").unwrap())
.unwrap();
assert_eq!(r.rows[0][0], Value::Number(dec!(2)));
assert_eq!(r.rows[1][0], Value::Number(dec!(3)));
@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

robcohen added a commit that referenced this pull request Jun 22, 2026
* feat(core): add MetaValue::Int(i64) variant (lineno -> Integer)

Add a first-class integer MetaValue. PR 1/2: variant + all match/wire arms +
cache version bump + synthetic BQL lineno uses Int; parser change is PR 2.
Resolves the Number-vs-Integer inconsistency from #1511 (#5b): meta(lineno) /
getitem(meta,lineno) are now true integers matching the lineno column.

Co-Authored-By: Claude Opus 4.8 <[email protected]>

* docs(loader): point CostNumber version-bump note at v12

Copilot on #1513: CACHE_VERSION is now 11, so the changelog hint should
reference the next bump (v12).

Co-Authored-By: Claude Opus 4.8 <[email protected]>

---------

Co-authored-by: Claude Opus 4.8 <[email protected]>
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