Repository navigation
fix(query): ORDER BY an expression finds the column it names - #2215
Conversation
`SELECT number + 1 ORDER BY number + 1` failed with `ORDER BY expression not found in SELECT: (number + 1)`. The header and the lookup spelled the same expression two ways: `ast::header_name` gives `number + 1`, `Display` parenthesizes and gives `(number + 1)`, and `sort_results` looked up by `Display`. `find_column`'s case-insensitive fallback cannot bridge that -- the two spellings differ by punctuation, not case. Before #2175 the header was `col0`, so the lookup missed for a different reason; the query has never worked. Both sites move together, and that is not tidiness. `sort_results` looks the target up, and `find_hidden_order_by_targets` NAMES the synthetic column when the expression is not reachable in the projection. Changing one alone trades one miss for another: a hidden column named by `Display` is invisible to a lookup by `header_name`. Each half has a test only it satisfies -- reverting the lookup fails three of four, reverting the naming fails exactly the hidden-column one. The issue asked for the neighboring paths to be re-verified rather than trusted, so they were, against bean-query: ordinals (`ORDER BY 1`, `ORDER BY 2`), multi-key, DESC, aggregate targets, and an aliased target whose ORDER BY needs the hidden column all match row for row. The wildcard is unchanged -- `SELECT *` output is byte-identical to main. Numbers differ from bean-query only by its right-alignment padding in CSV (`, 10.00` against `,10.00`), which is pre-existing and independent of ordering. `order-by-expression` joins the BQL compat corpus, verified to be real coverage: it reports `match: True`, and `match: False` when the lookup is reverted. Closes #2177
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.
🟡 Changes recommended
sort_results still resolves Expr::Function ORDER BY targets via Display rather than header_name, which can keep ORDER BY-on-function-expressions broken and inconsistent with the new hidden-column naming.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
Fixes BQL ORDER BY <expression> resolution by aligning ORDER BY target lookup and hidden sort-column naming with the same renderer used for SELECT headers (ast::header_name), matching bean-query behavior and adding regression coverage.
Changes:
- Use
ast::header_namewhen resolving ORDER BY expression targets (and when naming synthetic hidden ORDER BY columns). - Add a dedicated rustledger-query regression test suite for ORDER BY-on-expression scenarios (selected expression, hidden sort key, ordinals, boolean comparisons).
- Extend the compatibility corpus with an
order-by-expressionquery to prevent regressions.
File summaries
| File | Description |
|---|---|
| tests/compatibility/bql-queries.toml | Adds a corpus query ensuring ORDER BY expression lookup matches header spelling. |
| crates/rustledger-query/tests/order_by_expression_test.rs | Introduces regression tests covering ORDER BY expression resolution, hidden-column behavior, and ordinals. |
| crates/rustledger-query/src/executor/sort.rs | Updates ORDER BY resolution to use header spelling (header_name) for expression targets. |
| crates/rustledger-query/src/executor/execution.rs | Ensures hidden ORDER BY columns are named using the same spelling that ORDER BY lookup expects. |
Review details
- Files reviewed: 4/4 changed files
- Comments generated: 2
- 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 |
|
Correcting my own claim on this PR, and reporting what checking it turned up. I said the paths the issue named were re-verified. PIVOT BY was not — the issue listed wildcard, PIVOT BY and ordinals, and I checked the first and third. Done now:
But checking it found a pre-existing bug, filed as #2216: Neither the parser nor That also explains a wrong turn earlier in this review: my first PIVOT check used the CLI, which writes No change to this PR — #2216 is independent and pre-existing. |
Review feedback, and correct. I changed the arm that handles binary expressions and left `Expr::Function` on `Display`, which parenthesizes its arguments the same way. So `abs(number + 1)` still looked for `abs((number + 1))` against an `abs(number + 1)` header. `abs(number)` resolves either way, which is how the gap hid: the argument has to be COMPOUND before the two spellings part company, and every function case I had reached for was a bare column. Both arms now name the target the way the header does. Two tests rather than one, so a regression tells you which case broke: the compound-argument form, and the bare-column form that worked before. Reverting only this arm fails exactly the first. Swept wider against bean-query while here -- `number * 2`, unary `-number`, `ORDER BY <expr> DESC`, a function as a secondary sort key, and an aliased target sorted by its alias all match row for row.
|
Both comments were right, and the first one was a real gap.
And the test gap you named was the reason I missed it. There are now two tests, not one, so a regression says which case broke: the compound-argument form and the bare-column form. Reverting only the function arm fails exactly the first, and leaves the other five passing. Swept wider against bean-query while in there — |
…n divergence (#2359) `order-by-expression` hits the same bean-query grouping gap already registered for these three fava-investor fixtures on a dozen other queries: they set `option "render_commas" "True"`, beancount consumes the option and we honor it, and beanquery never reads it. `SELECT number + 1` returns `-1399999` from bean-query against our `-1,399,999`, identical once the separators come out. The pairs went unpinned only because the query postdates the block, having been added in #2215 for #2177. These were three of the seven unmasked mismatches the suite has been reporting at every coverage level (#2358); the remaining four are two files of upstream's 1e-7 plugin drift and two uncharacterized scale differences, which stay unmasked. Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
Closes #2177.
The header and the lookup spelled the same expression two ways:
ast::header_namegivesnumber + 1,Displayparenthesizes and gives(number + 1), andsort_resultslooked up byDisplay.find_column's case-insensitive fallback cannot bridge that — the spellings differ by punctuation, not case. Before #2175 the header wascol0, so the lookup missed for a different reason; the query has never worked.Why both sites move together
sort_resultslooks the target up.find_hidden_order_by_targetsnames the synthetic column when the expression is not reachable in the projection. Changing one alone trades one miss for another — a hidden column named byDisplayis invisible to a lookup byheader_name.Each half has a test only it satisfies:
DisplayThe neighbouring paths, re-verified rather than trusted
The issue asked for this explicitly, since the hidden-column machinery needed repair twice in #2170. Measured against bean-query (beanquery 0.2.0), row for row:
ORDER BY 1,ORDER BY 2(ordinals)ORDER BY account, number(multi-key)ORDER BY number DESCGROUP BY account ORDER BY sum(number)SELECT account AS a ORDER BY number + 1(hidden column)SELECT number > 0 ORDER BY number > 0Wildcard unchanged:
SELECT * FROM #postings ORDER BY dateoutput is byte-identical tomain. (It differs from bean-query — 31 columns to its 5 — but that predates this PR and is untouched by it.)Numbers differ from bean-query only by its right-alignment padding in CSV (
, 10.00against,10.00), which is pre-existing and independent of ordering.Corpus
order-by-expressionjoinstests/compatibility/bql-queries.toml, verified to be real coverage rather than decoration: run through the harness it reportsmatch: True, andmatch: Falsewhen the lookup is reverted.Note for #2176 / #2180
Sorting needed no ordering work —
compare_values_for_sortalready handles booleans and is total. This was purely the name lookup.🤖 Generated with Claude Code
https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr