Skip to content

Fix multi-byte characters being corrupted at zst chunk boundaries - #93

Open
8roundshock wants to merge 1 commit into
ArthurHeitmann:masterfrom
8roundshock:fix-zst-utf8-chunk-boundary
Open

8roundshock wants to merge 1 commit into
ArthurHeitmann:masterfrom
8roundshock:fix-zst-utf8-chunk-boundary

Conversation

@8roundshock

Copy link
Copy Markdown

Fixes #80.

AI disclosure: this change was written and tested by an AI agent (Claude Code) that I ran. If you'd rather not take AI-assisted contributions, or it just isn't what you want, feel free to close it.

Problem

getZstFileJsonStream decodes each decompressed chunk on its own. When a multi-byte UTF-8 character straddles two chunks, it is replaced with U+FFFD (café → caf��) and nothing is logged. With the default 10 MiB chunk size that is one place per 10 MiB where it can happen. It also happens at frame boundaries in multi-frame .zst files, whatever the chunk size.

Fix

Use an incremental UTF-8 decoder, which holds back an incomplete trailing sequence until the next chunk arrives. 5 lines added and 1 changed in scripts/fileStreams.py; codecs is in the standard library, so there is no new dependency.

Invalid bytes are still replaced with U+FFFD, and the result no longer depends on where the chunk boundaries fall: it is what decoding the whole file at once would give. The final=True call keeps that true for a file that ends in the middle of a character.

The .jsonl, .zst_blocks and .json readers decode whole lines or rows, so they were not affected and are unchanged.

Testing

The repo has no test suite and I didn't add one. I ran these locally on Python 3.10.3 and 3.12.14 with zstandard 0.25.0, each with orjson 3.13.0 and with the stdlib json fallback:

  • The reproduction script from fix(reader): preserve UTF-8 characters across zstd chunk boundaries #80: all three cases match after the fix, two of them fail before it.
  • Every chunk size from 1 to 692 bytes over text with 1, 2, 3 and 4 byte characters: rows equal strict decoding.
  • A two-frame file split at every byte position, default chunk size.
  • Invalid UTF-8: same rows as decoding the whole file at once with errors="replace", at any chunk size.
  • File ending mid-character, cut off .zst, junk after a valid frame, unparsable line in the middle: no exception, same rows and messages as before.
  • Old reader vs new reader on 3,598 generated inputs where the old one had no boundary to trip on (single chunk, or ASCII only): identical rows and identical printed output.
  • processFiles.py run over a folder with .zst, .jsonl, .json and .zst_blocks files.
  • Speed on a 409 MiB (uncompressed) synthetic dump with orjson: 5.24 s after, 5.33 s before (best of 4 runs each). On that file the old reader corrupted 9 of 820,041 rows and the new one none.

A second AI agent, given only the code, reviewed commit 2add299 independently with its own tests and found nothing to change.

I can add the test script to the repo if you'd like to have it.

🤖 Generated with Claude Code

getZstFileJsonStream decoded each decompressed chunk on its own, so a
UTF-8 character that straddles two chunks was replaced with U+FFFD.
Use an incremental decoder, which carries the incomplete bytes over to
the next chunk. Invalid bytes are still replaced as before.

Fixes ArthurHeitmann#80

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
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.

fix(reader): preserve UTF-8 characters across zstd chunk boundaries

1 participant