Skip to content

fix(query,ffi): one-arg value() is type-stable; column datatypes from first non-NULL row - #1703

Merged
robcohen merged 1 commit into
mainfrom
fix/value-type-stability
Jul 3, 2026
Merged

robcohen merged 1 commit into
mainfrom
fix/value-type-stability

Conversation

@robcohen

@robcohen robcohen commented Jul 3, 2026

Copy link
Copy Markdown
Member

Fixes the second route-smoke finding — two stacked defects (per the #1701 analysis):

1. value() was type-unstable across rows

The one-arg form inferred the target currency per row: Amount when a cost currency existed, the Inventory as-is when none did. Now: one-arg over Inventory → always Inventory (beanquery parity — value(inventory) is inventory-typed), converting per position with unpriced positions kept at raw units — which also fixes the silent-drop inconsistency vs the Position/Amount arms. The two-arg form keeps its Amount contract (explicit target is constant per query, never unstable — and existing tests pin number(value(sum(position), \"USD\"))); its pre-existing beanquery divergences are documented at the site rather than silently changed.

2. First-row datatype inference in the component

convert.rs declared each column's type from row 1 — baking in whichever shape that row had (and "null" when row 1 was NULL). Now: first non-NULL value per column, so no single row dictates a contract other rows contradict.

Regression tests pin both contracts across costed + costless groups (the exact pre-fix instability pair).

Closes #1701

🤖 Generated with Claude Code

Copilot AI review requested due to automatic review settings July 3, 2026 00:18
@robcohen
robcohen requested a review from a team as a code owner July 3, 2026 00:18
@codecov

codecov Bot commented Jul 3, 2026

Copy link
Copy Markdown

Codecov Report

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

Files with missing lines Patch % Lines
crates/rustledger-query/src/executor/mod.rs 83.33% 2 Missing ⚠️

📢 Thoughts on this report? Let us know!

@github-actions

github-actions Bot commented Jul 3, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Tool Time Memory
rustledger 32.6ms ± 1.8ms 23.7 MB
beancount 857.2ms ± 3.5ms 50.7 MB
Comparison 26.3x faster 2.1x less

vs main branch (2026-07-02)

Metric main this PR Change
Time 39.8ms 32.6ms -18.1% 🟢
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

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 addresses query result type/shape mismatches that caused FFI consumers to crash (issue #1701), by making VALUE()’s 1-arg behavior type-stable over inventories and improving FFI column datatype inference so it isn’t dictated by a single (possibly NULL) first row.

Changes:

  • Updated VALUE() evaluation so the one-argument form over Inventory always returns Inventory (type-stable across rows), while the explicit-currency form remains Amount.
  • Updated the FFI component’s column datatype inference to pick the first non-NULL value per column rather than using only row 1.
  • Added integration tests pinning both VALUE(sum(position)) (Inventory) and VALUE(sum(position), "USD") (Amount) contracts.

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 regression tests for VALUE() return-type stability across groups.
crates/rustledger-query/src/executor/mod.rs Adjusts VALUE() market-value conversion to keep 1-arg inventory results inventory-typed.
crates/rustledger-ffi-component/src/convert.rs Changes datatype inference to use the first non-NULL value per column instead of first row only.

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

@github-actions

github-actions Bot commented Jul 3, 2026

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

… first non-NULL row

Two stacked defects behind #1701 (Amount-declared column carrying
Inventory-shaped payloads in rustfava's Holdings route):

1. convert_to_market_value inferred the target currency PER ROW for the
   one-argument form (cost currency, else executor default), returning
   Amount when a target existed and the Inventory as-is when none did —
   a column type that depends on the row's data. The one-arg form over an
   Inventory now ALWAYS returns an Inventory (beanquery parity:
   value(inventory) is inventory-typed), converting per position; a
   position with no available price keeps its raw units, which also fixes
   the silent-drop inconsistency with the Position/Amount arms. The
   two-argument form keeps its Amount contract (explicit target is
   constant across the query, so it was never unstable); its pre-existing
   beanquery divergences are now documented at the site.

2. The component inferred each column's declared datatype from the FIRST
   row only — baking whichever shape row one carried into the contract the
   host trusts, and declaring "null" whenever row one happened to be
   NULL. It now infers from each column's first non-NULL value.

Regression tests pin both value() contracts (one-arg Inventory for every
row across costed and costless groups; two-arg Amount).

Closes #1701

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@robcohen
robcohen force-pushed the fix/value-type-stability branch from bb4b798 to 9accaad Compare July 3, 2026 02:48
@robcohen
robcohen merged commit 78c9833 into main Jul 3, 2026
47 of 48 checks passed
@robcohen
robcohen deleted the fix/value-type-stability branch July 3, 2026 03:21
@robcohen robcohen mentioned this pull request Jul 3, 2026
robcohen added a commit that referenced this pull request Jul 3, 2026
Minor (breaking) release carrying the route-smoke fix wave (#1699/#1700/
#1701, found by rustfava's new route-smoke suite):

- BREAKING parser/booking: compound cost {a # b} now carries both
  components and weighs N*a + b per beancount (#1700, via #1704).
  Ledgers that balanced under the old {a # b} == {{b}} misreading were
  genuinely unbalanced and now error; lot cost bases for compound specs
  are now correct. Binary caches invalidate automatically
  (CACHE_VERSION 13).
- BREAKING plugin-types: CostNumberData gained the Compound variant —
  exhaustive matches in plugin code need a new arm (deliberate: silent
  mishandling of compound costs is the failure mode the exhaustive enum
  prevents).
- FFI: WIT rustledger:ledger 3.2.0 -> 3.3.0 (additive compound case);
  runtime API_VERSION 3.3. Embedders regenerate bindings
  (rustfava#234); post-booking egress is unaffected.
- query: only() propagates NULL (fava Holdings by_currency 500, #1699
  via #1702); one-arg value() is inventory-typed for every row and the
  component declares column datatypes from the first non-NULL value
  (#1701, via #1703).

Workspace 0.19.1 -> 0.20.0; packages/mcp-server bumped to match.

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.

query: column declared Amount but value serialized as Inventory for value(sum(position))

2 participants