Repository navigation
fix(query): MIN and MAX order booleans, false before true - #2212
Conversation
`SELECT max(number > 0)` was a type error. bean-query answers TRUE,
because Python orders `False < True` and its `max`/`min` inherit that,
so MAX over booleans is `any` and MIN is `all`.
The order is defined for the aggregate path ONLY, and that boundary is
measured rather than assumed. bean-query refuses the operators on the
same values:
SELECT (number > 0) < (number > 5) FROM #postings
-> CompilationError: operator "less(bool, bool)" not supported
so `compare_values`, which backs `<` and `>`, keeps rejecting them.
Defining the order there would accept a query bean-query rejects at
compile time -- a divergence in the opposite direction from the one
being fixed. `value_less_than` is reached only by MIN/MAX, which is what
makes the split expressible at all.
Checked end to end against beanquery 0.2.0: `SELECT MAX(number > 0),
MIN(number > 0)` emits `TRUE,FALSE` from both tools, byte for byte. That
query joins the BQL compat corpus, which asks for one whenever an
aggregation function changes.
Three tests, each verified to fail on its own mutation: removing the
boolean arm, and adding it to `compare_values` as well. The second is
the one worth having -- it stops the order being extended to the
operators later as an obvious tidy-up.
Closes #2183
There was a problem hiding this comment.
🟢 Approval recommended
The change is narrowly scoped to the MIN/MAX aggregate path, includes regression tests preventing accidental operator behavior changes, and updates the compatibility corpus to lock in the expected behavior.
Pull request overview
This PR fixes BQL MIN/MAX aggregation over boolean expressions by defining an ordering of booleans as false < true only in the aggregate comparison path, matching bean-query / Python behavior while keeping boolean comparisons via </> rejected.
Changes:
- Extend the aggregate-only comparison helper (
value_less_than) to orderValue::Boolean(false) < Value::Boolean(true). - Add a focused regression test ensuring
MIN/MAXover booleans works while<on booleans still errors. - Add an end-to-end compatibility corpus query covering
MAX(number > 0), MIN(number > 0).
File summaries
| File | Description |
|---|---|
| tests/compatibility/bql-queries.toml | Adds a compat corpus query to lock in correct boolean MIN/MAX behavior. |
| crates/rustledger-query/tests/boolean_minmax_test.rs | New regression tests for boolean aggregate ordering and operator rejection. |
| crates/rustledger-query/src/executor/operators.rs | Implements boolean ordering in value_less_than (aggregate-only), leaving compare_values unchanged. |
Review details
- 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.
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 |
|
Review notes on this PR. Verified the claim I made about scope. All four NULLs are handled before the comparison. Both aggregates skip
Two things this turned up that are not in scope here:
Worth stating plainly: this PR makes that pre-existing divergence visible where it previously hit |
Reviewing this PR I went to mutate the boolean arm and my anchor matched twice: `compare_values_for_sort` already ordered booleans on main. There are three comparison functions in this file with three different policies, and nothing said so. They are not drift. They are bean-query's shape: it answers `max(number > 0)`, it sorts a boolean column, and it refuses `(number > 0) < (number > 5)` with `operator "less(bool, bool)" not supported`. Collapsing them would either accept a query it rejects or reject two it answers. The table is in the doc comment now, along with the reason the sort path is total where the other two are fallible: sorting cannot fail partway through a result set, so it orders NULL rather than erroring on it. Also verified the corpus entry is real coverage rather than decoration. Run against the file list the harness wants, `minmax-over-boolean` executes and reports `match: True`; reverse the boolean order and the same run reports `match: False`. The first attempt at that control was the anchor collision above, so it silently mutated nothing and reported a match either way.
|
Checked the sibling aggregates, since MIN/MAX cannot be the only ones that meet a boolean.
That also settles the scope question for this PR: MIN and MAX were the only two aggregates where bean-query orders booleans and we errored. One more note for whoever picks up #2177: |
Closes #2183.
Python orders
False < Trueand itsmax/mininherit that, so MAX over booleans isanyand MIN isall. bean-query gets it for free; we had to define the order.The boundary is the interesting part
The order is defined for the aggregate path only, and that is measured, not assumed. bean-query refuses the operators on the very same values:
So
compare_values— which backs<and>— keeps rejecting booleans. Defining the order there would accept a query bean-query rejects at compile time, a divergence in the opposite direction from the one being fixed.That split is expressible only because
value_less_thanis reached from nowhere butMIN/MAX; I checked its call sites before touching it.Verification
End to end against beanquery 0.2.0 / beancount 3.2.3, through the real
bean-queryCLI:Byte for byte. That query joins
tests/compatibility/bql-queries.toml, whose header asks for an entry whenever an aggregation function changes.Worth noting: my first comparison used beanquery's Python API plus
csv.writer, which printedTrue/Falseand looked like a mismatch. That bypasses bean-query's own renderer — the CLI printsTRUE/FALSE, matching #2182. The oracle has to be the CLI when output format is what's under test.Controls
value_less_thancompare_valuesThe second is the one worth having: it stops the order being extended to the operators later as an obvious tidy-up.
Numbers still aggregate unchanged (
max(number)→ 10,min(number)→ -10), so the new arm shadows nothing.🤖 Generated with Claude Code
https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr