Skip to content

fix(query): a string-literal header echoes the quote the query wrote - #2229

Merged
robcohen merged 1 commit into
mainfrom
fix/literal-quote-style
Sep 2, 2026
Merged

robcohen merged 1 commit into
mainfrom
fix/literal-quote-style

Conversation

@robcohen

@robcohen robcohen commented Sep 2, 2026

Copy link
Copy Markdown
Member

SELECT "dq" headed 'dq'. bean-query names such a column by reproducing the literal as typed, quote character included. Closes #2176.

Literal::String did not record which quote the source used, so the header had to guess — single quotes unless the value contained one. It now carries a QuotedString, and the header echoes what was written. Verified against beanquery 0.2.0:

query bean-query before after
SELECT 'plain' 'plain' 'plain' 'plain'
SELECT "dq" "dq" 'dq' "dq"
SELECT "o'clock" "o'clock" "o'clock" "o'clock"
SELECT 'it''s' 'it''s' "it''s" 'it''s'
SELECT 'say "hi"' 'say "hi"' 'say "hi"' 'say "hi"'
SELECT length('abc') length('abc') length('abc') length('abc')

The body is reproduced verbatim, as bean-query does: 'it''s' heads 'it''s', the doubled quote left as written rather than re-escaped or collapsed. That is consistent with the value semantics already pinned in string_literal_escapes_test.rs, where '' stays in the value.

Provenance is not part of the value

QuotedString implements PartialEq and Hash on the text alone. 'x' and "x" are the same string, and AST equality must not depend on typing style — same reasoning as Spanned, which keeps equality on its value rather than its span. The test pins the Hash/Eq agreement too, since a HashMap keyed on literals would split otherwise.

quote is None for a literal built in code rather than parsed, which has no style to echo. Those keep the #2175 fallback (single quotes, switching to double when the value contains a single quote and no double), so that fix is untouched and still covered.

Display echoes the source quote as well, which makes a parsed literal round-trip as written — the issue notes this may matter more than the header does — and still falls back to double quotes when there is no style.

Parser

string_literal now delegates to the new quoted_string_literal rather than restating the grammar. Writing the style-carrying version alongside the old one would have duplicated both quote arms, and a fix applied to one and not the other would make a literal parse differently depending on which caller reached it. Two callers want only the text and stay on the plain wrapper.

Verification

Header and Display are separately controlled: making either ignore the recorded style fails tests (3 integration + 1 unit for the header, 1 for Display), and the equality test fails if PartialEq starts comparing the style. The fallback rule has its own unit test, placed there because header_name is pub(crate) and a synthetic literal cannot be reached through parse — everything parsed carries a style.

4703 workspace tests green, clippy clean on 1.97.0, typos clean. BQL compat harness over 71 files / 1349 runs: no regression against baseline. The corpus does not exercise string-literal targets, so the oracle comparisons above are what carries this.

`SELECT "dq"` headed `'dq'`. bean-query names such a column by
reproducing the literal as typed, quote character included. Closes #2176.

`Literal::String` did not record which quote the source used, so the
header had to guess: single quotes unless the value contained one. It
now carries a `QuotedString`, and the header echoes what was written.
Verified against beanquery 0.2.0 -- `'plain'`, `"dq"`, `"o'clock"`,
`'it''s'`, `'say "hi"'`, and the same inside a call like
`length('abc')`, all agree.

The body is reproduced VERBATIM, as bean-query does: `'it''s'` heads
`'it''s'`, the doubled quote left as written rather than re-escaped.
That matches the value semantics already pinned in
`string_literal_escapes_test.rs`, where `''` stays in the value.

Quote style is source provenance, not part of the value, so
`QuotedString` implements `PartialEq` and `Hash` on the text alone --
`'x'` and `"x"` are the same string, and AST equality must not depend on
typing style. Same reasoning as `Spanned`, which keeps equality on its
value rather than its span. Pinned, including the Hash/Eq agreement a
`HashMap` keyed on literals would need.

`quote` is `None` for a literal built in code rather than parsed, which
has no style to echo; those keep the #2175 fallback, so that fix is
untouched. `Display` echoes the source quote too, which makes a parsed
literal round-trip as written -- the issue notes this may matter more
than the header does -- and still falls back to double quotes.

`string_literal` now delegates to the new `quoted_string_literal`
instead of restating the grammar. The two arms were about to be
duplicated, and a fix applied to one and not the other would make a
literal parse differently depending on which caller reached it.

The header and `Display` paths are controlled separately: making either
ignore the recorded style fails tests, and the equality test fails if
`PartialEq` starts comparing it.
@robcohen
robcohen requested a review from a team as a code owner September 2, 2026 01:34
Copilot AI lite review requested due to automatic review settings September 2, 2026 01:34

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The functional changes are well-scoped and backed by targeted tests, with only a minor formatting/convention nit noted.

Pull request overview

Fixes BQL column header rendering so string-literal targets preserve the original quote character used in the query (matching bean-query behavior), by carrying quote provenance through the AST and using it when rendering headers and Display.

Changes:

  • Introduces QuotedString + QuoteStyle and changes Literal::String to store quote provenance without affecting Eq/Hash semantics.
  • Updates the parser to produce style-carrying string literals (and centralizes string-literal grammar via quoted_string_literal).
  • Updates executor evaluation/parity harness to use the literal’s underlying string value, and adds regression tests covering headers, Display round-tripping, and Eq/Hash behavior.
File summaries
File Description
crates/rustledger-query/src/ast.rs Adds QuoteStyle/QuotedString, switches Literal::String payload, and updates header + Display rendering to echo parsed quote style with a fallback for synthetic literals.
crates/rustledger-query/src/parser.rs Parses string literals into QuotedString at the literal-expression site and reuses that grammar for string_literal() callers that only need the text.
crates/rustledger-query/src/executor/evaluation.rs Adjusts evaluation to extract the underlying string value from QuotedString.
crates/rustledger-query/src/executor/dual_eval_parity.rs Updates the parity harness to construct synthetic string literals using QuotedString.
crates/rustledger-query/tests/literal_quote_style_test.rs Adds regression coverage for header quoting, verbatim body reproduction, nested literal rendering, and Eq/Hash invariants for quote provenance.
Review details
  • Files reviewed: 5/5 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.

Comment thread crates/rustledger-query/src/parser.rs Resolved
@codecov

codecov Bot commented Sep 2, 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 Sep 2, 2026

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Shape rustledger beancount Speedup RSS (rledger / beancount)
simple 30.8ms ± 2.8ms 380.7ms ± 5.4ms 12.4x 55.1 MB / 50.1 MB
investment 43.3ms ± 2.3ms 1465.6ms ± 24.5ms 33.8x 59.2 MB / 58.0 MB
fifo 41.5ms ± 2.0ms 7862.6ms ± 122.3ms 189.4x 61.0 MB / 57.8 MB

vs base, measured in this run

Shape base this PR Change RSS base → PR
simple 31.3ms ± 3.2ms 30.8ms ± 2.8ms -1.5% ✅ within noise 55.1 MB → 55.1 MB (-0.0%)
investment 43.7ms ± 2.4ms 43.3ms ± 2.3ms -1.0% ✅ within noise 59.1 MB → 59.2 MB (+0.1%)
fifo 43.3ms ± 3.0ms 41.5ms ± 2.0ms -4.0% ✅ within noise 63.0 MB → 61.0 MB (-3.2%)

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

Copy link
Copy Markdown
Contributor

✅ Compatibility Test Results

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

robcohen commented Sep 2, 2026

Copy link
Copy Markdown
Member Author

Checked this rather than adjusting it, and the formatting is rustfmt's own output — cargo fmt --check -p rustledger-query passes on the branch as committed.

I tried the suggested one-line signature to be sure:

$ # signature rewritten onto one line
$ cargo fmt --check -p rustledger-query ; echo $?
1
$ cargo fmt -p rustledger-query          # rustfmt puts it straight back
fn quoted_string_literal<'a>()
-> impl Parser<'a, ParserInput<'a>, QuotedString, ParserExtra<'a>> + Clone {

So the leading -> is required here, not a missed cargo fmt. It differs from the neighbouring parsers because this one takes no parameters: those signatures break after their argument list and so end ) -> impl Parser<...>, while a zero-argument signature too long for one line can only break before the ->. parse_expr_with at line 890 and its neighbours are the parameterised shape.

Leaving it as rustfmt writes it — changing it would fail the formatting gate in CI. Commits on this repo run rustfmt through the prek pre-commit hook, which is what produced this form in the first place (it rejected my first commit attempt and reformatted the hunk).

@robcohen
robcohen merged commit 66cdeaf into main Sep 2, 2026
58 of 61 checks passed
@robcohen
robcohen deleted the fix/literal-quote-style branch September 2, 2026 02:01
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: string-literal headers do not preserve the query's quote style

2 participants