#1968·xberg

bug: DBF: row values come out in a random column order

Author: ihibtiCreated Sep 29, 2026Updated Sep 29, 2026

Description

Every dBASE table with more than one field is extracted with each row's values in a random order, so they land under the wrong column headers. The order differs from row to row and from one run to the next, so the same file does not extract the same way twice. The field types in metadata.format.fields are taken from the first row in that same order, so they can be paired with the wrong field names.

parse_dbf_records (crates/xberg/src/extractors/dbf.rs) takes the header from reader.fields() in declaration order, but reads each row with record.into_iter(). In dbase 0.8.0, Record wraps a HashMap<String, FieldValue> created with HashMap::new() per record, so every record gets its own hash keys and its own iteration order. Upstream treats that order as unspecified and points callers to reader.fields() (tmontaigu/dbase-rs#80). Two fields that share a name also collapse into one map entry, so such rows come out one cell short of the header.

The benchmark ground truth already records the scrambled output: the first data row of test_documents/ground_truth/dbf/dbf_stations.txt is | blue | rail-metro | Van Dorn Street | #0000ff | under | name | marker-col | marker-sym | line |, while the file stores Van Dorn Street, #0000ff, rail-metro, blue. The benchmark's bag-of-words F1 ignores order and every DBF unit test uses a single field, so nothing caught it.

Steps to reproduce

Fetch the fixture with python3 scripts/fetch_corpus.py --include 'dbf/**' in test_documents and copy dbf/stations.dbf next to this repro.py:

python
import asyncio
import hashlib
import sys

from xberg import ExtractInput, ExtractionConfig, extract


async def main(path: str) -> None:
    print("sha256", hashlib.sha256(open(path, "rb").read()).hexdigest())
    output = await extract(ExtractInput(kind="uri", uri=path), ExtractionConfig(use_cache=False))
    for row in output.results[0].tables[0].cells[:4]:
        print(row)


asyncio.run(main(sys.argv[1]))

Run it twice:

bash
uv run --with 'xberg==1.3.0' python repro.py stations.dbf
sha256 d959d96bb9b1d0d7a3cf0f248d64ed44a38da46bc03d8ae5e82c92ca2ad4e48b
['name', 'marker-col', 'marker-sym', 'line']
['rail-metro', 'blue', 'Van Dorn Street', '#0000ff']
['#0000ff', 'blue', 'rail-metro', 'Franconia-Springfield']
['Federal Center SW', 'rail-metro', 'blue', '#0000ff']
sha256 d959d96bb9b1d0d7a3cf0f248d64ed44a38da46bc03d8ae5e82c92ca2ad4e48b
['name', 'marker-col', 'marker-sym', 'line']
['blue', 'rail-metro', 'Van Dorn Street', '#0000ff']
['blue', 'Franconia-Springfield', '#0000ff', 'rail-metro']
['rail-metro', 'Federal Center SW', '#0000ff', 'blue']

Expected: every row in declared field order, identical across runs, e.g. ['Van Dorn Street', '#0000ff', 'rail-metro', 'blue'].

Relevant files and configuration

xberg 1.3.0 from PyPI, macOS arm64, Python 3.12, ExtractionConfig(use_cache=False).
stations.dbf: dBASE III, 4 Character fields, 86 records.
Also reproduced on main at 1409e7be4 through DbfExtractor::extract_content.

I have a fix with regression tests ready and will open a PR.