Repository navigation
fix(query): head results the way bean-query does - #2170
Conversation
Three naming rules, each measured against bean-query 3.x rather than
inferred:
bare column -> the column's own name, lowercased: `SELECT DATE`
heads `date`
alias -> lowercased, including mixed case: `AS LatestDate`
heads `latestdate`
function -> the target's SOURCE TEXT, case intact: `sum(NUMBER)`
stays `sum(NUMBER)`
We echoed the query's spelling for the first two and emitted only the
bare function name for the third, so `SELECT count(date)` headed `count`
where bean-query heads `count(date)`. Anything reading our CSV by header
name -- a script written against bean-query -- missed the field silently.
The third rule is not what the issue described. #2164 was filed about
casing alone; measuring first turned up the dropped arguments, and that
aliases are lowercased too, which the issue had explicitly assumed must
be preserved.
Lowercasing exposed a second problem. ORDER BY and PIVOT BY resolved
against result headers by exact match, so `ORDER BY DATE` stopped
finding the now-lowercase `date` column -- a query bean-query accepts and
that worked before this change. Worse, `find_hidden_order_by_targets`
uses the alias field as an internal naming mechanism, naming a hidden
column by its expression's source text (`MIN(date)`); lowercasing that
alias broke `ORDER BY MIN(date)` against it.
Both are fixed by one `find_column` helper, exact match first and then
case-insensitive, shared by the five ORDER BY / PIVOT BY lookup sites.
Exact-first so an exact hit still wins when two columns differ only by
case. Its `column_indices` map is now dead and removed.
The CLI aggregate snapshot moves from `SUM` to `SUM(position)`, which is
what bean-query emits for that query; confirmed directly rather than
assumed.
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
There was a problem hiding this comment.
Pull request overview
Aligns rustledger BQL result column headers with bean-query 3.x semantics so downstream consumers (notably CSV header-based scripts) get consistent, compatible column names, while preserving query correctness for ORDER BY / PIVOT BY.
Changes:
- Normalize result headers so bare columns and aliases are lowercased, while function targets use the expression text (including arguments).
- Make
ORDER BY/PIVOT BYcolumn resolution prefer exact matches, then fall back to case-insensitive matching via a shared helper. - Update/extend integration and snapshot tests to match the new bean-query-compatible naming behavior.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| crates/rustledger/tests/snapshots/cli_snapshot_test__query_aggregate_output.snap | Updates CLI snapshot header to reflect function-target header (SUM(position)). |
| crates/rustledger-query/tests/bql_integration_test.rs | Updates expectations and adds regression tests for header naming + case-insensitive ORDER BY resolution. |
| crates/rustledger-query/src/executor/sort.rs | Introduces find_column helper and applies it across ORDER BY / PIVOT BY lookup paths; removes per-sort column index map allocation. |
| crates/rustledger-query/src/executor/mod.rs | Adjusts SELECT header generation: aliases and bare columns are lowercased; functions use expression text. |
| crates/rustledger-query/src/executor/execution.rs | Mirrors alias-lowercasing behavior in execution-path column naming. |
💡 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 |
Review catch on #2170. `find_column` fell back to `eq_ignore_ascii_case`, but the headers it searches were produced by Unicode `to_lowercase` in `expr_to_name` and the alias path. The two halves disagreed for any non-ASCII identifier: SELECT date AS DÄTE FROM #notes ORDER BY DÄTE heads the result `däte`, then failed to find it. bean-query rejects that query outright, so there is no external behavior at stake -- but accepting an alias, lowercasing it, and then not finding it is incoherent on its own terms. Confirmed to fail when the comparison is put back to ASCII-only. The exact-match fast path is unchanged, so the allocating comparison only runs when a query writes a name in a different case than the header.
Review note on #2170. The Function arm heads a result with the target's source text; the Window arm deliberately does not. `OVER` is a rustledger extension with no bean-query behavior to match, and `ROW_NUMBER() OVER (PARTITION BY account ORDER BY date)` as a header helps nobody. Verified unchanged from main rather than assumed.
Review note on #2170. bean-query names a literal or binary-expression target by printing the expression; we emit colN. The obvious fix -- expr.to_string(), as the Function arm now uses -- renders (number + 1) and "lit" where bean-query gives number + 1 and 'lit', because bean-query prints with minimal parentheses and ours parenthesizes every binary node. Display is also what names the synthetic hidden ORDER BY columns, so it cannot be changed without care. Tracked in #2171 rather than shipped half-matching.
What
bean-query and rustledger disagreed on how to name result columns. Three
rules, each measured against bean-query 3.x:
SELECT DATEdateDATESELECT DaTedateDaTeSELECT DATE AS DdDSELECT date AS LatestDatelatestdateLatestDateSELECT count(date)count(date)countSELECT SUM(number)SUM(number)SUMSELECT sum(NUMBER)sum(NUMBER)sumSo: identifiers (bare columns and aliases) are lowercased; a function target
is headed by its source text, case intact.
This matters for anything consuming our CSV by header name. A script written
against bean-query reads the
narrationfield, getsNARRATIONfrom us, andmisses it with no error.
The issue understated it twice
#2164 was filed about casing of bare columns. Measuring first showed:
preserved, and that was wrong
Lowercasing broke ORDER BY, and that took two fixes
ORDER BYandPIVOT BYresolve against result headers by exact match, soORDER BY DATEstopped finding the now-lowercasedatecolumn. bean-queryaccepts that query and it worked before this change — a real regression,
caught because it was the risk the issue itself flagged.
Then a subtler one.
find_hidden_order_by_targetsuses the alias field asan internal naming mechanism, appending a hidden column named by its
expression's source text (
MIN(date)). Blanket alias-lowercasing renamed itto
min(date)while the lookup still searched forMIN(date), soORDER BY MIN(date)on a hidden column failed. That is a genuinely brokeninternal contract, not an expectation change, and
test_pivot_by_with_order_by_on_hidden_column_workscaught it.Both fixed by one
find_columnhelper — exact match first, thencase-insensitive — shared by all five ORDER BY / PIVOT BY lookup sites.
Exact-first so an exact hit still wins if two columns differ only by case. The
column_indicesmap it replaces is now dead and removed, dropping a mapallocation per sort.
Test changes
Six failed. Five were expectation updates that now match bean-query
(
sum→sum(number),["account","Balance","Converted"]→["account","balance","converted"]), including one stale comment thatdocumented the old naming explicitly. The sixth was the real hidden-column
breakage above.
The CLI aggregate snapshot moves
SUM→SUM(position). Confirmed that iswhat bean-query emits for that exact query rather than assumed.
Verification
and each was confirmed to fail against its own unfixed code — reverted
separately, not together.
known 34, found 34, new 0);corpus_paritydivergences still exactly 20, matching
main(pre-existing corpus_parity diverges under workspace feature unification when the debug component wasm is present #1809).Scope, checked rather than assumed
Unchanged by this PR, verified against a
mainbuild:SELECT *headers — they come from the table's column list, notexpr_to_namemeta['k']headsmeta['k'], matching bean-queryROW_NUMBER() OVER (...)still headsROW_NUMBER,deliberately not the source text the
Functionarm now uses.OVERis arustledger extension with no bean-query behaviour to match, and the full
source text as a header would help nobody. Documented at the code so it does
not read as an oversight; alias it (
AS rn) for a different name.Two aliases differing only by case now collapse to one header
(
SELECT date AS A, account AS aheadsa,a), as does a column collidingwith an alias. Both match bean-query exactly, checked directly since this is a
collision the change introduces.
Downstream surfaces
Checked against a
mainbuild rather than reasoned about:JOURNAL,BALANCESandPRINTare unchanged — they carry fixedcolumn names and never reach
expr_to_name.-f jsonchanges, because the header is both thecolumnsentry andthe per-row object key:
A consumer doing
row.COUNTnow needsrow["COUNT(date)"]. bean-query hasno JSON format (
beancount|csv|textonly), so there is nothing to matchhere; keeping the key identical to the CSV header is the consistent choice,
but it is a visible wire change worth stating. The wire-format bindings
freshness check passes — column names are data, not schema.
The rustfava component test fails on snapshot drift (
'sum(day)'vs'sum'), which is this change landing, not a defect.Known remaining gap
Literal and binary-expression targets still head as
colNwhere bean-queryprints the expression (
number + 1,NOT (number > 0)). Not fixed herebecause
expr.to_string()— the mechanism this PR uses for functions —renders
(number + 1)and"lit", so it would still not match. bean-queryprints with minimal parentheses, measured; ours parenthesizes every binary
node, and
Displayis also what names the synthetic hidden ORDER BY columns,so it cannot simply be changed. Filed as #2171 with the measurements.
Shipping the partial version was considered and rejected: it buys no
compatibility while churning affected headers a second time.
Left alone
SELECT DATE AS D FROM #notes ORDER BY dfails onmainand still failshere — a different lookup path (subquery result columns) with the same
case-sensitivity shape. Pre-existing, verified against a clean
mainbuild,and out of scope for this change.
Closes #2164