Repository navigation
feat(query): SUM over a boolean counts the matching rows - #2227
Conversation
`sum(number > 0)` was a type error; it now answers 2 on a ledger with two positive postings. Closes #2214. Python sums booleans as integers, so bean-query computes the same number. What it PRINTS is `TRUE`: its result column is typed from the argument, so the integer goes through the boolean formatter. Its API returns `[(2,)]`. We match the value, not the rendering. Reproducing `TRUE` would mean reproducing a display bug, and `2` is what a user asking "how many postings are positive" means. The outputs therefore differ on purpose, recorded in `docs/reference/compatibility.md`. An all-boolean sum returns an Integer rather than a Decimal, since it is a count and `COUNT` answers with one. `has_booleans` is tracked apart from `has_numbers` so a mixed sum still comes back as a Number and the Python scale semantics are untouched -- widening the numeric arm instead would have made a total passing through zero render `0` where bean-query renders `0.00`. `AVG` over a boolean still refuses, which is also what bean-query does (`no function matches "avg(bool)"`). Agreeing by rejecting is still agreeing, and the test pins that widening SUM did not widen AVG. The accumulator is duplicated, and the split is not grouped-vs-ungrouped as it looks: one copy serves queries written without `FROM`, the other serves every `FROM #postings` form. Established by marking one copy and seeing which queries moved -- the first version of the test file exercised the same copy twice while claiming to cover both. Removing either copy's arm now fails exactly the tests that name it.
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
There was a problem hiding this comment.
🔵 Needs a closer look
The SUM type error message is now misleading because it does not mention booleans even though booleans are accepted.
Pull request overview
Adds BQL support for SUM over boolean expressions by treating booleans as integer counts (matching bean-query’s computed value rather than its CLI rendering), and records the intentional output divergence in the compatibility reference.
Changes:
- Extend
SUMaggregation to acceptValue::Booleanand return anIntegerfor all-boolean sums. - Add regression tests covering boolean sums (grouped, ungrouped, and no-
FROMaccumulator path) and guarding that numeric scale semantics remain unchanged. - Document the bean-query rendering mismatch and the deliberate choice to print the computed integer value.
File summaries
| File | Description |
|---|---|
| docs/reference/compatibility.md | Documents the deliberate output divergence for SUM(bool) and records related NULL-comparison compatibility notes. |
| crates/rustledger-query/tests/sum_over_booleans_test.rs | Adds regression tests covering boolean SUM behavior across both accumulator implementations and guards numeric scale behavior. |
| crates/rustledger-query/src/executor/aggregation.rs | Implements boolean handling in SUM across both aggregation paths and returns an integer for all-boolean totals. |
Review details
Suppressed comments (2)
crates/rustledger-query/src/executor/aggregation.rs:369
- SUM now accepts booleans, but the type error message still says it "requires numeric or position" values, which is misleading when users pass a boolean expression.
This issue also appears on line 910 of the same file.
_ => {
return Err(QueryError::Type(
"SUM requires numeric or position value".to_string(),
));
crates/rustledger-query/src/executor/aggregation.rs:913
- This SUM path also now accepts booleans, but the type error message still claims SUM only accepts numeric or position values.
_ => {
return Err(QueryError::Type(
"SUM requires numeric or position value".to_string(),
));
- Files reviewed: 3/3 changed files
- Comments generated: 0
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
📊 Benchmark Results
vs base, measured in this run
A difference counts only when it clears the combined spread of the two measurements, per shape (2σ: What do these numbers mean?
|
✅ Compatibility Test Results
What's tested
Full results available in workflow artifacts |
sum(number > 0)was a type error; it now answers2on a ledger with two positive postings. Closes #2214.The value, not the rendering
Python sums booleans as integers, so bean-query computes the same number. What its CLI prints is
TRUE— the result column is typed from the argument, so the integer goes through the boolean formatter. Through its API the number is visible:So "match bean-query" is ambiguous here, and this takes the value:
sum(number > 0)2TRUE2[('Assets:Cash', 0), ('Expenses:Food', 2)]count(number > 0)4444avg(number > 0)Reproducing
TRUEwould mean reproducing a display bug, and2is what a user asking "how many postings are positive" means. Recorded as a deliberate output divergence indocs/reference/compatibility.md, along with a section on #2213's NULL comparisons — that one is agreement, but it's easy to assume otherwise sinceWHEREfiltering is unaffected.Implementation notes
An all-boolean sum returns an
Integer, since it is a count andCOUNTanswers with one.has_booleansis tracked separately fromhas_numbersdeliberately: widening the numeric arm instead would have folded booleans into the Decimal total, and a mixed sum passing through zero would then render0where bean-query renders0.00.summing_numbers_is_unchangedpins that.AVGover a boolean still refuses, which is what bean-query does. Agreeing by rejecting is still agreeing, and the test pins that wideningSUMdid not widenAVG.On the duplicated accumulator
SUMis implemented twice, and the split is not grouped-vs-ungrouped as it appears: one copy serves queries written withoutFROM, the other serves everyFROM #postingsform, grouped or not.I found this the hard way. The first version of the test file tested the same copy twice while its docstring claimed to cover both — a removal experiment failed both tests instead of one, which is what exposed it. I marked one copy with a sentinel and re-ran the queries to establish the real split. Now, removing either copy's boolean arm fails exactly the tests that name it: site 1 →
sum_without_a_from_clause_counts_booleans, site 2 → the other two.Verification
4693 workspace tests green, clippy clean on 1.97.0, BQL compat harness over 71 files / 1349 runs shows no regression against baseline.