Skip to content

fix(query): route weight/cost(position) over #postings to the posting - #2431

Merged
robcohen merged 2 commits into
mainfrom
fix/postings-table-posting-route-2429
Sep 26, 2026
Merged

robcohen merged 2 commits into
mainfrom
fix/postings-table-posting-route-2429

Conversation

@robcohen

@robcohen robcohen commented Sep 26, 2026 •

Copy link
Copy Markdown
Member

What

weight(position) and cost(position) over FROM #postings now match the default FROM on every posting.

Both functions need the posting itself. weight ranks a price ahead of a cost, and a {{T}} lot's cost is its total. A Position value carries neither. The default FROM already reads the posting (#1966, #2428). #postings builds its rows as values, so it answered from the Position instead:

Query over #postings Before After (= default FROM)
weight(position) of 10 EUR @ 1.10 USD 10 EUR 11.00 USD (its weight column)
cost(position) of 3 X {{500 USD}} 500.0000000000000000000000001 500

How

  • build_postings_table computes both values per posting into two hidden columns:
    • weight uses compute_posting_weight, the canonical cost_number_weight path;
    • cost uses compute_posting_cost, falling back to the value path as the default FROM does.
  • The columns are named with a leading NUL, so they can't collide with user aliases, and Table::with_hidden keeps them out of SELECT *.
  • A third hidden column holds a row's cost(position) overflow, raised only when a query reads that row's cost(position). The default FROM also computes it lazily, so one overflowing lot doesn't fail SELECT account FROM #postings.
  • evaluate_subquery_expr routes WEIGHT(position) and COST(position) to these columns only when the column exists. A subquery that aliases something else as position has no hidden column, so it keeps the value path.

Tests

crates/rustledger-query/tests/postings_table_matches_default_from.rs:

  • Drift guard: every posting of a priced / {{T}} / per-unit ledger returns identical weight, weight(position) and cost(position) under both FROM clauses, with weight == weight(position). With the route disabled, this test fails.
  • SELECT * shows no hidden column.
  • A subquery aliasing units(position) AS position still gets 10 EUR.
  • An overflowing lot fails only the query that reads its cost(position), under both FROM clauses.

Checks

  • cargo clippy --workspace --all-features --all-targets -D warnings: clean.
  • Workspace tests: 5122 passed, 0 failed (at 57cbd1e); rustledger-query 753 passed at 3abe5ce.
  • cargo mutants --in-diff: 3 caught, 5 unviable, 0 missed.

Docs

Closes #2429

🤖 Generated with Claude Code

https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr

`#postings` materializes its rows as values, so `weight(position)` and
`cost(position)` there answered from the Position: a priced posting
weighed its units, and a `{{T}}` lot cost units x the rounded per-unit
cost. The default FROM reads the posting for both (#1966, #2425).

The table now computes both from the posting into two hidden
NUL-named columns, which `SELECT *` does not show, and the row
evaluator routes the two calls there when the column is present. A
subquery with its own `position` column carries no hidden column and
keeps the value path.

Closes #2429

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
@robcohen
robcohen requested a review from a team as a code owner September 26, 2026 02:53
@codecov

codecov Bot commented Sep 26, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.28571% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...tes/rustledger-query/src/executor/system_tables.rs 90.00% 2 Missing ⚠️

📢 Thoughts on this report? Let us know!

@github-actions

github-actions Bot commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Shape rustledger beancount Speedup RSS (rledger / beancount)
simple 44.2ms ± 0.6ms 820.0ms ± 10.4ms 18.5x 51.0 MB / 50.0 MB
investment 68.2ms ± 0.6ms 3065.7ms ± 46.5ms 44.9x 55.0 MB / 58.2 MB
fifo 65.6ms ± 0.6ms 15501.3ms ± 32.0ms 236.4x 55.1 MB / 58.0 MB

vs base, measured in this run

Shape base this PR Change RSS base → PR
simple 44.1ms ± 1.2ms 44.2ms ± 0.6ms +0.3% ✅ within noise 51.0 MB → 51.0 MB (+0.0%)
investment 68.5ms ± 0.7ms 68.2ms ± 0.6ms -0.4% ✅ within noise 55.0 MB → 55.0 MB (+0.0%)
fifo 66.0ms ± 0.9ms 65.6ms ± 0.6ms -0.6% ✅ within noise 55.0 MB → 55.1 MB (+0.2%)

A difference counts only when it clears the combined spread of the two measurements, per shape (2σ: simple 0.6ms, investment 0.4ms, fifo 0.5ms), 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%, 178 masked)
Full AST Match 100%
Total Files 813
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

…eads it

`#postings` computes every row's `cost(position)` up front, and raised
its overflow while building the table, so one lot whose cost overflows
failed every `#postings` query, `SELECT account` included. The default
FROM computes it only when asked. The table now keeps the error in a
hidden column, and the route raises it when a query reads that row's
`cost(position)`.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
@robcohen

robcohen commented Sep 26, 2026 •

Copy link
Copy Markdown
Member Author

Deep review

Fixed in 3abe5ce: an overflowing lot failed every #postings query

The table computes every row's cost(position) while it is built, and raised the overflow right there with ?. So one lot whose units × cost overflows failed every FROM #postings query, including SELECT account FROM #postings. The default FROM computes the cost only when a query asks for it, so it answered normally. That would have been a new failure introduced by this PR.

The fix:

  • The table keeps the overflow message in a third hidden column.
  • The route raises it only when a query reads that row's cost(position).
  • The value-path COST of a Position or NULL can only fail with this overflow, an Evaluation error. Any other error variant still fails the table immediately, so nothing gets relabeled.

On a ledger with one overflowing lot, both FROM clauses now give:

  • SELECT account → 2 rows;
  • SELECT cost(position) … WHERE account = 'Assets:C', which filters out the bad row → 1 row;
  • SELECT cost(position) → the overflow error.

A new test, an_overflowing_cost_fails_only_the_query_that_reads_it, fails on 57cbd1e and passes now.

Checked, no change needed

  • Other paths agree across both FROM clauses: GROUP BY with sum(weight(position)), WHERE, ORDER BY an expression, HAVING, first(), DISTINCT, and mixed case (WEIGHT(POSITION), Cost(Position)).
  • Overflow in weight(position): both FROM clauses give NULL, from the canonical posting_weight.
  • Hidden-column names: the NUL prefix can't be typed and can't appear in a subquery's rendered headers, so a user alias can't collide with them. SELECT * still hides all three.

Not in scope, noted

  • weight(position) over any subquery, e.g. FROM (SELECT *) on the default FROM, gets the Position value and returns units (10 EUR) for a priced posting. That is the WEIGHT() ignores the price annotation, returning units in the wrong currency #1966 bug one level up, and main behaves the same. A subquery row keeps no link to its posting, so fixing it means carrying the posting-derived values through the subquery. I'd file it as a follow-up, not fix it here.
  • FROM (SELECT * FROM #postings) doesn't parse.

Verification (3abe5ce)

  • cargo clippy --workspace --all-features --all-targets -D warnings: clean.
  • rustledger-query: 753 passed, 0 failed.
  • cargo mutants --in-diff on the whole PR diff: 3 caught, 5 unviable, 0 missed.

Second pass (3abe5ce): no new findings

  • Cost: callgrind on profile_query (20k transactions × 2 iterations, stubbed vs. real hidden columns) puts them at +19.7M instructions in build_postings_table, +1.45%, about 120 per row. The perf: #postings builds all 31 columns per row regardless of projection (~600ms for 40k postings) #2169 gate was for columns that were ~40% of the build, and its own comment says cheaper ones are not worth gating, so these stay ungated. Gating wouldn't remove the error column anyway: a WHERE that filters out an overflowing row must still answer.
  • Select-list alias: SELECT units(position) AS position, weight(position) gives 11.00 USD under both FROM clauses; the alias doesn't reach the argument.
  • Hidden-list reset: with_hidden assigns the list outright, but #postings had no earlier with_hidden call to overwrite. The name rule still hides _entry_meta and _posting_meta, and select_star_matches_bean_query_per_table passes.
  • OPEN/CLOSE/CLEAR: FROM #postings doesn't parse them, so there is no second path to reconcile.

@robcohen
robcohen merged commit 640e833 into main Sep 26, 2026
60 of 61 checks passed
@robcohen
robcohen deleted the fix/postings-table-posting-route-2429 branch September 26, 2026 09:13
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: weight(position) and cost(position) over FROM #postings skip the posting route (10 EUR for a priced posting; 500.00…01 for a {{T}} lot)

1 participant