Skip to content

test(query): algebraic properties, and a generator that reaches boundaries - #1982

Closed
robcohen wants to merge 1 commit into
mainfrom
test/query-properties
Closed

robcohen wants to merge 1 commit into
mainfrom
test/query-properties

Conversation

@robcohen

@robcohen robcohen commented Aug 7, 2026

Copy link
Copy Markdown
Member

#1902 Phase 1 — the last uncovered crate. Miri excludes rustledger-query for
rowan reasons that are not going away, so this is the compensating coverage.

Three algebraic properties

The existing properties ask whether the same input gives the same answer twice.
These ask whether the answer is right, using relationships that hold whatever
the ledger contains — so they need no expected value, which is what makes them
checkable over generated input.

property what it catches
WHERE p and WHERE NOT p partition the unfiltered rows a predicate that misevaluates in one direction only — the union stops reconstructing the whole
grouping preserves the total the #1177 shape: rows right, group-key sidecar wrong
ORDER BY is a permutation a comparator that is not a total order losing or duplicating rows

Aimed where this crate actually breaks: #1177 was a grouping/parallel-path
divergence, and the two bugs found by hand this cycle (#1963, #1966) were both
in evaluation rather than parsing. The neighbouring null-ordering tests pin
which order; none pinned that the multiset survived it.

The generator was the more interesting finding

Sabotaging the partition property is how it surfaced. Swapping number > 0 for
number > 1 — a filter that should lose every row between them — changed
nothing.

With a uniform -1_000_000..1_000_000, landing in (0, 1] needs
|n| <= 10^scale: at most 1000 of two million draws, roughly 0.3 expected
hits across an entire run
. There was nothing between 0 and 1 to lose.

The amount strategy now spans three magnitude bands, and that same sabotage
fails. This affects the pre-existing determinism properties too, which were
sampling the same narrow band.

Sabotage-verified

sabotage result
the two halves overlap partition fails
compare against a filtered total grouping fails
drop a row from the ordered side permutation fails
number > 1 (boundary) fails now; passed before the generator fix

Worth noting the third and fourth: my first two attempts at sabotaging the
partition were no-ops — one because the generator filters zero amounts, one
because it never produced boundary values. A sabotage that doesn't fail is
either a bad sabotage or a real gap, and telling those apart is where the
generator weakness came from.

cargo test --workspace — 132 suites, 0 failures. Clippy clean.

…aries

#1902 Phase 1, the last uncovered crate. Miri excludes this one for
rowan reasons that are not going away, so this is the compensating
coverage.

The existing properties ask whether the same input gives the same
answer twice. These ask whether the answer is RIGHT, via relationships
that hold whatever the ledger contains - so they need no expected
value, which is what makes them checkable over generated input:

- WHERE p and WHERE NOT p must PARTITION the unfiltered rows. Neither
  half may invent, lose or double-count. A predicate that misevaluates
  in one direction only shows up as a union that no longer
  reconstructs the whole.
- Grouping must preserve the total: SUM(number) GROUP BY account,
  summed back over groups, equals the ungrouped SUM. That is the
  invariant #1177 broke - rows right, group-key sidecar wrong.
- ORDER BY must permute, never add, drop or alter. Sorting is where a
  comparator that is not a total order loses rows, and the
  null-ordering tests next door pin WHICH order rather than that the
  multiset survived.

Aimed where this crate actually breaks: #1177 was a grouping/parallel
divergence, and the two bugs found by hand this cycle (#1963, #1966)
were both in evaluation rather than parsing.

Also widens the amount generator, which sabotaging the partition
property is how I found. Swapping `number > 0` for `number > 1`
changed NOTHING - because with a uniform -1_000_000..1_000_000 range,
landing in (0, 1] needs |n| <= 10^scale, at most 1000 of two million
draws, about 0.3 expected hits across a whole run. There was nothing
between 0 and 1 to lose. Now three magnitude bands, and that same
sabotage fails.

Every property sabotage-verified: overlapping halves fail the
partition, a wrong total fails the grouping, a dropped row fails the
permutation, and the boundary case fails now where it did not before.
@robcohen
robcohen requested a review from a team as a code owner August 7, 2026 13:10
Copilot AI review requested due to automatic review settings August 7, 2026 13:10
@github-actions

github-actions Bot commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Tool Time Memory
rustledger 36.5ms ± 1.7ms 23.9 MB
beancount 883.8ms ± 2.8ms 50.7 MB
Comparison 24.2x faster 2.1x less

vs main branch (2026-08-07)

Metric main this PR Change
Time 37.1ms 36.5ms -1.6% ✅
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

@codecov

codecov Bot commented Aug 7, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

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 expands verification coverage in rustledger-query by adding property-based “algebraic invariant” tests (partitioning predicates, grouping totals, and ORDER BY permutation) and by strengthening the generated amount distribution so boundary-adjacent values are exercised.

Changes:

  • Improve the proptest amount generator to cover multiple magnitude bands, increasing the likelihood of boundary hits around 0 and 1.
  • Add algebraic property tests for WHERE p/WHERE NOT p partitioning, grouping total preservation, and ORDER BY being a permutation of the unordered results.

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

Comment on lines +266 to +268
prop_assert!(!g.rows.is_empty(), "grouping must produce at least one group");
prop_assert_eq!(t.rows.len(), 1, "an ungrouped SUM is a single row");

@github-actions

github-actions Bot commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

⚠️ Compatibility Test Results

Metric Result
Check Compatibility 100%
BQL Compatibility 95% (raw 89%, 162 masked)
Full AST Match 100%
Total Files 812
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

@robcohen

robcohen commented Aug 7, 2026

Copy link
Copy Markdown
Member Author

Superseded by #1983 — same commits plus Copilot's per-key grouping assertion, on chore/query-properties. This branch was test/, which the branch-name gate rejects (allowed: feat, feature, fix, docs, chore, refactor, release, hotfix, perf, claude, dependabot, copilot). Renaming a branch closes its PR anyway, so reopening under the right prefix was the cheaper move.

@robcohen robcohen closed this Aug 7, 2026
@robcohen
robcohen deleted the test/query-properties branch August 7, 2026 13:30
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