Repository navigation
fix(query): refuse * as the argument of any function but count, as bean-query does - #2887
Merged
Merged
Conversation
…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]>
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Contributor
📊 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?
|
Contributor
✅ Compatibility Test Results
What's tested
Full results available in workflow artifacts |
…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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Behavior change (read first)
A query that passes
*to any function butcountnow errors where it answered.SELECT sum(*)used to return one blank row, exit 0. It now fails withno function matches "sum(*)", exit 1.WHERE sum(*) > 0used to match nothing silently. It now fails the same way.MIN(*) OVER ()used to reportinvalid arguments for function MIN: .... It now reportsno function matches "min(*)", the same error as the aggregatemin(*).What changed
executor/execution.rs): the newcheck_star_argumentsruns before any path executes a SELECT, ahead of the aggregate-placement check.*argument to any call butcount, plain or window.QueryError::UnknownFunction("sum(*)"). That is the same variant and wording the crate already uses for bean-query'sno function matches "<sig>" name and argument types, with the name lower-cased as bean-query prints it (SUM(*)givessum(*)).parser.rs):*: a window'sPARTITION BY *andORDER BY *are refused by name with'*' is not a value: ..., at the*.simple_argtook a bare*. bean-query has no window functions, so this is rledger's own surface.count(* 1): now readsexpected ')' after the '*': it stands alone in a call such as count(*), at the1. It used to readfound '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 sharedOPERATOR_STARTSconst. The operator definitions inexprcarry a cross-reference to it.test_star_before_every_operator_is_namedrunsSELECT * <op> 1for everyBinaryOperator, through an exhaustive match, so a new operator must add a row there. I checked that the test fails whenORis dropped fromoperator_follows.bean-query 0.2.0 behavior (rustledger-compat image)
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(*))sum(*)errorSELECT count(*),COUNT(*)SELECT count(* 1),count(*1),count(*) OVER (PARTITION BY *)ParseError: syntax errorOne known difference: bean-query names whichever error comes first in the query.
SELECT nosuchcol, sum(*)iscolumn "nosuchcol" not foundthere, andno function matches "sum(*)"here. Both refuse. This is noted in the CHANGELOG.Verification
crates/rustledger-query/tests/bql_literals_and_wildcard_test.rs:test_execute_refuses_a_wildcard_argument_but_countasserts the exact error string for 14 queries across every clause, a subquery and a window. It also checks that thecount(*)forms still answer.test_parse_refuses_the_wildcard_in_a_windowtest_parse_names_the_close_a_call_star_needstest_star_before_every_operator_is_namedwindow_min_max_test::arguments_are_checkednow expectsUnknownFunction("min(*)")forMIN(*) OVER ().rledger queryontests/fixtures/examples/example.beancount, with debug builds in separate worktrees and target dirs. The queries were extracted fromdocs/**/*.mdand from string literals incrates/*/testsandcrates/rustledger-query/src.cargo test -p rustledger-query --all-features: 998 pass, after merging origin/main.cargo clippy -p rustledger-query --all-features --all-targets -- -D warningsRUSTDOCFLAGS=-Dwarnings cargo doc --workspace --no-depscargo fmt --checkHow to test
Review round 1
The shorthand statements now run the check.
check_star_argumentsran only on SELECT.check_shorthand_filtersnow checks the FROM filter and WHERE clause ofJOURNAL,BALANCESandPRINT. Before:JOURNAL "Assets" FROM sum(*) = 1,BALANCES WHERE sum(*) > 0andBALANCES FROM sum(*) = 1gave 0 rows with exit 0.PRINT FROM sum(*) = 1gave 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(*) = 1andPRINT FROM count(*) = 1giveaggregates are not allowed in FROM clause.BALANCES WHERE count(*) > 0givesaggregates 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(*) > 0names the FROM aggregate, andBALANCES FROM sum(*) = 1 WHERE count(*) > 0namessum(*). 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_countgained the four JOURNAL/BALANCES/PRINT rows.test_execute_refuses_an_aggregate_in_a_shorthand_filtercovers the aggregate cases and the two ordering cases, and checks that the statements still answer without an aggregate.check_shorthand_filterscalls removed, both tests fail:"JOURNAL \"Assets\" FROM sum(*) = 1" answered [].Nit:
test_star_before_every_operator_is_namednow builds its operator list and an exhaustivematchfrom one macro invocation. A newBinaryOperatordoes 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
*and aggregate checks run before columns and functions are resolved, in SELECT (as on main) and now in JOURNAL, BALANCES and PRINT.BALANCES FROM nosuchcol = 1 WHERE count(*) > 0andBALANCES WHERE nosuchfn(date) = 1 AND count(*) > 0name the aggregate here, where bean-query names the unknown column or function.*and aggregate checks.5271042cd98. origin/main had not moved, so there was no merge.Closes #2880
🤖 Generated with Claude Code