Skip to content

feat(extract): choose between importer entries by the file's columns - #2527

Merged
robcohen merged 1 commit into
mainfrom
feat/extract-select-importer-by-header
Oct 9, 2026
Merged

robcohen merged 1 commit into
mainfrom
feat/extract-select-importer-by-header

Conversation

@robcohen

@robcohen robcohen commented Oct 8, 2026

Copy link
Copy Markdown
Member

What changed

Content-based selection between [[importers]] entries, option D as decided on #2295.

When several entries match a file by filename_pattern, extract reads that file's header and keeps only the entries whose named columns are all present. If one survives, it is used. Otherwise the existing Multiple importers match file … refusal stands. Its first line is unchanged, and it now adds one line per candidate saying what the header showed:

error: Multiple importers match file 'StarlingStatement_2023.csv': starling-gbp, starling-eur. Use --importer to select one.
  the file's header did not settle it:
    'starling-gbp': header has no amount_column "Amount (GBP)"
    'starling-eur': header has no amount_column "Amount (EUR)"

Rules

  • Only a column the entry names by header can rule it out. That means a *_column key set to a header name, with CLI column flags applied first, as they will be for the entry that runs.
  • Never ruled out, kept as a candidate with the reason in the message: an entry that names no columns (index-only, or only the defaults), a headerless one (skip_header = true), an OFX entry, one that runs preprocess (its columns describe the command's output, not this file), one whose config does not build, and any entry when the file cannot be read as text. So index-based, headerless, OFX and WASM cases still get the refusal.
  • A single filename match is unchanged: it is used without reading the header.
  • All three resolvers (dispatch, preprocess, the CSV branch) go through the one resolve_config_entry_named, and all check the source file. So the entry that preprocesses is still, by construction, the entry that extracts.

Canonical functions, not copies

  • CsvImporter::header(content, &CsvConfig) reads the header with the same reader extraction uses (its delimiter and has_header). The extraction path now calls the same helper. skip_rows skips data rows after the header in this importer, so it does not move the header, and the header check behaves the same way.
  • ImporterEntry::named_columns() reads names off the config build_config_from_entry builds, so a name and an index are told apart exactly as extraction tells them apart ("3" is an index in both).

Why

petemounce's Starling GBP and EUR exports share a filename (StarlingStatement_<from>_<to>.csv) and differ only in Amount (GBP) vs Amount (EUR). Two entries naming those columns can now be told apart without --importer or per-currency directories. This matches the reply drafted for the issue.

How tested

New tests in extract_cmd use petemounce's exact header shapes:

  • the_header_picks_between_entries_matching_one_filename: the GBP header picks starling-gbp, and the EUR header picks starling-eur.
  • a_header_selected_entry_imports_in_its_currency: end to end through run_with_writer, the rows come out in EUR and GBP respectively.
  • no_entry_fitting_the_header_lists_missing_columns_per_entry: a USD header gets the refusal, with each entry's missing column.
  • several_entries_fitting_the_header_are_still_refused.
  • entries_the_header_cannot_judge_are_kept: index-only, headerless, OFX and preprocess entries each keep the refusal and say why.
  • a_single_filename_match_is_not_checked_against_the_header.
  • Importer: named_columns_lists_only_columns_named_by_header and header_reads_the_row_extraction_reads.

Shown to fail without the fix: with header filtering disabled, 5 of the 6 CLI tests fail. The single-match test passes either way by design. Existing tests, including test_run_auto_select_errors_on_multiple_importers, pass unchanged. cargo test -p rustledger-importer -p rustledger --all-features, workspace clippy (-D warnings) and rustdoc (-D warnings) are clean.

Closes #2295

🤖 Generated with Claude Code

https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr

When several [[importers]] entries match a file by filename_pattern,
read the file's header (with each entry's own delimiter and header
setting, through the CSV importer's reader) and keep only the entries
whose named columns are all present. One survivor is used; otherwise the
existing "Multiple importers match" refusal stands, now listing per
entry which configured columns the header lacks. Entries the header
cannot judge (index or default columns only, headerless, OFX,
preprocess) are never ruled out. A single filename match is unchanged.

Closes #2295

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 October 8, 2026 18:11
@codecov

codecov Bot commented Oct 8, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 99.66887% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
crates/rustledger/src/cmd/extract_cmd/mod.rs 99.47% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@github-actions

github-actions Bot commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Shape rustledger beancount Speedup RSS (rledger / beancount)
simple 45.5ms ± 2.3ms 827.7ms ± 2.3ms 18.2x 51.0 MB / 50.2 MB
investment 68.4ms ± 0.4ms 3045.0ms ± 49.0ms 44.5x 55.1 MB / 57.8 MB
fifo 66.2ms ± 0.7ms 15448.7ms ± 26.2ms 233.3x 58.9 MB / 57.8 MB

vs base, measured in this run

Shape base this PR Change RSS base → PR
simple 44.4ms ± 1.6ms 45.5ms ± 2.3ms +2.5% ✅ within noise 51.0 MB → 51.0 MB (-0.1%)
investment 68.2ms ± 0.4ms 68.4ms ± 0.4ms +0.4% ✅ within noise 55.0 MB → 55.1 MB (+0.1%)
fifo 65.9ms ± 0.6ms 66.2ms ± 0.7ms +0.5% ✅ within noise 54.9 MB → 58.9 MB (+7.2%)

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

github-actions Bot commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

✅ Compatibility Test Results

Metric Result
Check Compatibility 100%
BQL Compatibility 100% (raw 93%, 179 masked)
Full AST Match 100%
Total Files 819
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 Oct 8, 2026 •

Copy link
Copy Markdown
Member Author

Review (adversarial pass)

  • The header is read from the source file in all three resolvers (dispatch, preprocess, the CSV branch), so the selection cannot differ between them.
  • A file that can't be read as UTF-8 marks every candidate "cannot be checked", so nothing is ruled out and the refusal stands.
  • A UTF-8 BOM on the header (\u{feff}Date) would make the CSV importer itself miss Date. The header check reads the same row with the same reader, so it agrees with the importer: the entry is ruled out, and the message names the column. This is pre-existing importer behavior; BOM handling is a possible follow-up.
  • Minor, pre-existing: the "Using importer …" line doesn't say the header made the choice. Not changed here.

Cross-PR integration (applies to #2524, #2526, #2527, #2531, #2532, #2533)

All six PRs were merged onto origin/main 25fc6be in two orders: 2524→2526→2527→2531→2532→2533, and the reverse.

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.

[Feature]: Allow an importer to assign currency based on file-pattern to support multiple-currency accounts

1 participant