Skip to content

fix(query): a table name can end a subquery or a statement - #2437

Merged
robcohen merged 3 commits into
mainfrom
fix/table-name-before-paren-2435
Sep 27, 2026
Merged

robcohen merged 3 commits into
mainfrom
fix/table-name-before-paren-2435

Conversation

@robcohen

@robcohen robcohen commented Sep 26, 2026 •

Copy link
Copy Markdown
Member

What

A table name can now end a subquery or a statement.

Query Before After
SELECT account FROM (SELECT account FROM #postings) syntax error at the outer ( parses
SELECT count(*) FROM (SELECT * FROM #accounts) syntax error parses
SELECT x FROM (SELECT x FROM MyTable) syntax error parses
SELECT account FROM #postings; syntax error at # parses
SELECT account FROM #postings) error still an error

Why

After a table name, the table_from branch in parser.rs required a lookahead of WHERE, GROUP, ORDER, HAVING, LIMIT, PIVOT or end(). A ) closing a subquery or a ; ending the statement failed that lookahead. The parser then backtracked out of the table branch and reported a syntax error at the outer ( or at the #. ) and ; are now in the lookahead. The subquery and statement parsers already consume them.

A bare FROM name that is no table is the FROM filter

Fixing the lookahead changed a second behavior, so this PR fixes that too. When the table branch rejected a name, the parser fell back to the FROM filter, so FROM payee; and a subquery's FROM payee) had been filters by accident. beanquery reads a bare name that no table has as the FROM filter in every spelling: SELECT count(*) FROM payee gives 2846 on example.beancount. Plain FROM payee on main reported table 'payee' does not exist. Now execute_select_from_table runs such a name as the FROM filter, and reports a missing table only if the name is no column either. Whether it is a column is decided up front (is_from_filter_column, which evaluates it on a sample transaction), so a typo is reported even on a ledger with no transactions. # names and existing tables are unchanged.

Tests

  • a_table_name_ends_a_subquery_or_statement (parser unit test) covers:
    • system and user tables ending a subquery, with and without whitespace before the );
    • a nested subquery;
    • a trailing ;, with and without whitespace;
    • a stray ) staying an error.
  • It fails with the two lookahead entries removed.
  • The BQL: weight(position) and cost(position) over a subquery answer from the Position value, not the posting #2432 tests (subquery_posting_values.rs) drop their eight WHERE true workarounds. That runs the plain form end to end through the executor.
  • from_name_is_table_or_filter.rs: a bare column name is the filter in four spellings; a name that is neither a table nor a column is a missing table; a table of that name wins; another unknown column keeps its own error; a missing table is reported on a ledger without transactions.

Checks

  • cargo clippy --workspace --all-features --all-targets -D warnings: clean.
  • Workspace tests: 5141 passed, 0 failed.
  • cargo mutants --in-diff: 7 caught, 3 unviable, 0 missed. The two lookahead entries themselves yield no mutants; the self-check covers them.

Closes #2435

🤖 Generated with Claude Code

https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr

After a table name the FROM parser required WHERE, GROUP, ORDER,
HAVING, LIMIT, PIVOT or the end of input, so `FROM (SELECT account
FROM #postings)` and `SELECT account FROM #postings;` failed the
lookahead and backtracked to a syntax error at the outer `(` or the
`#`. A `)` or `;` now ends the clause there too.

The #2432 tests drop their `WHERE true` workaround.

Closes #2435

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
@robcohen
robcohen requested a review from a team as a code owner September 26, 2026 20:23
@codecov

codecov Bot commented Sep 26, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.73684% with 4 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
crates/rustledger-query/src/parser.rs 94.11% 3 Missing ⚠️
crates/rustledger-query/src/executor/execution.rs 96.00% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@github-actions

github-actions Bot commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

🔴 Slower than base on: simple

Shape rustledger beancount Speedup RSS (rledger / beancount)
simple 47.8ms ± 4.8ms 846.1ms ± 4.0ms 17.7x 51.0 MB / 50.1 MB
investment 71.7ms ± 0.5ms 3099.6ms ± 51.0ms 43.2x 55.3 MB / 57.9 MB
fifo 68.9ms ± 0.6ms 16021.9ms ± 154.0ms 232.4x 58.9 MB / 57.9 MB

vs base, measured in this run

Shape base this PR Change RSS base → PR
simple 44.5ms ± 0.4ms 47.8ms ± 4.8ms +7.4% 🟡 slower 51.0 MB → 51.0 MB (+0.1%)
investment 71.6ms ± 0.6ms 71.7ms ± 0.5ms +0.1% ✅ within noise 55.3 MB → 55.3 MB (-0.0%)
fifo 68.6ms ± 0.6ms 68.9ms ± 0.6ms +0.5% ✅ within noise 55.4 MB → 58.9 MB (+6.3%)

A difference counts only when it clears the combined spread of the two measurements, per shape (2σ: simple 2.1ms, investment 0.4ms, fifo 0.4ms), 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

Copy link
Copy Markdown
Contributor

✅ Compatibility Test Results

Metric Result
Check Compatibility 100%
BQL Compatibility 100% (raw 93%, 178 masked)
Full AST Match 100%
Total Files 813
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

Letting a table name end a subquery or statement changed what
`FROM payee;` and a subquery's `FROM payee)` meant: the parser had
read those as filter expressions, only because it could not parse a
table there, and now read them as a missing table. beanquery reads a
bare name that no table has as the FROM filter in every spelling
(`SELECT count(*) FROM payee` counts postings with a payee), where
rledger's plain `FROM payee` already reported a missing table.

The parser cannot tell a table from a column, so the executor decides:
a name no table has is run as the FROM filter, and reported as a
missing table only if it is no column either.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
@robcohen

robcohen commented Sep 26, 2026 •

Copy link
Copy Markdown
Member Author

Deep review

Fixed in 1e09c66: the fix changed what a bare FROM name means

When the table branch rejected a name, the parser fell back to the FROM-filter branch. So FROM payee; and a subquery's FROM payee) had parsed as filter expressions. That wasn't by design: it happened only because no table name could be parsed there. After 406ead4 they parsed as tables and failed.

What beanquery does, checked with the oracle on example.beancount:

Query beanquery main 406ead4 1e09c66
SELECT count(*) FROM payee 2846 table 'payee' does not exist same error 2846
SELECT count(*) FROM payee; 2846 2846 error 2846
SELECT count(*) FROM (SELECT account FROM payee) 2846 2846 error 2846
SELECT count(*) FROM flag / FROM year 3549 error error 3549
SELECT count(*) FROM (SELECT account FROM #postings) 3549 syntax error 3549 3549

beanquery reads a bare name that no table has as the FROM filter, in every spelling. Main was inconsistent: without ; the name was a table, with ; or ) it was a filter.

Fix. The parser can't tell a table from a column, so the executor decides. In execute_select_from_table, a non-# name that neither a user nor a built-in table has is re-run as the default FROM with that name as its filter. If the column doesn't exist either, the error is still table '<name>' does not exist. # names and existing tables are unchanged: transactions is still the table.

Tests. crates/rustledger-query/tests/from_name_is_table_or_filter.rs:

  • FROM payee gives the same count in four spellings;
  • a name that is neither a table nor a column is still a missing table;
  • a table of that name wins, and # names are only ever tables;
  • another unknown column keeps its own error. The mutation run found this gap: with the guard forced to true, SELECT nosuchcol FROM payee reported "table 'payee' does not exist".

Disabling the fallback fails a test.

Also improved

The error for a stray ) now points at the ) (position 29), not at the # (position 20).

Verification (1e09c66)

  • cargo clippy --workspace --all-features --all-targets -D warnings: clean.
  • Workspace tests: 5139 passed, 0 failed.
  • cargo mutants --in-diff: 6 caught, 2 unviable, 0 missed.

Second pass (c6a9799): a typo was silent on a ledger without transactions

1e09c66 mapped the FROM filter's unknown-column error back to a missing table. That error only fires once a transaction is evaluated. On a ledger with none, a typo returned an empty result:

On a ledger with only an open main beanquery 1e09c66 c6a9799
SELECT count(*) FROM nosuchthing table does not exist CompilationError: column "nosuchthing" not found 0 table does not exist
SELECT account FROM (SELECT account FROM nosuchthing) table does not exist compile error no rows table does not exist
SELECT account FROM payee table does not exist no rows no rows no rows

Fix. is_from_filter_column decides before anything runs, by evaluating the name as a FROM filter on a one-posting sample transaction. It uses the filter's own resolver, so it can't drift from what the filter accepts. The after-the-fact error mapping is gone. The test a_missing_table_is_reported_on_a_ledger_without_transactions fails with the check forced to "yes".

Swept against beanquery, SELECT count(*) FROM <name> on example.beancount:

  • Agree: payee, narration, date, year, flag, tags, account, position, meta, type, description.
  • links: beanquery's empty-aggregate quirk (beanquery#1055), already adjudicated.
  • Disagree: number (3549 vs 3513), cost_number (421 vs 238), id (0 vs 3549). Main gives the same numbers for FROM number; and FROM number = number, so this is an existing gap in how a FROM filter evaluates posting-level columns, not something this PR adds. It is worth its own issue.

Verification (c6a9799).

  • cargo clippy --workspace --all-features --all-targets -D warnings: clean.
  • Workspace tests: 5141 passed, 0 failed.
  • cargo mutants --in-diff: 7 caught, 3 unviable, 0 missed.

…any ledger

The previous commit ran a bare FROM name that no table has as the FROM
filter, and mapped the filter's unknown-column error back to a missing
table. That error only fires once a transaction is evaluated, so on a
ledger with none a typo (`FROM nosuchthing`) returned an empty result
where main and beanquery's compiler report it.

Whether the name is a column is now decided before anything runs, by
evaluating it as a FROM filter on a one-posting sample transaction, so
the check cannot drift from what the filter resolves.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
@robcohen
robcohen merged commit 444c18e into main Sep 27, 2026
59 of 60 checks passed
@robcohen
robcohen deleted the fix/table-name-before-paren-2435 branch September 27, 2026 12:19
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: a table name followed by ')' or ';' fails to parse, so FROM (SELECT ... FROM #postings) is a syntax error

1 participant