Skip to content

Read ranges with one request and open files in smaller blocks - #456

Draft
shcheklein wants to merge 6 commits into
mainfrom
ranged-reads
Draft

shcheklein wants to merge 6 commits into
mainfrom
ranged-reads

Conversation

@shcheklein

@shcheklein shcheklein commented Dec 3, 2026 •

Copy link
Copy Markdown
Member

Reading a few bytes of a Drive file downloaded up to 100 MiB first.

Example

fs.head("root/data/model.bin", 1024)     # first 1 KiB of a 2 GiB file
fs.cat_file("root/data/model.bin", 0, 16)
with fs.open("root/data/model.bin") as f:
    magic = f.read(8)

Before:

  • head() downloaded the first 100 MiB and returned 1 KiB of it.
  • cat_file(path, start, end) with a start failed, because the download stream cannot seek. tail() failed the same way.
  • open() downloaded 100 MiB per read request.

The 100 MiB is the Google client library's default chunk size. It came with the streaming reader in #39 (2020); no reason was given for using it.

After

  • head(), tail() and cat_file(path, start, end) send one request with an HTTP Range. head(path, 1024) downloads 1 KiB.
    • Negative positions count from the end, as in a slice. A range with a negative end is read from its start to the end of the file and cut locally, so only a range from the end to a position from the start needs the file's size.
    • A range past the end returns b"", and so does tail(path, 0) or a negative size, without a download.
    • A Google Workspace file (a Doc, a Sheet) can only be exported whole, so it is exported and then cut with the caller's bounds. Drive's size for such a file is not the length of its export.
    • A range of a folder raises IsADirectoryError, and of a missing file ID FileNotFoundError.
    • With compression=, or any other option that changes the bytes, the read goes through fsspec as before.
  • open() asks Drive for fs.blocksize bytes per request: 4 MiB by default, as in other fsspec filesystems. fs.open(path, block_size=...) still sets it per file. A program that reads large files from start to end makes 25 times more requests than with 100 MiB; the guide suggests a larger block_size, such as 64 MiB, for it.

get() / get_file(), which download whole files, keep their 100 MiB chunks for now. A smaller chunk means more requests; I want a measurement on real Drive before changing that.

Validation

Live, on real Drive, with one 2 MiB file of random bytes that the read tests share:

  • head(path, 10) returns the first 10 bytes, and the client asks for bytes=0-9 and receives 10 bytes;
  • tail(path, 10) asks for bytes=-10 and receives 10 bytes;
  • cat_file returns the same bytes as a slice of the data, for a range in the middle, one counted from the end, one to the end, and one past the end;
  • tail(path, 0) and tail(path, -5) return b"" and download nothing;
  • head() of an empty file is b"";
  • cat_file(path, end=5, compression="gzip") returns the first 5 uncompressed bytes;
  • cat_file also returns the slice for a range from the start to before the end, and from the end to a position;
  • a range of a Google Doc, from the start, before the end, at the end and from the end to a position, equals that slice of its export (real Drive gave a new Doc a size that made the first version return b"" for the range before the end);
  • a range of a folder raises IsADirectoryError, by path and by folder ID;
  • head() of the ID of a deleted file raises FileNotFoundError (a made-up ID can get 400 from Drive instead of 404);
  • open().read(10) asks for one block: 4 MiB with no block_size or "default", 512 KiB with block_size=512 * 1024.

Against the local fake Drive, which honours ranges like Drive, the first version of this PR failed the empty-tail, compression, folder and missing-ID cases. The Doc case needs the size real Drive reports. The offline test of the old 100 MiB default is replaced by the live block test.

The fsspec guide describes the read sizes and the reads that download more than they return. The offline suite passes with the current google-auth and with 2.15.0 (534 passed).

🤖 Generated with Claude Code

shcheklein and others added 2 commits December 2, 2026 19:34
- head(), tail() and cat_file(path, start, end) ask Drive for the bytes
  they return, with one ranged request. head() used to download the
  first 100 MiB of a file, and cat_file() with a start failed because
  the stream cannot seek. A Google Workspace file is exported whole
  and cut, since Drive exports only whole files.
- open() asks Drive for fs.blocksize bytes (4 MiB by default) per
  read, instead of 100 MiB, unless block_size says otherwise.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Comment thread pydrive2/fs/spec.py Resolved
Comment thread pydrive2/fs/spec.py Resolved
Comment thread pydrive2/fs/spec.py Resolved
Comment thread pydrive2/test/test_fs.py Resolved
Comment thread pydrive2/fs/spec.py Resolved

@shcheklein shcheklein left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[AI generated — Copilot]

5-pass review summary: strong improvement for ranged reads and short open() reads, but has real edge-case bugs (tail(path, 0) downloads the entire file, missing _translate_not_found, silent b"" on directories with negative ranges, unchunked open-ended cat_file(path, start=N)), plus a 7x test speedup opportunity (two_mib_file scope).

Comment thread pydrive2/fs/spec.py Resolved
Comment thread pydrive2/fs/spec.py Resolved
Comment thread pydrive2/fs/spec.py Resolved
Comment thread pydrive2/fs/spec.py Resolved
Comment thread pydrive2/test/test_fs.py Resolved
Comment thread pydrive2/test/test_fs.py Resolved
Comment thread pydrive2/test/test_fs.py Resolved
@shcheklein shcheklein mentioned this pull request Dec 3, 2026
1 of 2 tasks
…s as asked

tail() of no bytes returns b"" without a download. A range of a folder
raises IsADirectoryError, and of a missing file ID FileNotFoundError. A
read with compression goes through fsspec, as before. A Google
Workspace export is sliced with the caller's bounds, since Drive's size
for such a file is not the export's length. One lookup gives the file's
ID and size.

The live read tests share one 2 MiB file, and check the default
download block size without changing it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Workspace file reads normalize bounds against the reported size rather than the export length, causing mixed negative/positive ranges to return empty data instead of the expected bytes.

1 open issue
What changed in this PR

This PR reduces unnecessary downloads in PyDrive2’s fsspec interface when reading small portions of Drive files.

Changes:

  • Adds HTTP range reads for head(), tail(), and bounded cat_file() calls.
  • Changes default open() download blocks to 4 MiB.
  • Adds live read tests and documents request-size tradeoffs.
File Description
pydrive2/​test/​test_fs.py Adds range, export, error, compression, and block-size tests.
pydrive2/​test/​test_fs_unit.py Removes the old default-chunk-size test.
pydrive2/​fs/​spec.py Implements range reads and smaller default read blocks.
pydrive2/​files.py Adds a single-request HTTP range download helper.
docs/​fsspec.rst Explains read sizes and sequential-read tuning.

Review effort: Balanced

Comment thread pydrive2/fs/spec.py Resolved
A range that ends before the end of the file is read from its start or
from the end and cut locally, so only a range from the end to a
position from the start needs the size. Drive's size of a Google
Workspace file is not the length of its export: real Drive gave a new
Doc a size that made cat_file(doc, -3, -1) return b"". A file without
a checksum, which is how Drive marks Workspace files and shortcuts, has
no usable size and is exported whole.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Drive answers some made-up IDs with 400 Bad Request instead of 404, so the
test now uses the ID of a file it deleted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Empty-range reads bypass raw-ID root validation, masking missing-file and directory errors, and the documentation overstates exact-download guarantees for negative bounds and compressed reads.

1 open issue
1 previously missed issue

In code that hasn't changed since last review

Medium severity Validate raw-ID roots before returning empty ranges

pydrive2/​fs/​spec.py:1443

An empty range bypasses validation when the filesystem is rooted at a raw file ID: _file_for_range(..., need_size=False) returns the ID without metadata, so head(folder_id, 0) and cat_file(missing_id, 0, 0) return b"" instead of IsADirectoryError or FileNotFoundError. Validate the raw-ID root before this early return, without downloading content.

1 resolved since last review

Review effort: Balanced

Comment thread docs/fsspec.rst Resolved
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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.

2 participants