Skip to content

docs(query): record the two metadata divergences as decisions - #2172

Merged
robcohen merged 3 commits into
mainfrom
docs/bql-metadata-decisions
Aug 28, 2026
Merged

robcohen merged 3 commits into
mainfrom
docs/bql-metadata-decisions

Conversation

@robcohen

@robcohen robcohen commented Aug 27, 2026 •

Copy link
Copy Markdown
Member

What

#2168 raised two metadata divergences from bean-query. Both are deliberate,
but neither was written down where a reader would look, so both read as gaps.
This records them at the code and pins the one that can silently drift.

1. Metadata key order stays alphabetical

bean-query emits filename/lineno first, then the user's keys in
declaration order. We emit alphabetical:

bc: {'filename': ..., 'lineno': 4, 'aaa': ..., 'zzz': ...}
rl: {aaa: ..., filename: ..., lineno: 4, zzz: ...}

We can reproduce neither half. Metadata is FxHashMap<String, MetaValue>, so declaration order is lost at parse time; and Value::Object
is a BTreeMap, so the query layer cannot impose an order even if it had one.
Matching means an order-preserving map in rustledger-core: a new dependency
(indexmap is not one today) and a changed rkyv layout, since Metadata is
part of the archived cache payload and rkyv is a default feature — so a
CACHE_VERSION bump as well. All to change the order keys print in.

Correction to an earlier draft of this PR: I claimed this would also shift
meta.hash, which rustfava pins. That is wrong — rustledger-ffi-wasi::hash
sorts the metadata keys itself before hashing, precisely because Metadata is
a hash map, so the digest is already independent of iteration order. The
recommendation is unchanged, but that reason was not real and I had asserted
it without checking.

The sort is also the only thing making SELECT meta deterministic over a hash
map. Same shape as the #prices ordering in #2163: removing it to chase
bean-query would trade a stable order for an unstable one.

2. lineno metadata does not shadow the lineno column

bean-query stores the source location in the same dict users write to, so:

2020-01-03 * "p" "n"
  lineno: 999
$ bean-query ... "SELECT lineno FROM #entries"   ->  999      the user's value
$ rledger query ... "SELECT lineno FROM #entries" ->  4        the real line

We read the SourceLocation directly, so ours is immune by construction
rather than by a guard. A column named lineno should report where the
directive is, not what someone typed.

The meta map is the other way round: the user's value wins there, which
does match bean-query, because augmented_meta only fills absent keys.

Test

user_metadata_cannot_shadow_the_lineno_column asserts both halves, so
neither can drift toward the other — someone "fixing" this toward bean-query
would have to delete an assertion that says why not.

Confirmed to fail when the column is made to prefer metadata, which is
bean-query's behaviour.

Verification

413 tests, clippy at CI's 1.97, fmt, typos, rustdoc all clean.

Closes #2168

#2168 investigated. Both are deliberate; neither was written down where a
reader would look, so both read as gaps.

Metadata key order is alphabetical, not bean-query's `filename`/`lineno`
first then declaration order. We can reproduce neither half: `Metadata`
is an `FxHashMap`, so declaration order is lost at parse time, and
`Value::Object` is a `BTreeMap`, so the query layer cannot impose an
order even if it had one. Matching would mean an order-preserving map in
rustledger-core -- a new dependency, rkyv support for the cache format,
and a shifted `meta.hash` that rustfava pins -- to change the order keys
print in. The sort is also the only thing making `SELECT meta`
deterministic over a hash map, the same shape as the `#prices` ordering
in #2163.

bean-query lets a directive's `lineno:` metadata override its `lineno`
COLUMN, because it stores the location in the dict users write to. We
read the `SourceLocation` directly, so ours is immune by construction. A
column named `lineno` should say where the directive is, not what someone
typed. The `meta` map is the other way round -- the user's value wins,
matching bean-query -- and the new test asserts both halves so neither
can drift toward the other.

Confirmed the test fails when the column is made to prefer metadata.
@robcohen
robcohen requested a review from a team as a code owner August 27, 2026 19:37
Copilot AI lite review requested due to automatic review settings August 27, 2026 19:37

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

Records two deliberate BQL metadata divergences vs bean-query (alphabetical meta key order and non-shadowable lineno column) directly in the query executor code, and adds an integration test intended to pin the lineno behavior to prevent silent drift.

Changes:

  • Documented why SELECT meta emits metadata keys in deterministic alphabetical order (and why bean-query order cannot be reproduced without broader structural changes).
  • Documented that the lineno column is sourced from SourceLocation (not user metadata), while meta["lineno"] still allows the user’s value to win (bean-query-compatible).
  • Added an integration test to assert the non-shadowing behavior and the meta behavior.

Reviewed changes

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

File Description
crates/rustledger-query/tests/bql_integration_test.rs Adds a regression test intended to pin lineno column vs meta["lineno"] shadowing behavior.
crates/rustledger-query/src/executor/mod.rs Adds rationale/comments explaining deterministic alphabetical metadata key ordering for meta.
crates/rustledger-query/src/executor/functions/util.rs Expands documentation for source_lineno_value to record the deliberate lineno behavior divergence.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@github-actions

github-actions Bot commented Aug 27, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Shape rustledger beancount Speedup RSS (rledger / beancount)
simple 49.7ms ± 5.2ms 805.2ms ± 6.0ms 16.2x 55.0 MB / 50.1 MB
investment 68.5ms ± 4.0ms 3032.7ms ± 24.7ms 44.3x 59.0 MB / 57.9 MB
fifo 66.8ms ± 3.9ms 15583.0ms ± 441.8ms 233.4x 61.0 MB / 57.9 MB

vs base, measured in this run

Shape base this PR Change RSS base → PR
simple 48.3ms ± 3.6ms 49.7ms ± 5.2ms +2.9% ✅ within noise 55.1 MB → 55.0 MB (-0.2%)
investment 68.2ms ± 3.0ms 68.5ms ± 4.0ms +0.5% ✅ within noise 59.0 MB → 59.0 MB (+0.0%)
fifo 67.7ms ± 3.0ms 66.8ms ± 3.9ms -1.5% ✅ within noise 61.1 MB → 61.0 MB (-0.1%)

A difference counts only when it clears the combined spread of the two measurements, per shape (2σ: simple 2.8ms, investment 2.3ms, fifo 2.2ms), and is at least 5%.
10,000 transactions per shape · 20 runs, 3 warmup · hyperfine

What do these numbers mean?
  • simple — Two-posting transfers, one currency. The historical workload — keep as the floor, not as the benchmark.
  • investment — Cost specs, per-unit prices and lot reductions. Exercises booking and inventory, which simple leaves at 0.3%.
  • fifo — Bare {} reductions against a FIFO account — how FIFO sells are normally written. The other four shapes reach the ordered-selection path twice in 2k transactions between them, so it was effectively unprofiled until it turned out to be quadratic (plan_ordered scans every lot per reduction: FIFO ledgers are quadratic (8.8s at 20k transactions) #2083).
  • vs base: the PR and the base-branch commit it is built on, built and measured in the same job
  • RSS: peak resident set size, a single sample — reported without a verdict because there is no spread to test it against
  • A shape with no beancount column is one bean-check reports errors on; it is timed for rledger only

@github-actions

Copy link
Copy Markdown
Contributor

✅ Compatibility Test Results

Metric Result
Check Compatibility 100%
BQL Compatibility 100% (raw 93%, 170 masked)
Full AST Match 100%
Total Files 811
What's tested
  • Check: Exit codes and error counts match bean-check
  • BQL: Query results match bean-query
    The headline is the EFFECTIVE rate, after pairs where beanquery
    itself is wrong are masked. raw is the same run with nothing
    masked. A widening gap means the registry grew, not that rledger
    changed.
  • Files: ~800 test files downloaded from 10+ beancount repositories

Full results available in workflow artifacts

@codecov

codecov Bot commented Aug 27, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Review catch on #2172. The test built an `Executor` from a span with
`file_id: 0` against an empty `SourceMap`, so the lookup missed and the
`lineno` column resolved to 0. The assertions were "an integer" and "not
999", both of which 0 satisfies -- so the test passed even with source
locations entirely broken, which is the regression it exists to catch.

Now it registers a real file, puts the note on line 3, and asserts
`Integer(3)`. Confirmed to fail both ways: when the column is made to
prefer metadata, and when the source location is dropped. The old version
passed that second mutation.

The metadata value is `MetaValue::Int`, not `String`. A real parse of
`lineno: 999` yields a number -- `-f json` renders it unquoted -- so the
fixture now matches what the parser actually produces.
Self-review catch on #2172. The comment listed "a shifted meta.hash that
rustfava pins" among the costs of making `Metadata` order-preserving.
That is not true. `rustledger-ffi-wasi::hash::metadata` sorts the keys
itself before hashing -- precisely because `Metadata` is an `FxHashMap`
and iterating it would make the digest nondeterministic -- so the hash is
already independent of iteration order.

I asserted it without checking, in a session spent flagging exactly that
habit.

The recommendation is unchanged and the two real costs are now stated
precisely: `indexmap` is not a dependency today, and `Metadata` is part
of the rkyv-archived cache payload with rkyv on by default, so the layout
change needs a CACHE_VERSION bump.
@robcohen
robcohen merged commit b2e9c6f into main Aug 28, 2026
60 of 61 checks passed
@robcohen
robcohen deleted the docs/bql-metadata-decisions branch August 28, 2026 12:48
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.

BQL: metadata key order is alphabetical not declaration order; bean-query lets user meta shadow the lineno column

2 participants