Repository navigation
Fix multi-byte characters being corrupted at zst chunk boundaries - #93
Open
8roundshock wants to merge 1 commit into
Open
8roundshock wants to merge 1 commit into
8roundshock wants to merge 1 commit into
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #80.
Problem
getZstFileJsonStreamdecodes 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.zstfiles, 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;codecsis 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=Truecall keeps that true for a file that ends in the middle of a character.The
.jsonl,.zst_blocksand.jsonreaders 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
jsonfallback:errors="replace", at any chunk size..zst, junk after a valid frame, unparsable line in the middle: no exception, same rows and messages as before.processFiles.pyrun over a folder with.zst,.jsonl,.jsonand.zst_blocksfiles.A second AI agent, given only the code, reviewed commit
2add299independently 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