Repository navigation
[Data][Delta DSv2][8/11] Open the Delta table once, in a Ray task - #68407
abhishekverma-ray wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Code Review
This pull request refactors the Delta Lake V2 datasource to open the table once in a Ray task rather than on the driver. This pins the table version, loads the log, and keeps the file list off the driver by returning a pickleable DeltaTableInfo object and storing the add actions in the object store. Feedback was provided regarding a Python 3.9 compatibility issue in the test suite where collections.Counter objects are compared using the <= operator, which is only supported in Python 3.10+.
| assert collections.Counter(log_requests(recorded)) <= collections.Counter( | ||
| log_requests(v1_recorded) | ||
| ) |
There was a problem hiding this comment.
Using <= for collections.Counter comparisons is only supported in Python 3.10 and newer. Since Ray supports Python 3.9, this will raise a TypeError on Python 3.9 environments. Please use a Python 3.9-compatible way to verify that all log requests in recorded are subset of v1_recorded.
| assert collections.Counter(log_requests(recorded)) <= collections.Counter( | |
| log_requests(v1_recorded) | |
| ) | |
| c1 = collections.Counter(log_requests(recorded)) | |
| c2 = collections.Counter(log_requests(v1_recorded)) | |
| assert all(c1[k] <= c2[k] for k in c1) |
921ceae to
fb47ed5
Compare
fb47ed5 to
eb68b6e
Compare
…68282) ## Summary Adds `DeltaDatasourceV2` and `DeltaScanner`, the DataSourceV2 datasource for one version of a Delta Lake table. Third part of the split of #67676. `read_delta` starts using it in #68283. Replaces #67889, which was opened from a fork; earlier review is there. This PR is part of a GitHub stack, so its diff shows only this layer. ## Before No DataSourceV2 datasource for Delta. ## After - `DeltaDatasourceV2(DataSourceWithMetadata)`: the driver pins the version and takes the schema and partition columns from the log. The file list is built from the log in a Ray task, not on the driver; deltalake 1.6 and later load it when the table is opened anyway (`without_files` is ignored there). `_get_file_indexer()` returns the `DeltaFileIndexer` from #67888, and `get_file_partitioner()` the shared row-group bin packer from #67887. #68407 opens the table only once, in a Ray task. - `DeltaScanner(ParquetScanner)`: data files are Parquet, so filter, column and limit pushdown are inherited. Partition columns keep their place in the output; an `include_paths` `path` column comes last, as with the V1 reader. - Partition values come from the log, not the paths (#67888): `DeltaScanner` prunes partitions with `DeltaPartitionPruner`, which the listing applies to each file's values, and reads with `DeltaFileReader`, a `ParquetFileReader` that takes each file's values from the manifest's `__delta_partition_values` column. `ParquetScanner` gets a `reader_class` class attribute so the scanner can swap the reader without copying `create_reader`. - Everything is read through a filesystem rooted at the table directory, as `DeltaTable.to_pyarrow_dataset` does: the caller's, or `deltalake`'s `DeltaStorageHandler` built from `storage_options`. Credentials and `az://` behave as in V1. - Data files are opened with their sizes from the log, so S3 gets no HEAD request per open: - With `deltalake`'s filesystem, `DeltaTableFileSystemHandler` wraps `DeltaStorageHandler`. The listing gives it every file's size before the footer reads, and `DeltaFileReader` gives it each manifest's sizes. It builds the deltalake handler on first use. - With a caller's `SubTreeFileSystem` over S3 (what `read_unity_catalog` passes), `filesystem_for_sized_opens` now unwraps it, as it already did a plain `S3FileSystem`. - `count()` takes each file's row count from the log (`numRecords`), not from its Parquet footer: the whole-file listing that `count()` uses carries the counts in a `__delta_num_records` manifest column, and `DeltaFileReader.read_metadata` reads a footer only for a file whose writer recorded no stats. Tables with deletion vectors, whose counts include deleted rows, never reach this reader. - `delta_datasource_v2_unsupported_reason()` says when a table must stay on the V1 reader: reader features beyond `timestampNtz` (deletion vectors, ...), column mapping, a partition column type whose values don't round-trip through a string (binary, ...), PyArrow scan options, or a table `path` column with `include_paths`. It fails closed. V1 reads some of these tables; it rejects the others (column mapping, deletion vectors) with the same error as before. #68408 replaces this fallback with errors, before #68286 makes V2 the default. - `generated_id_column` rejects Delta reads for now (#68285 adds support). - The pickle gate scan allowlists `delta_datasource_v2.py`: its only match is a schema `.to_arrow()`; data files are read and gated in `ParquetFileReader`. ```mermaid flowchart LR subgraph IO["Subsystem: IO"] subgraph D["Component: datasource_v2/formats/delta"] DS["driver: DeltaDatasourceV2<br/>log schema, pinned version"] --> IX["DeltaFileIndexer (2/11)"] DS --> BP["make_row_group_bin_packer (1/11)"] DS --> SC["DeltaScanner → DeltaFileReader<br/>partition values from the manifest,<br/>table-rooted filesystem"] end end classDef added fill:#c8e6c9,stroke:#2e7d32,color:#000 class DS,SC added ``` ## Subsystems IO (`_internal/datasource_v2/formats/delta/`; `ParquetScanner.reader_class`; `filesystem_for_sized_opens` in `_internal/util.py`). Ripple: logical planning (`bind_generated_id_column.py` rejects Delta scanners). ## Not a duplicate Split out of #67676, which takes over #65142 (tobby168, credited as co-author). No other open PR reads Delta through DSv2. ## Testing Same env as #67887: - `pytest python/ray/data/tests/unit/datasource_v2/test_delta_datasource_v2.py`: 20 passed. It covers: - the schema and partitioning from the log, and version pinning; - column order and `path` placement, and unknown-column errors; - pickling the indexer and scanner, and reading through the unpickled reader: each row gets its file's partition values from the log, and every data file opens with its size from the log (fails if the reader doesn't pass the sizes, or if two files' partition values are swapped); - `count()`'s row counts: from the log with no file opened, and from the footers when the log has no counts (with the reader change reverted, the first case opens all 3 files and fails); - every fallback reason, and a real table passing the eligibility check; - the `generated_id_column` rejection. - `pytest python/ray/data/tests/unit/datasource_v2/`: 1546 passed (rerun after the review round in a fresh env with the Nov 18 Ray nightly and CI pins). At the top of the stack (#68287), after the `count()` change: unit 1740 passed, `test_delta.py` and `test_delta_v2.py` 138 passed, 1 skipped. I skipped `test_text_datasource_v2.py`, which master added this week and which needs `snappy`, not installed locally. - `tests/unit/datasource/` (pickle gate scan): 125 passed. - `pytest python/ray/data/tests/test_util.py`: 51 passed, including `filesystem_for_sized_opens` on a `SubTreeFileSystem` over S3. - The S3 request count is tested end to end in #68283. - pre-commit and pyrefly (CI file set) pass. AI assistance was used (Claude Code); every line has been reviewed by the submitter. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Signed-off-by: Abhishek Verma <[email protected]> Co-authored-by: tobby168 <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
eb68b6e to
88bb093
Compare
88bb093 to
062a54b
Compare
062a54b to
0e28241
Compare
0e28241 to
8e3dd6b
Compare
8e3dd6b to
adee4b6
Compare
adee4b6 to
b98528d
Compare
read_delta opened the table on the driver for its version, schema and the V2 check, and the listing task opened it again for the file list. deltalake 1.6+ has no metadata-only load, so both loaded the whole log: twice V1's requests to _delta_log, and the full file list on the driver. Open the table once, in a Ray task, on every deltalake version. The task returns the table's description (version, schema, partition columns, V2 check) to the driver and keeps the add actions in the object store, where the listing reads them. partition_filters are checked and applied in the same task. The driver never holds the file list, and the before/after-1.6 without_files split is gone. The task also rejects a log that names data files by absolute path (a shallow clone's), so read_delta fails instead of the listing task. The V1 reader can't read such tables either, so the error no longer points to it. The add actions carry their version. If Ray rebuilt them by rerunning the task, which may read a later version, or lost them, the listing loads the pinned version itself. Errors from opening the table reach the caller unwrapped. Reads with PyArrow scan options go to V1 without the task. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Abhishek Verma <[email protected]>
…(opt-in) (#68283) ## Summary Routes `ray.data.read_delta` through the Delta Lake DataSourceV2 reader behind the private, temporary `DataContext._use_delta_datasource_v2` (off by default). Last part of the split of #67676. #68284, #68285, #68330, #68407 and #68408 build on it. #68286 deletes the switch, so `use_datasource_v2` alone decides, as it does for `read_parquet` and `read_orc`. Replaces #67890, which was opened from a fork; earlier review is there. This PR is part of a GitHub stack, so its diff shows only this layer. ## Before `read_delta` opens the whole table on the driver (`DeltaTable.to_pyarrow_dataset()`), builds the full file list there, and reads it through the V1 `ParquetDatasource`, one read task per file. ## After With the switch on (and `use_datasource_v2`, already the default), `read_delta` reads through `DeltaDatasourceV2` (#68282): - The table on the driver is used only for its log metadata (`without_files=True` on deltalake before 1.6; later versions ignore it). - The file list is built in a Ray task from the log at the pinned version (#67888). - Files are split into row-group read units, and DSv2 pushdown and resume apply. - Files are listed in path order, so one version lists its files in one order however its log is checkpointed. `deltalake`'s own order follows the row layout of the latest checkpoint, which the protocol leaves to the writer. - `filesystem=`, `storage_options`, `catalog=` and `include_paths` behave as before. Some reads keep using the V1 reader, with an info log saying why: - Tables that `delta_datasource_v2_unsupported_reason()` rejects. V1 then reads the table that was checked, at the same version. On deltalake 1.6+ it reuses that table rather than loading it again; before 1.6 it reopens it with its file list. - Reads that pass `partition_filters` (added in #67900). The V2 reader doesn't apply them yet and would read every partition, so these reads go to V1 without opening the table for the V2 check, with their own info log. #68330 applies them in V2. `filter(expr)` on partition columns already prunes in V2. Later layers change this before #68286 makes V2 the default: - #68407 opens the table only once, in a Ray task, instead of on the driver and again in the listing. That task also rejects a log that names data files by absolute path (as a shallow clone's does), so `read_delta` fails right away. V1 can't read such tables either. - #68408 replaces the V1 fallback with errors. ### Changes from V1 - An empty table's `schema()` is the schema in the log. V1 returns `None`. - `input_files()` is empty until the read runs, because the files are listed in Ray tasks (as for `read_parquet` on DataSourceV2). - Read tasks hold row-group runs packed by size, not one file each. A large file can be split across tasks, and small files share one. - The log is read in Ray tasks (the listing here; from #68407 also the table open), so worker nodes, not only the driver, need read access to the table's `_delta_log`. Credentials in `storage_options` travel with the tasks. #68286 says so in the `read_delta` docstring. - A data file whose size is missing from the log is still read (its footer read finds the size); only files of size 0 are skipped. - With `preserve_order=True`, rows come back in path order. V1 reads files in `deltalake`'s order (newest commit first for JSON commits), which can change for one version when a checkpoint lays out its rows in another order. - Unchanged: the version is pinned when `read_delta` is called. ```mermaid flowchart TD subgraph Before subgraph BS[Subsystem: IO] subgraph BC["Component: read_delta (read_api.py)"] B1["driver: DeltaTable(...).to_pyarrow_dataset()<br/>(full file list)"] --> B2["V1 ParquetDatasource.from_pyarrow_dataset"] B2 --> B3["Read: one task per file"] end end end subgraph After subgraph AS[Subsystem: IO] subgraph AC["Component: read_delta → DataSourceV2 formats/delta"] A0{"_use_delta_datasource_v2, table supported<br/>and no partition_filters?"} -->|no| A5["V1 path (unchanged)"] A0 -->|yes| A1["driver: DeltaTable (log metadata)<br/>pin version, schema, partition cols"] A1 --> A2["ListFiles task: DeltaFileIndexer<br/>add actions at pinned version, sorted by path, footer reads"] A2 --> A3["OnlineBinPacker: row-group read units"] A3 --> A4["ReadFiles: DeltaScanner / ParquetFileReader"] end end end classDef added fill:#c8e6c9,stroke:#2e7d32,color:#000 classDef changed fill:#fff9c4,stroke:#f9a825,color:#000 class A0 changed class A1,A2,A3,A4 added ``` ## Subsystems API (`read_api.read_delta`, the private `DataContext._use_delta_datasource_v2`) carries the change; IO for the listing order, the README and the tests. ## Not a duplicate Split out of #67676, which takes over #65142 (tobby168, credited as co-author). richardliaw asked for a restart on the current DSv2 layout. No other open PR reads Delta through DSv2. ## Testing Same env as #67887 (deltalake 1.5.0, pyarrow 23). Each module runs in its own pytest process, as CI runs one `py_test` per file: - `test_delta.py`: 54 passed. `test_delta_v2.py`: 35 passed. `tests/datasource_v2/test_delta_footer_listing.py`: 2 passed. - `test_delta.py` runs under both readers through conftest's `datasource_v2` fixture. Tests that take the V1 path with either reader run once: the `partition_filters` tests (until #68330), the multiple-paths check, `_delta_file_sizes` and `use_datasource_v2 = False`. An empty table's schema from the log is a separate V2-only test. - The no-`get_file_info` check (`test_delta_read_takes_file_sizes_from_the_log`) runs under both readers. Its handler records each call in a file, so calls made inside Ray tasks count too. - What `test_delta_v2.py` checks, V2 only (`datasource_v2` fixture, `ray_start_regular_shared`), through `read_delta` and `Dataset` methods. It tells the readers apart by the read operator's name in `ds.stats()`. - V2 returns the same rows and schema as V1 for partitioned tables (special-character, null, int and date partitions). - Filters on partition and data columns, `columns=`, `include_paths` and a table-rooted `filesystem=`. - Count after overwrite, version pinning at call time, row-group splitting, the read order, a seeded shuffle, and each V1 fallback. - `test_delta_footer_listing.py` builds the indexer as `read_delta` does and lists the table: the row groups a resumed job skips, and those a filter prunes. Footer reads run in Ray actors, so these live in `tests/datasource_v2/`, not `tests/unit/`. - Tests that fail without this layer's fixes: - `test_a_version_reads_its_files_in_one_order` reads a table, rewrites its checkpoint with the rows in reverse order (as the protocol allows), and reads the same version again, through the same Dataset and a new `read_delta`. Without the path sort, the order changes; so does the listing-order check in `test_indexer_and_scanner_survive_worker_serialization`. - `test_snapshot_repr_leaves_out_storage_options` fails when `storage_options` is in the snapshot's `repr`. - `test_partition_filters_fall_back_to_v1` fails without the fallback (the read goes to V2). - `test_skips_only_empty_files` fails with the old size check, which also dropped files of unknown size. - `test_falls_back_to_v1_with_pyarrow_scan_options` appends to the table right after the check, with deltalake reporting 1.5 and 1.6. It checks that V1 reads the checked version and, on 1.6, opens the table once. Both cases fail with the old code, which reopened the latest version. - `test_partition_values_come_from_the_log` reads tables whose paths don't give their partition values: random file prefixes, timestamp partitions with a time zone, and the string `__HIVE_DEFAULT_PARTITION__` next to nulls. It reads them through V2 and matches V1, with and without a filter on the partition column. - `test_delta_read_does_not_unpickle_a_tampered_data_file`: a data file swapped for one with a pickled-object column reads as plain bytes under both readers, and its pickle never runs. The test also writes the swapped file's size into the log, because V2 opens data files with the log's sizes, as Delta readers do. - `test_s3_reads_send_no_head_requests` reads a table on moto S3, through deltalake's filesystem and through a caller's `SubTreeFileSystem` over S3. Using moto's request recorder, it checks that no data file gets a HEAD request. - `tests/unit/datasource_v2/` and `tests/unit/datasource/`: 1521 passed, 225 skipped. `test_write_delta.py`: 105 passed. - Each guard was removed one at a time, and a test failed each time: path decoding, the log-based whole-file indexer, version pinning, projection pinning, the protocol fallback, shuffle, the indexer's own filesystem, schema pinning against tampered files, the path order and the `repr`. - pre-commit and pyrefly (CI file set, with deltalake 1.5 and pyarrow 23 resolved) pass. AI assistance was used (Claude Code); every line has been reviewed by the submitter. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Signed-off-by: Abhishek Verma <[email protected]> Co-authored-by: tobby168 <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
b98528d to
1849bf1
Compare
Summary
read_deltaopened the Delta table on the driver to pin the version, take the schema and check that V2 can read it, and the listing task opened it again for the file list.deltalake1.6+ has no metadata-only load (delta-io/delta-rs#5034), so both opens loaded the whole log: twice V1's requests to_delta_log, and the full file list held on the driver. This layer opens the table once, in a Ray task.This PR is part of a GitHub stack, so its diff shows only this layer.
Before
DeltaTable(...)(withwithout_files=Truebefore 1.6, a full load from 1.6 on), the V2 check,partition_filtersvalidation.DeltaTable(...)again at the pinned version, thenget_add_actions().After
open_delta_table()runs one Ray task that opens the table (on everydeltalakeversion), checks it, validatespartition_filtersand selects their files, and loads theaddactions. It has two return values: a smallDeltaTableInfo(URI, version, schema, partition columns, V2 check) that the driver reads, and theaddactions, which stay in the object store. The driver never holds the file list. Thewithout_filessplit bydeltalakeversion is gone.DeltaTableSnapshot.loaded_add_actionsholds the reference; the listing reads it instead of opening the table.addactions carry their version in the schema metadata. If they're lost, or Ray rebuilt them by rerunning the task (which could read a later version whenversion=isn't set), the listing loads the pinned version itself.partition_filters) reaches the caller as its own type, not wrapped inRayTaskError.read_delta()raises instead of the listing task failing once the read runs. The V1 reader can't read these tables either (it raisesFileNotFoundErroron deltalake 1.5), so the error no longer suggests switching to it.runtime_env, which may be what installsdeltalakeon the workers.flowchart LR subgraph IO["Subsystem: IO"] subgraph RA["Component: read_delta (read_api.py)"] R["open_delta_table(): one Ray task"] end subgraph D["Component: datasource_v2/formats/delta"] T["task: open, check, load add actions"] -->|DeltaTableInfo| DR["driver: DeltaDatasourceV2"] T -->|add actions, object store| L["listing tasks: DeltaTableSnapshot.add_actions()"] end R --> T end classDef changed fill:#c8e6c9,stroke:#2e7d32,color:#000 class R,T,L changedSubsystems
IO (
_internal/datasource_v2/formats/delta/,read_deltainread_api.py).Not a duplicate
Part of the Delta DataSourceV2 stack; it addresses review feedback on #68282. No other open PR changes how the Delta V2 reader opens the table.
Testing
Ray nightly of Nov 18 with CI pins (pyarrow 23.0.1, pandas 2.3.3, deltalake 1.5.0), moto for S3:
test_s3_reads_send_no_extra_requests(wastest_s3_reads_send_no_head_requests) records the S3 requests of the same read on V2 and V1 and checks that V2 sends no request to_delta_logthat V1 doesn't. On the test table onedeltalakeload is 6 requests; V1 sends 6 with a caller's filesystem and 8 without. With the listing loading the log itself again, both cases fail (12 requests).test_listing_reloads_add_actions_of_another_version:addactions of a later version make the listing load the pinned one.test_delta_read_deletion_vectors_message_is_actionableused to mockDeltaTable.protocolin the test process, which can't reach the task. It now reads a table whose hand-written log declares the feature (write_deletion_vector_table), so both readers hit the real protocol error.test_read_delta_rejects_data_files_outside_the_tablerewrites a log entry to an absolute path and expectsread_delta()itself to raise. Without the check,read_delta()returns and only the read fails.pytestovertest_delta.py,test_delta_v2.py,unit/datasource_v2/test_delta_file_indexer.pyandunit/datasource_v2/test_delta_datasource_v2.py: 169 passed.tests/unit/datasource_v2/andtests/unit/datasource/: 1733 passed (I skippedtest_text_datasource_v2.py, which needssnappy, not installed locally).test_delta.py,test_delta_v2.pyandtest_write_delta.py, 229 passed; unit 1588 passed.deltalakeandpyarrowinstalled) reports no errors in the changed files.AI assistance was used (Claude Code); every line has been reviewed by the submitter.
🤖 Generated with Claude Code