Skip to content

fix(dbf): detect the text encoding from the DBF header - #389

Merged
luabida merged 1 commit into
AlertaDengue:mainfrom
davispecia:fix/dbf-encoding-from-ldid
Nov 23, 2026
Merged

luabida merged 1 commit into
AlertaDengue:mainfrom
davispecia:fix/dbf-encoding-from-ldid

Conversation

@davispecia

Copy link
Copy Markdown
Contributor

Closes #382

O leitor rápido de DBF decodificava tudo como latin-1, mas os arquivos do SIH declaram cp850 no cabeçalho, então o ç virava U+0087.

O que muda:

  • O encoding vem do LDID (byte 29 do cabeçalho). LDID 0x00 ou desconhecido (como o 0x35 do SIM) cai em latin-1, que é o comportamento atual.
  • encoding= continua forçando um code page, e fallback_encoding= muda o padrão.
  • Decode sem perda: byte indefinido no code page (ex.: 0x81 em cp1252) vira o code point de mesmo número, então o valor original pode ser recuperado.
  • O caminho do dbfread (fast=False ou fallback) usava cp1252 fixo. Agora usa o mesmo encoding detectado (DBF.encoding).
  • O encoding deixou de ser global do módulo, então leituras em paralelo de arquivos com code pages diferentes não se atrapalham mais.

Testes com cabeçalhos sintéticos (cp850, cp1252, LDID desconhecido) nos três leitores rápidos e no caminho do dbfread, além de leitura concorrente. A suíte completa passa localmente.

Os parquets do SIH já convertidos ficaram com o texto errado, então vale reconverter esses arquivos.

The fast DBF reader decoded every file as latin-1, but SIH files declare
cp850 in the header (LDID, byte 29), so ç (0x87) came out as U+0087.

- read_dbf_fast, read_dbf_filtered and stream_dbf_fast take
  encoding=None by default and use the code page declared by the LDID;
  LDID 0x00 and unknown LDIDs (0x35 in SIM) use fallback_encoding
  (latin-1). Passing encoding= still overrides detection.
- Decoding is lossless for single-byte code pages: a byte the code page
  leaves undefined (0x81 in cp1252) maps to the code point with the same
  number, as the WHATWG windows-1252 decoder does.
- The encoding is passed down to _decode/_decode_column instead of being
  written to the module-level _ENCODING, so concurrent reads of files
  with different code pages no longer overwrite each other.
- The dbfread path in DBF.load/stream/to_parquet (fast=False, or the
  fallback when the fast reader fails) used a fixed cp1252; it now uses
  the same detected encoding, exposed as DBF.encoding.

Refs AlertaDengue#382
@davispecia
davispecia force-pushed the fix/dbf-encoding-from-ldid branch from b32617f to 313faba Compare November 21, 2026 16:45
@codecov-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 98.90710% with 2 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (main@30479ce). Learn more about missing BASE report.

Files with missing lines Patch % Lines
pysus/data/dbf_reader.py 96.22% 2 Missing ⚠️
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.
Additional details and impacted files
@@           Coverage Diff           @@
##             main     #389   +/-   ##
=======================================
  Coverage        ?   97.37%           
=======================================
  Files           ?      185           
  Lines           ?    24442           
  Branches        ?        0           
=======================================
  Hits            ?    23800           
  Misses          ?      642           
  Partials        ?        0           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@luabida
luabida merged commit 1486990 into AlertaDengue:main Nov 23, 2026
18 checks passed
@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version 2.12.3 🎉

The release is available on:

Your semantic-release bot 📦🚀

luabida added a commit to luabida/PySUS that referenced this pull request Nov 23, 2026
Add --group filtering to management check/run, applied to both origins
and S3 mirrors, and a --reupload-encoding shortcut for the SIA/SIH
groups affected by the DBF encoding fix (AlertaDengue#389).

The shortcut selects SIA AD/AN/AR/BI and SIH RD/RJ, implies --force, and
uses a stable journal so an interrupted run resumes where it stopped.
Dry runs with --force preview every selected file as outdated.

Add pysus.management.scripts.reupload_encoding as a standalone resumable
job with --status, --reset and --dry-run. It checkpoints the catalog on
interrupt before exiting so uploaded files are not missing from the
catalog on restart.
luabida added a commit to luabida/PySUS that referenced this pull request Nov 23, 2026
Add --group filtering to management check/run, applied to both origins
and S3 mirrors, and a --reupload-encoding shortcut for the SIA/SIH
groups affected by the DBF encoding fix (AlertaDengue#389).

The shortcut selects SIA AD/AN/AR/BI and SIH RD/RJ, implies --force, and
uses a stable journal so an interrupted run resumes where it stopped.
Dry runs with --force preview every selected file as outdated.

Add pysus.management.scripts.reupload_encoding as a standalone resumable
job with --status, --reset and --dry-run. It checkpoints the catalog on
interrupt before exiting so uploaded files are not missing from the
catalog on restart.
luabida added a commit to luabida/PySUS that referenced this pull request Nov 23, 2026
Add --group filtering to management check/run, applied to both origins
and S3 mirrors, and a --reupload-encoding shortcut for the SIA/SIH
groups affected by the DBF encoding fix (AlertaDengue#389).

The shortcut selects SIA AD/AN/AR/BI and SIH RD/RJ, implies --force, and
uses a stable journal so an interrupted run resumes where it stopped.
Dry runs with --force preview every selected file as outdated.

Add pysus.management.scripts.reupload_encoding as a standalone resumable
job with --status, --reset and --dry-run. It checkpoints the catalog on
interrupt before exiting so uploaded files are not missing from the
catalog on restart.
luabida added a commit to luabida/PySUS that referenced this pull request Nov 23, 2026
Add --group filtering to management check/run, applied to both origins
and S3 mirrors, and a --reupload-encoding shortcut for the SIA/SIH
groups affected by the DBF encoding fix (AlertaDengue#389).

The shortcut selects SIA AD/AN/AR/BI and SIH RD/RJ, implies --force, and
uses a stable journal so an interrupted run resumes where it stopped.
Dry runs with --force preview every selected file as outdated.

Add pysus.management.scripts.reupload_encoding as a standalone resumable
job with --status, --reset and --dry-run. It checkpoints the catalog on
interrupt before exiting so uploaded files are not missing from the
catalog on restart.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DBF reader decodes SIH text with the wrong code page; encoding should come from the file header

3 participants