Repository navigation
fix(query): a comparison with a NULL operand yields NULL - #2225
Conversation
`payee != ''` answered FALSE on a payee-less posting where bean-query answers NULL. Closes #2213. FALSE and NULL are not interchangeable downstream: `COUNT(expr)` skips NULLs but counts every FALSE, so `count(payee != '')` over payee-less rows answered 4 where it should answer 0. `COUNT` excluding NULLs was already correct and tested -- it was the comparison feeding it that manufactured a non-NULL. All six comparison operators shared one guard, so all six were wrong, and the fix is that guard returning `Value::Null`. The target is Python's rule, which is what beanquery implements, not strict SQL three-valued logic: `NOT (NULL)` is TRUE there (Python's `not None`), where SQL would say NULL. That stays as it is and is now pinned, so a later "make it proper 3VL" change meets the decision instead of assuming the current answer is an oversight. Filtering is unaffected: `to_bool(NULL)` is false, so `WHERE` still drops the row. That is why the bug survived -- filtering looked right, and only projecting or counting a comparison exposed it. Verified against beanquery 0.2.0 / beancount 3.2.3: all seven expressions in the issue now agree. The workspace suite passed unchanged before these tests were added, which is the coverage gap that let this land; two of the five new tests fail against the old behavior, and the other three pin invariants that must NOT move (`WHERE` filtering, `NOT` coercion, and ordinary two-operand comparisons still being boolean).
There was a problem hiding this comment.
🟢 Approval recommended
The semantic change is small, targeted, and backed by focused regression tests that cover both the bug and the key invariants that must not change.
Pull request overview
Fixes BQL comparison semantics in rustledger-query so that =, !=, <, <=, >, >= with a NULL operand yield NULL (matching beanquery / Python-beancount behavior), preventing downstream aggregates like COUNT(expr) from incorrectly counting FALSE values.
Changes:
- Updated the executor’s comparison short-circuit so NULL comparisons return
Value::Nullinstead ofValue::Boolean(false). - Added regression tests covering all six comparison operators,
COUNTbehavior over NULL comparisons,WHEREfiltering behavior, andNOT (NULL)coercion behavior.
File summaries
| File | Description |
|---|---|
| crates/rustledger-query/src/executor/operators.rs | Changes comparison short-circuit to return NULL when either operand is NULL for the six core comparison operators. |
| crates/rustledger-query/tests/null_comparison_test.rs | Adds regression tests to pin NULL-comparison semantics and the downstream effects on COUNT, WHERE, and NOT. |
Review details
Suppressed comments (1)
crates/rustledger-query/src/executor/operators.rs:265
- This block comment states “A comparison with a NULL operand is NULL”, but the guard only applies when
is_comparison(op)is true (the six equality/ordering operators). Tightening the wording prevents readers from assumingIN/NOT IN/regex operators also return NULL on NULL operands.
// A comparison with a NULL operand is NULL, not FALSE (#2213). The two
// are not interchangeable: `COUNT` skips NULLs but counts every FALSE,
// so `count(payee != '')` over payee-less rows answered 4 where it
// should answer 0. Filtering is unaffected -- `to_bool(NULL)` is false,
// so `WHERE` still drops the row.
- Files reviewed: 2/2 changed files
- Comments generated: 1
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
📊 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 |
Extends the previous commit. Review asked whether the NULL rule really covered every comparison, since `~`, `!~`, `IN` and `NOT IN` were left out of the guard. Checking against beanquery 0.2.0 rather than assuming: on a payee-less posting bean-query answers NULL for all four, where we answered FALSE, TRUE, FALSE, TRUE. Both regex arms carried a comment stating the old answer "matches Python beancount behavior". It does not, and the oracle says so directly. Those arms are now unreachable and the claim is gone. An empty collection is NOT null and must keep its boolean answer: `'food' IN tags` on an untagged posting is FALSE, agreeing with bean-query, because `tags` there is an empty SET rather than a missing value. Pinned by `membership_in_an_empty_set_is_false_not_null` -- the mistake would look like a fix, since an untagged posting reads as "missing", and it would break a common query shape. `is_comparison` is renamed `propagates_null`, which is what the predicate now decides; it documents why `IS NULL`, `AND` and `OR` stay out.
|
Good catch, and it was more than a wording problem — the fix was incomplete. The comment was imprecise because the guard covered only the six equality/ordering operators. Rather than just narrow the wording, I checked what
All four were the same #2213 bug. Both regex arms carried a comment asserting the old answer "matches Python beancount behavior" — it does not, and the oracle says so directly. Those arms are now unreachable and the false claim is gone.
The risk this opened. An untagged posting reads as "missing", so it would be easy to propagate NULL from Re-verified: 4688 workspace tests green, clippy clean on 1.97.0, and the BQL compat harness over 71 files / 1349 runs shows no regression against baseline. |
payee != ''answeredFALSEon a payee-less posting where bean-query answers NULL. Closes #2213.FALSE and NULL are not interchangeable downstream.
COUNT(expr)skips NULLs but counts every FALSE, socount(payee != '')over payee-less rows answered 4 where it should answer 0.COUNTexcluding NULLs was already correct and tested — it was the comparison feeding it that manufactured a non-NULL.All six comparison operators shared one guard, so all six were wrong. The fix is that guard returning
Value::Null.Target semantics
Python's rule, which is what beanquery implements — not strict SQL three-valued logic.
NOT (NULL)isTRUEthere (Python'snot None) where SQL would say NULL. That behavior stays and is now pinned by a test, so a later "make it proper 3VL" change meets the decision rather than assuming the current answer is an oversight.Filtering is unaffected:
to_bool(NULL)is false, soWHEREstill drops the row. That is why this survived — filtering looked right, and only projecting or counting a comparison exposed it.Verification
Against beanquery 0.2.0 / beancount 3.2.3, all seven expressions from the issue now agree:
payee != ''FALSEpayee = ''FALSEpayee > 'a'FALSEpayee < 'a'FALSEpayee IS NULLTRUETRUETRUENOT (payee = '')TRUETRUETRUEcount(payee != '')040Also checked on a mixed ledger that ordinary comparisons are untouched (
'Cafe' = 'Cafe'→ TRUE,WHERE payee != ''→ 2 rows), agreeing with bean-query on each.BQL compat harness over 71 corpus files / 1349 runs: no regression against the pre-change baseline, no new stale masks, counts unchanged. The corpus barely exercises NULL comparisons, which is part of why this survived.
On the coverage gap
The full workspace suite (4686 tests) passed unchanged after the semantic fix, before any test was added — nothing covered this at all. Two of the five new tests fail against the old behavior; the other three deliberately pass both ways because they pin invariants that must not move (
WHEREfiltering,NOTcoercion, and ordinary two-operand comparisons still being boolean). Without that third group, returning NULL unconditionally would satisfy the rest of the file.Not addressed here: the issue's secondary note that
SELECT count(*) ... WHERE <no matches>returns no row from bean-query where we return0. That is an aggregate-over-empty-input difference, already recorded as a deliberate divergence inaggregate_count_null_test.rs.