Skip to content

fix(query): refuse * as the argument of any function but count, as bean-query does - #2887

Merged
robcohen merged 4 commits into
mainfrom
fix/bql-star-args
Nov 7, 2026
Merged

robcohen merged 4 commits into
mainfrom
fix/bql-star-args

Conversation

@robcohen

@robcohen robcohen commented Nov 7, 2026 •

Copy link
Copy Markdown
Member

Behavior change (read first)

A query that passes * to any function but count now errors where it answered.

  • SELECT sum(*) used to return one blank row, exit 0. It now fails with no function matches "sum(*)", exit 1.
  • WHERE sum(*) > 0 used to match nothing silently. It now fails the same way.
  • MIN(*) OVER () used to report invalid arguments for function MIN: .... It now reports no function matches "min(*)", the same error as the aggregate min(*).

What changed

  • Compile-time refusal (executor/execution.rs): the new check_star_arguments runs before any path executes a SELECT, ahead of the aggregate-placement check.
    • It refuses a * argument to any call but count, plain or window.
    • It covers every clause: targets, FROM filter, WHERE, GROUP BY, HAVING, PIVOT BY, ORDER BY. A subquery goes through the same path.
    • The error is QueryError::UnknownFunction("sum(*)"). That is the same variant and wording the crate already uses for bean-query's no function matches "<sig>" name and argument types, with the name lower-cased as bean-query prints it (SUM(*) gives sum(*)).
  • Parser polish listed in the issue (parser.rs):
    • Window *: a window's PARTITION BY * and ORDER BY * are refused by name with '*' is not a value: ..., at the *. simple_arg took a bare *. bean-query has no window functions, so this is rledger's own surface.
    • count(* 1): now reads expected ')' after the '*': it stands alone in a call such as count(*), at the 1. It used to read found '1' expected identifier, ..., ',', or ')'. A * that an operator or , follows is still refused as '*' is not a value.
    • operator_follows: the operator characters are now the shared OPERATOR_STARTS const. The operator definitions in expr carry a cross-reference to it. test_star_before_every_operator_is_named runs SELECT * <op> 1 for every BinaryOperator, through an exhaustive match, so a new operator must add a row there. I checked that the test fails when OR is dropped from operator_follows.

bean-query 0.2.0 behavior (rustledger-compat image)

Query bean-query
SELECT sum(*), SUM(*), first(*), units(*), nosuch(*) CompilationError: no function matches "sum(*)" name and argument types (each with its own name, lower-cased)
... WHERE sum(*) > 0, FROM sum(*) = 1, ORDER BY sum(*), HAVING sum(*) > 0, FROM (SELECT sum(*)) the same sum(*) error
SELECT count(*), COUNT(*) answers
SELECT count(* 1), count(*1), count(*) OVER (PARTITION BY *) ParseError: syntax error

One known difference: bean-query names whichever error comes first in the query. SELECT nosuchcol, sum(*) is column "nosuchcol" not found there, and no function matches "sum(*)" here. Both refuse. This is noted in the CHANGELOG.

Verification

  • New tests in crates/rustledger-query/tests/bql_literals_and_wildcard_test.rs:
    • test_execute_refuses_a_wildcard_argument_but_count asserts the exact error string for 14 queries across every clause, a subquery and a window. It also checks that the count(*) forms still answer.
    • test_parse_refuses_the_wildcard_in_a_window
    • test_parse_names_the_close_a_call_star_needs
    • test_star_before_every_operator_is_named
  • window_min_max_test::arguments_are_checked now expects UnknownFunction("min(*)") for MIN(*) OVER ().
  • Each guard fails against main: the sweep below shows main answering or giving the old message on every one of these queries.
  • Sweep, main vs this branch:
    • Ran 1428 distinct queries through rledger query on tests/fixtures/examples/example.beancount, with debug builds in separate worktrees and target dirs. The queries were extracted from docs/**/*.md and from string literals in crates/*/tests and crates/rustledger-query/src.
    • 1403 were byte-identical, output and exit code. The 25 that differ are exactly this PR's new test queries.
  • Checks:
    • cargo test -p rustledger-query --all-features: 998 pass, after merging origin/main.
    • cargo clippy -p rustledger-query --all-features --all-targets -- -D warnings
    • RUSTDOCFLAGS=-Dwarnings cargo doc --workspace --no-deps
    • cargo fmt --check

How to test

cargo test -p rustledger-query --all-features --test bql_literals_and_wildcard_test
cargo test -p rustledger-query --all-features --test window_min_max_test
rledger query tests/fixtures/examples/example.beancount 'SELECT sum(*)'   # error, exit 1

Review round 1

  • The shorthand statements now run the check. check_star_arguments ran only on SELECT. check_shorthand_filters now checks the FROM filter and WHERE clause of JOURNAL, BALANCES and PRINT. Before:

    • JOURNAL "Assets" FROM sum(*) = 1, BALANCES WHERE sum(*) > 0 and BALANCES FROM sum(*) = 1 gave 0 rows with exit 0.
    • PRINT FROM sum(*) = 1 gave a different error.

    All four now give no function matches "sum(*)", as bean-query does.

  • Aggregates in the same clauses are refused too. The three statements also skipped check_aggregate_placement. They now refuse with bean-query's messages:

    • JOURNAL "Assets" FROM count(*) = 1, JOURNAL FROM count(*) = 1, BALANCES FROM count(*) = 1 and PRINT FROM count(*) = 1 give aggregates are not allowed in FROM clause.
    • BALANCES WHERE count(*) > 0 gives aggregates are not allowed in WHERE clause.
  • Error order matches bean-query among these two checks. bean-query compiles FROM before WHERE, and in each clause the calls before the aggregates. So BALANCES FROM count(*) = 1 WHERE sum(*) > 0 names the FROM aggregate, and BALANCES FROM sum(*) = 1 WHERE count(*) > 0 names sum(*). Every row was checked against bean-query 0.2.0, and the tests assert the exact error. The order holds only between the * and aggregate checks: they run before columns and functions are resolved, so a clause that also names an unknown column or function gets the * or aggregate error first, where bean-query names the unknown name (see review round 2).

  • Tests:

    • test_execute_refuses_a_wildcard_argument_but_count gained the four JOURNAL/BALANCES/PRINT rows.
    • The new test_execute_refuses_an_aggregate_in_a_shorthand_filter covers the aggregate cases and the two ordering cases, and checks that the statements still answer without an aggregate.
    • With the three check_shorthand_filters calls removed, both tests fail: "JOURNAL \"Assets\" FROM sum(*) = 1" answered [].
  • Nit: test_star_before_every_operator_is_named now builds its operator list and an exhaustive match from one macro invocation. A new BinaryOperator does not compile until it is listed, and once listed it is tested.

  • CHANGELOG updated for the shorthand statements.

  • Sweep re-run against main (4d786b4b44d, separate worktree and target dir): 1525 queries, now including JOURNAL/BALANCES/PRINT strings from the tests. 1489 are identical. The 36 that differ are exactly this PR's test queries.

  • Checks:

    • cargo test -p rustledger-query --all-features: 999 pass.
    • cargo clippy -p rustledger-query --all-features --all-targets -- -D warnings, RUSTDOCFLAGS=-Dwarnings cargo doc --workspace --no-deps, cargo fmt --check.
  • Merged origin/main (test(plugin): keep the back-to-back timeout test independent of machine load #2886).

Review round 2

  • Docs only. The * and aggregate checks run before columns and functions are resolved, in SELECT (as on main) and now in JOURNAL, BALANCES and PRINT.
    • So BALANCES FROM nosuchcol = 1 WHERE count(*) > 0 and BALANCES WHERE nosuchfn(date) = 1 AND count(*) > 0 name the aggregate here, where bean-query names the unknown column or function.
    • Both refuse, and the behavior is kept.
  • The CHANGELOG's "known difference" sentence now covers aggregates, unknown functions and the shorthand statements, with these examples.
  • The round-1 "error order" bullet is qualified to say the order holds only between the * and aggregate checks.
  • Commit 5271042cd98. origin/main had not moved, so there was no merge.

Closes #2880

🤖 Generated with Claude Code

…s bean-query does

bean-query 0.2.0 parses `*` as any call's only argument, then refuses it at
compile time unless the call is `count`: `SELECT sum(*)` is `no function
matches "sum(*)" name and argument types`. rledger ran it and answered one
blank row with exit status 0, and `WHERE sum(*) > 0` matched nothing.

The executor now refuses such a call before the query runs, in every
clause and in a subquery, with `no function matches "sum(*)"` (the name
lower-cased, as bean-query prints it). A window call takes `*` only for
`count` too, so `MIN(*) OVER ()` gives the same error instead of
`invalid arguments`.

Parser polish from the same review:
- a window's PARTITION BY and ORDER BY refuse `*` by name, as anywhere a
  value belongs;
- `count(* 1)` reads `expected ')' after the '*'` at the `1`, where it
  listed `,` among what may follow the `*`;
- the hand-written operator list `target` uses is cross-referenced with
  the operator definitions and shares `OPERATOR_STARTS`, and a test over
  every `BinaryOperator` pins that a `*` before each gets the named error.

Closes #2880

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@robcohen
robcohen requested a review from a team as a code owner November 7, 2026 05:39
@codecov

codecov Bot commented Nov 7, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@github-actions

github-actions Bot commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Shape rustledger beancount Speedup RSS (rledger / beancount)
simple 51.2ms ± 2.1ms 811.3ms ± 5.7ms 15.9x 42.7 MB / 51.0 MB
investment 77.9ms ± 1.2ms 2954.1ms ± 45.0ms 37.9x 58.5 MB / 58.9 MB
fifo 74.9ms ± 1.5ms 14675.7ms ± 77.7ms 195.9x 54.5 MB / 58.9 MB

vs base, measured in this run

Shape base this PR Change RSS base → PR
simple 49.9ms ± 0.2ms 51.2ms ± 2.1ms +2.7% ✅ within noise 42.8 MB → 42.7 MB (-0.2%)
investment 77.7ms ± 1.3ms 77.9ms ± 1.2ms +0.3% ✅ within noise 58.9 MB → 58.5 MB (-0.6%)
fifo 74.6ms ± 1.2ms 74.9ms ± 1.5ms +0.5% ✅ within noise 54.8 MB → 54.5 MB (-0.5%)

A difference counts only when it clears the combined spread of the two measurements, per shape (2σ: simple 1.0ms, investment 0.8ms, fifo 0.8ms), and is at least 5%.
10,000 transactions per shape · 20 runs, 3 warmup · hyperfine

What do these numbers mean?
  • simple — Two-posting transfers, one currency. The historical workload — keep as the floor, not as the benchmark.
  • investment — Cost specs, per-unit prices and lot reductions. Exercises booking and inventory, which simple leaves at 0.3%.
  • fifo — Bare {} reductions against a FIFO account — how FIFO sells are normally written. The other four shapes reach the ordered-selection path twice in 2k transactions between them, so it was effectively unprofiled until it turned out to be quadratic (plan_ordered scans every lot per reduction: FIFO ledgers are quadratic (8.8s at 20k transactions) #2083).
  • vs base: the PR and the base-branch commit it is built on, built and measured in the same job
  • RSS: peak resident set size, a single sample — reported without a verdict because there is no spread to test it against
  • A shape with no beancount column is one bean-check reports errors on; it is timed for rledger only

@github-actions

github-actions Bot commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

✅ Compatibility Test Results

Metric Result
Check Compatibility 100%
BQL Compatibility 100% (raw 93%, 179 masked)
Full AST Match 100%
Total Files 826
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 and others added 3 commits November 7, 2026 07:30
…egates in FROM and WHERE

Review round 1 of #2887. The `*`-argument check ran only on SELECT, so
`BALANCES WHERE sum(*) > 0` and `JOURNAL "Assets" FROM sum(*) = 1` still
answered nothing with exit 0. The shorthand statements also skipped the
aggregate-placement check, so `BALANCES WHERE count(*) > 0` and
`PRINT FROM count(*) = 1` answered where bean-query 0.2.0 refuses with
`aggregates are not allowed in WHERE clause` / `... in FROM clause`.

`check_shorthand_filters` now runs both checks on their FROM filter and
WHERE clause, in bean-query's order (FROM, then WHERE; in each, the calls
before the aggregates), each checked against bean-query.

The operator test now builds its list and an exhaustive match from one
macro invocation, so a new `BinaryOperator` cannot be skipped.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… and aggregates

Review round 2 of #2887. The checks run before columns and functions are
resolved, in SELECT and in JOURNAL, BALANCES and PRINT alike, so a query
that also names an unknown column or function gets the `*` or aggregate
error first, where bean-query names the unknown name. Both refuse.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@robcohen
robcohen merged commit 9bed2e3 into main Nov 7, 2026
58 of 59 checks passed
@robcohen
robcohen deleted the fix/bql-star-args branch November 7, 2026 15:43
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.

BQL: sum(*) returns an empty result instead of a compile error

1 participant