Skip to content

fix(query): report BQL syntax errors where they are, read dot-edge decimals and stacked signs, and use bean-query's postings wildcard - #2865

Merged
robcohen merged 6 commits into
mainfrom
fix/bql-parser-errors-and-wildcard
Nov 5, 2026
Merged

robcohen merged 6 commits into
mainfrom
fix/bql-parser-errors-and-wildcard

Conversation

@robcohen

@robcohen robcohen commented Nov 5, 2026 •

Copy link
Copy Markdown
Member

What

Two BQL compatibility fixes reported against @rustledger/wasm 0.24.0, each checked row by row against beanquery 0.2.0 / beancount 3.2.3 (the rustledger-compat container).

#2830, parse errors, dot-edge decimals, stacked signs

  • A syntax error inside a statement is now reported where it is, naming the character. When every statement alternative failed, chumsky reported the last alternative's keyword error (PRINT) at position 0, which surfaced as unexpected token 'SELECT'. When the failure is at the first keyword, the parser now re-runs the one statement that keyword names (statement_errors) and reports that statement's error. A word that names no statement (FETCH x) is still reported at position 0.
  • A number literal may start or end with a dot (.5, 5., -.5), as in bean-query's grammar.
  • Unary minus stacks (--6, -- 6, ---6), and may be followed by a + (-+6). A + takes a bare atom only, as in bean-query's grammar (uplus: '+' atom), so ++6, +-6, +(1), +date.year are syntax errors on both sides, and so is any sign before * (+*, -*, --*), reported at the *. One gap: bean-query also accepts +(1, 2) and 'a' IN +('a', 'b') through its list-constant atom; rledger refuses every +( (see Review round 2).
  • A sign run counts toward the same MAX_NESTING_DEPTH as the brackets around it, in the up-front nesting_exceeds_limit scan. Signs and brackets therefore cannot multiply into a stack overflow (20 brackets each behind 128 signs is a parse error, not a crash).
  • A bracket or quote left open (SELECT (1, SELECT 'abc) is reported at the end as the missing closer (expected ')' to close the '(' at position 7). Before, the narrowed error named the opener as the offending character.

#2829, SELECT * over the postings

  • SELECT * gives bean-query's PostingsTable.wildcard_columns (date, flag, payee, narration, position) for the default FROM, FROM postings and FROM #postings alike, and through a subquery or CREATE TABLE AS. Before, the default form added account, and FROM #postings gave all 31 visible columns. Every column is still selectable by name. #postings uses a new Table::with_wildcard, which hides the non-wildcard columns from * only, and no longer computes balance / account_balance / meta just because the query says *.

Why

  • Every leading-character error read syntax error at position 0: unexpected token 'SELECT', which was wrong in both position and token.
  • A positional consumer of SELECT * saw a different shape from bean-query's.

bean-query comparison, per row

Query bean-query main this PR
SELECT `payee` FROM postings parse error position 0, unexpected token 'SELECT' position 7, found '`'
SELECT [payee] FROM postings (also @, $, ?) parse error position 0 position 7, found '['
SELECT .5 LIMIT 1 0.5 position-0 error 0.5
SELECT 5. LIMIT 1 5 error at 16 5
SELECT -.5 LIMIT 1 -0.5 position-0 error -0.5
SELECT -- 6 LIMIT 1 6 position-0 error 6
SELECT ---6 LIMIT 1 -6 position-0 error -6
SELECT -+6 LIMIT 1 -6 position-0 error -6
SELECT +-6, ++6, +(1) syntax error position-0 error syntax error at the sign
SELECT (1 syntax error position-0 error expected ')' at the end
SELECT 2 LIMIT 1; garbage 2 error at 18 error at 18, unchanged on purpose (see below)
SELECT * LIMIT 1 date, flag, payee, narration, position adds account the five
SELECT * FROM postings LIMIT 1 the same five 31 columns the five

Deliberate deviation, kept: a trailing ; text stays a syntax error (#2403). bean-query treats ; as the start of an end-of-line comment, so SELECT 1;2 silently runs SELECT 1. test_parse_still_refuses_text_after_a_semicolon pins this. The compat suite has no such query, so nothing goes in KNOWN_RUST_DIVERGENCES.

Not fixed here (pre-existing on main): an optional clause whose body fails is still reported at its keyword, e.g. SELECT 1 WHERE `x` gives unexpected token 'WHERE'. That is the same chumsky alternative-error pattern, one level down, and would need the same narrowing per clause.

The narrowed error lists only what the first failing rule expected (found '`' expected identifier, or '*'), not every alternative an expression could start with.

Only SELECT changes here. JOURNAL, BALANCES, PRINT and CREATE TABLE ... AS SELECT already reported the right position on main.

SELECT * FROM #postings no longer keeps a running inventory for a balance column its wildcard does not show (#1080). A SELECT * over a user table that has a balance column still does.

The CHANGELOG lists the SELECT * change under Breaking, because a positional consumer of the default wildcard loses the account column. The parse fix is listed under Fixed. The parser's output for already-valid queries is unchanged, so the parse cache is not bumped.

Review round 2

Merged origin/main (#2862, #2864, #2869); the CHANGELOG conflict kept both sides. This PR bumps no parse-cache version.

  1. A unary + no longer takes the * wildcard. The atom after + came from primary_expr, which includes *, so SELECT +*, count(+*) and WHERE +* parsed, the last giving no rows. bean-query 0.2.0 refuses all three with syntax error, as it does SELECT + * and SELECT -+*; main refused them too. The guard is now one_of("(-+*"). test_parse_refuses_unary_plus_before_the_wildcard pins all five and that count(*) still counts.
  2. +( before a list: documented gap, not implemented. Checked against bean-query 0.2.0: its atom includes a list constant, so SELECT +(1, 2) gives [1, 2], +(1,) gives [1], and 'a' IN +('a', 'b') gives TRUE; +(account, 'x') is a syntax error there, since list elements must be constants. Matching it is not just a parser change: rledger has no list constant outside IN at all (SELECT (1, 2) is refused, where bean-query gives [1, 2]), and rledger's set renders as 1, 2, not [1, 2]. Accepting only +(...) would add a form that the bare list still lacks. So every +( stays refused. The CHANGELOG now names the gap instead of claiming parity with bean-query's grammar, the parser comment says the same, and test_parse_refuses_unary_plus_before_a_list pins it (the unsigned IN ('a', 'b') still answers TRUE). Bare list constants would be their own change.
  3. The nesting comment says what a binary - costs. The scan cannot tell a binary - from a unary one, so a bracket right after any - costs two levels: a - (b - (... reaches the 128 bound at half the brackets a + (b + (... does. The comment says so, and test_parse_counts_a_bracket_after_a_binary_minus_twice pins it (100 + ( parse, 60 - ( parse, 70 - ( are refused).

Review round 3

Merged origin/main again, now with #2867; the CHANGELOG conflict kept both entries.

  1. No sign takes the * wildcard (regression fixed). Round 2 guarded only the + path; a run of - still took primary, which includes *, so SELECT --*, SELECT -- *, SELECT - -* and count(--*) parsed and failed on a row ("negation requires numeric value"), and SELECT date FROM year = 1999 WHERE --* silently gave no rows. Main refused --* but accepted a single -*. bean-query 0.2.0 refuses every one with syntax error (checked: -*, --*, - -*, count(--*), WHERE --*, the year = 1999 query). The unary parser now checks the operand after a + or any run of -, so -* is refused too.
  2. The error names the *. It was syntax error at position 7: found '+' expected identifier, or '*', at the sign, as if a * were what the target list wanted. It is now syntax error at position 8: '*' is not a value, so a sign cannot apply to it, at the * (a validate on the unary parser, which keeps the parse and reports there). test_parse_refuses_a_sign_before_the_wildcard (renamed from round 2's plus-only test) pins the exact position and message for 12 queries: +*, + *, count(+*), WHERE +*, -+*, -*, --*, -- *, - -*, count(--*), WHERE --* and the FROM year = 1999 WHERE --* query, plus count(*) and SELECT * still working.
  3. Depth pinned exactly. test_parse_counts_a_bracket_after_a_binary_minus_twice now asserts 128 + ( parse and 129 do not, 64 - ( parse and 65 do not.
  4. Not in this PR: * as an operand of a binary operator (1 + *, 2 * *, WHERE 1 + *) is also wrong on main and is left for a separate issue.

How to test

cargo test -p rustledger-query --test bql_literals_and_wildcard_test
cargo test -p rustledger-query -p rustledger --all-features

The new bql_literals_and_wildcard_test.rs has 10 tests. Run against an origin/main build (separate worktree and target dir, at c5e94c5), 8 of them fail. Two pass there: the ; pin, because that behavior is unchanged, and test_parse_refuses_unary_plus_before_a_list, because main also refuses every +( (it pins the documented gap). A main-vs-branch run of all 68 queries in docs/ on example.beancount differs on 4. Two are the wildcard (SELECT *, SELECT * WHERE ... IN links), and two are the bare-syntax fragments SELECT and SELECT ..., whose error messages changed. All four are intended.

Existing tests that asserted the old wildcard shape now name their columns (SELECT *, account ..., or explicit lists).

Closes #2829
Closes #2830

🤖 Generated with Claude Code

…cimals and stacked signs, and use bean-query's postings wildcard

A syntax error at a statement's first token was reported as the last
statement alternative's keyword error, position 0, "unexpected token
'SELECT'". The parser now re-runs the statement the keyword names and
reports its error where it is. Number literals may start or end with a
dot, and unary signs stack, bounded by MAX_NESTING_DEPTH. A trailing
"; text" stays an error (#2403).

SELECT * over the postings is bean-query's PostingsTable.wildcard_columns
(date, flag, payee, narration, position) for the default FROM and
FROM #postings alike.

Closes #2829
Closes #2830

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@robcohen
robcohen requested a review from a team as a code owner November 5, 2026 05:49
robcohen and others added 3 commits November 5, 2026 02:05
…s, and name an unclosed delimiter

Review follow-up. A + takes a bare atom only, as bean-query's
uplus: '+' atom, so ++6, +-6 and +(1) are syntax errors. A run of
minus signs counts toward the same nesting bound as the brackets around
it, so the two cannot multiply into a stack overflow. A bracket or quote
left open is reported at the end as the missing closer. SELECT * FROM
#postings no longer tracks a running balance its wildcard does not show.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…n cost

A unary + took primary_expr, which includes the * wildcard, so SELECT +*,
count(+*) and WHERE +* parsed (the last gave no rows). bean-query has no
* atom and refuses all three; so does main. The atom after + now excludes
* as it excludes a bracket or a sign.

bean-query also accepts +(1, 2) and IN +(...) through its list-constant
atom. rledger has no list constant outside IN (SELECT (1, 2) is refused
too), so every +( stays refused: the CHANGELOG now names that gap instead
of claiming parity, and a test pins it.

The nesting-scan comment said a binary minus costs one level at most. It
costs one for every bracket after a minus, so a - (b - (... reaches the
limit at half the depth. The comment says so, and a test pins it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@codecov

codecov Bot commented Nov 5, 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 5, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Shape rustledger beancount Speedup RSS (rledger / beancount)
simple 39.5ms ± 2.6ms 588.3ms ± 11.8ms 14.9x 34.8 MB / 51.0 MB
investment 61.3ms ± 1.9ms 2139.5ms ± 24.3ms 34.9x 44.5 MB / 59.0 MB
fifo 56.7ms ± 1.0ms 11651.9ms ± 25.8ms 205.4x 40.9 MB / 58.8 MB

vs base, measured in this run

Shape base this PR Change RSS base → PR
simple 38.4ms ± 0.9ms 39.5ms ± 2.6ms +2.9% ✅ within noise 35.0 MB → 34.8 MB (-0.6%)
investment 62.2ms ± 1.5ms 61.3ms ± 1.9ms -1.4% ✅ within noise 44.7 MB → 44.5 MB (-0.6%)
fifo 59.5ms ± 1.2ms 56.7ms ± 1.0ms -4.7% ✅ within noise 40.9 MB → 40.9 MB (-0.0%)

A difference counts only when it clears the combined spread of the two measurements, per shape (2σ: simple 1.2ms, investment 1.1ms, fifo 0.7ms), 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 5, 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 2 commits November 5, 2026 05:19
Only the + path excluded *, so a run of minus signs still took it:
SELECT --*, count(--*) and SELECT - -* parsed and failed on a row
("negation requires numeric value"), and WHERE --* over no rows gave
nothing. Main refused --* but accepted a single -*. bean-query refuses
all of them: * is no atom of its grammar.

The unary parser now checks the operand after any + or run of -, and a
wildcard there is reported at the * itself ("* is not a value, so a sign
cannot apply to it") instead of at the sign as "expected identifier, or
*", which suggested a * was accepted there.

The nesting test is pinned exactly: 128 "+ (" parse and 129 do not,
64 "- (" parse and 65 do not.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@robcohen
robcohen enabled auto-merge (squash) November 5, 2026 11:31
@robcohen
robcohen merged commit 5f57384 into main Nov 5, 2026
59 of 60 checks passed
@robcohen
robcohen deleted the fix/bql-parser-errors-and-wildcard branch November 5, 2026 11:34
robcohen added a commit that referenced this pull request Nov 6, 2026
…ument (#2878)

BQL read `*` as an ordinary operand, so `SELECT 1 + *` gave blank rows,
`count(1 + *)` gave 0 and `SELECT date WHERE 1 + *` gave no rows and no
error, where bean-query 0.2.0 refuses each as a syntax error. Its grammar
takes `*` only as the whole target list (`select: ... targets:(target+ |
asterisk)`) and as a call's only argument (`function: fname '('
asterisk ')'`).

- `target` reads a bare `*` before an expression, unless an operator,
  `.` or `[` follows it. rledger's `SELECT *, account` extension keeps
  working.
- `function_args` reads a `*` alone before `)`.
- Any other `*` reaches the `*` atom in `primary_expr`, which refuses it
  by name at the `*` ("'*' is not a value: ..."): `1 + *`, `2 * *`,
  `count(1 + *)`, `count(*, 1)`, `WHERE 1 + *`, `(*)`, `*.x`, `-(*)`,
  `-*.x`, `GROUP BY *`, `ORDER BY *`. This replaces the sign-only check
  from #2865, which caught only a bare signed `*`.
- That refusal is reported as written when the query also fails further
  in (``SELECT -*, `x` ``, `SELECT -* FROM`), instead of "unexpected token
  '*'".
- A sign after a unary `+` (`++6`, `+-6`, `+-*`) is refused at that sign
  by name, instead of "found '+' expected identifier, or '*'". The plus
  check now runs before the atom rather than as `atom.and_is(..)`, whose
  rewind dropped the `*` atom's error, so `SELECT +*` parsed.

`-1 * 2`, `2*3`, `count(*)`, `count( * )`, `sum(*)` (parsed, then
checked like any call, as in bean-query) and `SELECT * FROM (SELECT *)`
are unchanged.

Closes #2871

Co-authored-by: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant