fix: handle multi-line CSV fields in paged get_as_file()#203
Open
carlkesselman wants to merge 1 commit intomasterfrom
Open
fix: handle multi-line CSV fields in paged get_as_file()#203carlkesselman wants to merge 1 commit intomasterfrom
carlkesselman wants to merge 1 commit intomasterfrom
Conversation
The paged CSV download in get_as_file() determined the @after() cursor by parsing the last raw byte line of each page as a CSV record. When fields contain embedded newlines (RFC 4180 quoted fields), the "last line" is a fragment inside a quoted value, not a complete record. This produced an invalid RID for the cursor (e.g., whitespace + quote character) that sorts before all real RIDs, causing every subsequent page to re-fetch all records — an infinite loop. In one case, 121 records were duplicated 6,814 times producing an 824K-row, 2 GB file. Fix: after writing a page, read back the last complete CSV record from the destination file using Python's csv module, which handles multi-line quoted fields correctly. Uses a chunked reverse-read strategy to avoid loading the entire file into memory. Co-Authored-By: Claude Opus 4.6 (1M context) <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.
Summary
get_as_file()when CSV data contains multi-line quoted fields (RFC 4180)_read_last_csv_record()helper that reads back the last complete record from the destination file using Python'scsvmoduleProblem
The paged CSV download determines the
@after()cursor by parsing the last raw byte line of each page. When fields contain embedded newlines (e.g., OCR text with grid data), the "last line" is a fragment inside a quoted value. This fragment produces an invalid RID for the cursor that sorts before all real RIDs, causing every subsequent page to re-fetch all records.Impact: In one case, 121 records were duplicated 6,814 times, producing an 824K-row, 2 GB CSV file from a 2,130-row table.
Fix
After writing each page to disk, read back the last complete CSV record from the file using
csv.DictReader, which correctly handles multi-line quoted fields. Uses a chunked reverse-read strategy (starting from end of file) to avoid loading the entire file into memory.Test plan
_read_last_csv_recordwith single-line records🤖 Generated with Claude Code