Repository navigation
fix(query): PIVOT BY applies when the query names a table - #2218
Conversation
`PIVOT BY` was silently dropped by any query with a `FROM` clause. It
parsed, it validated, and then the un-pivoted result came back with no
error. Since `rledger query` is normally written with an explicit table,
that was the form users hit -- the CLI could not pivot at all.
Four execution paths run ORDER BY and LIMIT; only one ran PIVOT:
execute_select (no FROM) sort PIVOT limit
execute_select_from_table sort -- limit
execute_aggregate_from_table sort -- limit
execute_select_from_subquery sort -- limit
Neither the parser nor `apply_pivot` was at fault: the parser populates
`pivot_by` for the FROM form, and `apply_pivot` produces the right shape
when it is reached. The step was simply absent from three tails.
Two of the three now have it, in the same slot `execute_select` uses --
after ORDER BY and the hidden-column strip, before LIMIT. Both are
reachable and each improves something different:
* `execute_aggregate_from_table` is the reported case; the pivot now
applies and the rows match bean-query.
* `execute_select_from_table` cannot pivot, since PIVOT BY requires
GROUP BY -- but it now SAYS so, with the same message the no-FROM
path gives, instead of ignoring the clause. bean-query answers that
input with a Python traceback, so an error beats both.
The subquery path is deliberately left alone. Our parser rejects
`FROM (SELECT ...)` outright, so nothing can reach it carrying a pivot,
and a branch no test can exercise is worse than a documented gap -- the
table above is in the test file for whoever adds the next pipeline step.
Each insertion has a test only it satisfies: removing the table-path one
fails the without-GROUP-BY test, removing the aggregate-path one fails
the other two.
Headers still differ from bean-query -- `account` against `account/year`,
and value columns reversed. That is how the output is labeled on a
result that is otherwise correct, it changes the currently-working
no-FROM path too, and it is filed as #2217 rather than folded in here.
Closes #2216
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
There was a problem hiding this comment.
🟢 Approval recommended
The changes address a confirmed silent-wrong-output bug and add targeted regression tests to prevent recurrence with minimal behavioral risk.
Pull request overview
Fixes a correctness gap in rustledger-query’s BQL execution pipeline where PIVOT BY was parsed/validated but not applied for queries that specify a table via FROM, restoring pivot functionality for the typical rledger query ... FROM #postings CLI usage.
Changes:
- Apply
PIVOT BYin theFROM <table>aggregate execution path so pivoted results are produced instead of silently returning the un-pivoted shape. - Apply
PIVOT BYin the non-aggregateFROM <table>path soPIVOT BYwithoutGROUP BYerrors (matching the existing no-FROMbehavior) instead of being ignored. - Add focused regression tests that (a) reproduce the reported case, (b) prevent drift between
FROMand no-FROMexecution shapes, and (c) pin the diagnostic for missingGROUP BY.
File summaries
| File | Description |
|---|---|
| crates/rustledger-query/src/executor/execution.rs | Adds the missing apply_pivot step to the relevant FROM-clause execution tails in the same pipeline position used by the no-FROM path. |
| crates/rustledger-query/tests/pivot_with_from_test.rs | Adds regression tests ensuring pivot shape parity between FROM and no-FROM, and ensuring PIVOT BY without GROUP BY is rejected for FROM queries. |
Review details
- Files reviewed: 2/2 changed files
- Comments generated: 1
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
📊 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 |
… one
Review feedback, and correct. I wrote one comment and pasted it into two
places, so `execute_aggregate_from_table` claimed the pivot runs "after
ORDER BY and the hidden-column strip". That path has no strip -- zero
mentions of `num_hidden` -- because `find_hidden_order_by_targets`
materializes nothing for an aggregate query: an ORDER BY target there
must already be in GROUP BY or be an aggregate, so it is projected
already.
Each comment now describes the path it is in, and each states the thing
a reader of that path needs:
* the aggregate path says why there is no strip to sit after;
* the table path says that reaching `apply_pivot` from there ALWAYS
ends in `PivotWithoutGroupBy`, since the function returns early to
the aggregate path whenever `group_by.is_some()`. Verified against
that routing condition rather than assumed. Calling `apply_pivot`
anyway, rather than raising the error inline, keeps one source of
truth for the validation rules and their wording.
|
Correct, and fixed in the follow-up commit. I wrote one comment and pasted it into both places, so Each comment now describes the path it is in, and says the thing a reader of that path needs:
On the second point: calling |
Found probing the newly reachable path with inputs I had not tried. Two
ways of emptying the same query disagreed about its shape:
WHERE <no match> ... PIVOT BY -> account, year, sum(number)
HAVING <no match> ... PIVOT BY -> account
bean-query reports the pivoted shape for both.
The aggregate path returned early when GROUP BY produced no groups,
skipping ORDER BY, PIVOT and LIMIT. Two of those are no-ops on zero
rows, which is presumably why the shortcut looked safe. PIVOT is not: it
reshapes the COLUMNS, so the early return was the difference between the
two spellings above.
The tail is now a named function both exits go through, which is a
stronger guarantee than remembering to duplicate three steps. Its doc
records why the empty case cannot skip it, and the no-strip note moved
there with it.
Verified the change does not simply drop columns from every empty
result: the same query WITHOUT a pivot still reports all three, and that
is asserted alongside. Restoring the early return fails the new test and
leaves the other three passing.
`execute_aggregate_from_table` states in a comment that a pure aggregate over zero matching rows still yields one row -- COUNT(*) is 0, SUM is NULL -- while a GROUP BY over zero rows yields none. Nothing pinned it. Every other `count(*)` test runs on data that matches. That mattered here: the branch is one arm of the if/else whose other arm this PR rewrote, and the only thing that caught it still holding was a hand comparison against `main`. The first version of this test was worthless. It passed against BOTH mutations of that branch -- removing the one-row rule, and extending it to GROUP BY -- because the queries had no `FROM` clause and so ran through `execute_select` instead. A test for a code path the query never reaches. With `FROM #postings` it fails against each mutation, and the reason is recorded in the test so the next person does not drop the clause as noise. Also records that the one-row rule is a deliberate divergence: bean-query returns NO row for `SELECT count(*)` over an empty filter.
Closes #2216.
PIVOT BYwas silently dropped by any query with aFROMclause. It parsed, it validated, and the un-pivoted result came back with no error. Sincerledger queryis normally written with an explicit table, the CLI could not pivot at all:Four tails, one pivot
execute_select(no FROM)execute_select_from_tableexecute_aggregate_from_tableexecute_select_from_subqueryNeither the parser nor
apply_pivotwas at fault — the parser populatespivot_byfor theFROMform, andapply_pivotproduces the right shape when reached. The step was simply absent from three tails.Two fixed, one deliberately not
Both additions sit in the slot
execute_selectuses (after ORDER BY and the hidden-column strip, before LIMIT), and each is reachable and improves something different:execute_aggregate_from_table— the reported case. The pivot applies and the rows match bean-query.execute_select_from_table— cannot pivot, sincePIVOT BYrequiresGROUP BY. But it now says so, with the same message the no-FROMpath gives, instead of ignoring the clause. bean-query answers that input with a Python traceback, so an error beats both.The subquery path is left alone. Our parser rejects
FROM (SELECT ...)outright, so nothing can reach it carrying a pivot. A branch no test can exercise is worse than a documented gap — the table above lives in the test file for whoever adds the next pipeline step.Controls
execute_select_from_tableinsertionGROUP BYtest failsexecute_aggregate_from_tableinsertionEach insertion has a test only it satisfies. A third test pins that the
FROMand no-FROMforms produce the same shape — the bug was precisely that they had drifted.Left for #2217
Headers still differ:
accountagainst bean-query'saccount/year, and value columns reversed (sum(number) / 2024against2024/sum(number)). That is labeling on an otherwise-correct result, it changes the currently-working no-FROMpath too, and it deserves its own review.🤖 Generated with Claude Code
https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr