Repository navigation
[Data][Delta DSv2][3/7] Add the Delta Lake DataSourceV2 datasource - #67889
abhishekverma-ray wants to merge 3 commits into
Conversation
806310c to
8868a51
Compare
794fe07 to
38f12f0
Compare
There was a problem hiding this comment.
Code Review
This pull request implements the DataSourceV2 reader for Delta Lake tables, introducing DeltaDatasourceV2, DeltaFileIndexer, and DeltaScanner. It also enhances partition handling by introducing a null_fallback option to correctly parse and type null partition values (such as __HIVE_DEFAULT_PARTITION__) across Parquet, ORC, and CSV formats, and resolves a pickling issue with the ParquetFileReader batch size sentinel. The review feedback highlights two critical bugs in delta_file_indexer.py: first, ADD_ACTION_PARTITION_PREFIX should be updated to "partition_values." to match the flattened columns from deltalake; second, the check for the literal null partition sentinel should be refactored using values.unique().to_pylist() to avoid ValueError and AttributeError exceptions.
| # Columns of ``DeltaTable.get_add_actions(flatten=True)``. | ||
| ADD_ACTION_PATH_COLUMN = "path" | ||
| ADD_ACTION_SIZE_COLUMN = "size_bytes" | ||
| ADD_ACTION_PARTITION_PREFIX = "partition." |
There was a problem hiding this comment.
In deltalake, the add action stores partition values in a struct/map named partition_values. When flattened via get_add_actions(flatten=True), the resulting columns are prefixed with partition_values., not partition.. Because of this, the check in _check_no_literal_null_partition_values is silently bypassed as the column names never match.
| ADD_ACTION_PARTITION_PREFIX = "partition." | |
| ADD_ACTION_PARTITION_PREFIX = "partition_values." |
| pa.types.is_string(values.type) or pa.types.is_large_string(values.type) | ||
| ): | ||
| continue | ||
| if values.index(HIVE_DEFAULT_PARTITION).as_py() != -1: |
There was a problem hiding this comment.
Using values.index(HIVE_DEFAULT_PARTITION).as_py() has two issues: (1) ChunkedArray.index raises a ValueError if the value is not present (which is the common case here), rather than returning -1. (2) ChunkedArray.index returns a Python int, which does not have an .as_py() method, leading to an AttributeError. Since partition columns have low cardinality, a robust and efficient way to check for the sentinel is to get the unique values and check membership in Python.
| if values.index(HIVE_DEFAULT_PARTITION).as_py() != -1: | |
| if HIVE_DEFAULT_PARTITION in values.unique().to_pylist(): |
38f12f0 to
4ac99a1
Compare
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 2 potential issues.
Reviewed by Cursor Bugbot for commit 4ac99a1. Configure here.
6a85013 to
235de47
Compare
Add the listing for the Delta Lake DataSourceV2 reader. A Delta table's data files are the ones its transaction log names at a version, not whatever the table directory holds, so the indexers here read the file list from the log instead of walking directories: - `DeltaTableSnapshot`: a picklable reference to one table version. A `deltalake.DeltaTable` can't be pickled, so listing tasks reopen the table from it at the version the driver pinned. - `DeltaFileIndexer(_DeltaLogListing, FooterFileIndexer)`: lists the `add` actions, then reads each file's Parquet footer, so listing rows are row-group runs (pruning, read units and resume as for Parquet). - `DeltaWholeFileIndexer`: the log-based listing for metadata-only reads. `FooterFileIndexer.as_whole_file_indexer` would walk the directory. - Paths are relative to the table root and URL-decoded once. Partition values come from Hive-style directories, parsed with `null_fallback=HIVE_DEFAULT_PARTITION`; a file not under them, or a partition value that is literally `__HIVE_DEFAULT_PARTITION__`, fails the listing instead of reading wrong values. - The indexer carries the table's filesystem and the read's shuffle, since the framework passes neither to a metadata-backed source. Nothing uses the indexers yet: the datasource and `read_delta` wiring follow in separate PRs. Co-authored-by: tobby168 <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Abhishek Verma <[email protected]>
The listing took each file's partition values from its Hive directory and dropped the ones the log records, so it failed tables the V1 reader reads: files outside partition directories (the Delta protocol doesn't require them) and the string `__HIVE_DEFAULT_PARTITION__`, which shares a directory with null values. Now the listing reads the values from the `add` actions: - `partition_values_as_strings` casts them to the table's types and back to strings with Arrow, the form the reader casts back exactly, as it does a value parsed from a path. - `DeltaPartitionPruner` evaluates the pushed-down partition predicate on those values. - Both indexers add them to each manifest in `PARTITION_VALUES_COLUMN`, which the bin packer carries to the read tasks. The reader that uses it comes with the datasource. The two directory checks go. Also drop the unused `list_files_from_file_infos` override and `DeltaWholeFileIndexer`'s `shuffle_config`, and the unit tests that the `read_delta` tests cover. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Abhishek Verma <[email protected]>
Add `DeltaDatasourceV2`, a `DataSourceWithMetadata` for one version of a Delta Lake table, and `DeltaScanner`: - The driver opens the log without its file list, pins the version and takes the schema and partition columns from the log; listing goes through the `DeltaFileIndexer` from the previous PR. - Data files are Parquet: `DeltaScanner` is a `ParquetScanner`, so the filter, column and limit pushdown are inherited. Partition columns keep their place in the output, and an `include_paths` `path` column comes last, as with the V1 reader. - Partition values come from the log, not from the paths: `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. `ParquetScanner` gets a `reader_class` attribute for that. - Everything is read through a filesystem rooted at the table directory, as `DeltaTable.to_pyarrow_dataset` does: the caller's, or a `deltalake` `DeltaStorageHandler` built from `storage_options`. - `delta_datasource_v2_unsupported_reason` says why 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`. - `generated_id_column` rejects Delta reads for now. - The pickle gate scan allowlists `delta_datasource_v2.py`, whose only match is a schema `.to_arrow()`; data files are read and gated in `ParquetFileReader`. `read_delta` doesn't use the datasource yet; that wiring follows in a separate PR. Co-authored-by: tobby168 <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Abhishek Verma <[email protected]>
235de47 to
daec722
Compare
) ## Summary Adds the file listing for the Delta Lake DataSourceV2 reader: indexers that list a table version's data files from its transaction log. Second part of the split of #67676. Builds on #67887 (merged). Nothing uses the indexers until #67889 and #67890. The review changes are in the second commit (443b3a1cdb). ## Before No DataSourceV2 listing for Delta. `read_delta` lists files on the driver with `DeltaTable.to_pyarrow_dataset()`. ## After New `_internal/datasource_v2/formats/delta/delta_file_indexer.py`: - `DeltaTableSnapshot`: a picklable reference to one table version (`deltalake.DeltaTable` can't be pickled). Listing tasks reopen the table from it at the version the driver pinned. - `DeltaFileIndexer(_DeltaLogListing, FooterFileIndexer)`: lists the version's `add` actions instead of walking the directory, which also holds removed and uncommitted files, then reads each file's Parquet footer. Listing rows are row-group runs, so predicate pruning, read units and resume work as for Parquet. - `DeltaWholeFileIndexer`: the log-based listing for metadata-only reads. `FooterFileIndexer.as_whole_file_indexer` would walk the directory. - Paths are relative to the table root and URL-decoded exactly once; an absolute path fails with a pointer to the V1 reader. - Partition values come from the log's `add` actions, not from the paths: the Delta protocol doesn't require partition directories, and Delta stores a null and the string `__HIVE_DEFAULT_PARTITION__` in the same one. `partition_values_as_strings` casts them to the table's types and back to strings with Arrow, the form the reader casts back exactly, as it does a value parsed from a path. `DeltaPartitionPruner` prunes files on them, and both indexers add them to each manifest in `__delta_partition_values`, which `OnlineBinPacker` carries to the read tasks. The reader that uses it comes in #67889. - The indexer carries the table's filesystem and the read's shuffle, because the framework passes neither to a metadata-backed source. ```mermaid flowchart LR subgraph IO["Subsystem: IO"] subgraph D["Component: datasource_v2/formats/delta"] S["DeltaTableSnapshot<br/>(uri, pinned version)"] --> L["ListFiles task: _DeltaLogListing<br/>add actions → relative paths, sizes,<br/>partition values (pruned, added to manifests)"] L --> F["FooterFileIndexer<br/>footer reads → row-group runs"] L --> W["DeltaWholeFileIndexer<br/>one row per file"] end end classDef added fill:#c8e6c9,stroke:#2e7d32,color:#000 class S,L,F,W added ``` ## Subsystems IO (`_internal/datasource_v2/formats/delta/`). ## Not a duplicate Split out of #67676, which takes over #65142 (tobby168, credited as co-author) on the current DSv2 layout. No other open PR reads Delta through DSv2. ## Testing Same env as #67887: - `pytest python/ray/data/tests/unit/datasource_v2/test_delta_file_indexer.py`: 18 passed. Covers what `read_delta` can't reach: absolute paths, partition values from the log (a null and the literal `__HIVE_DEFAULT_PARTITION__` told apart, a file outside partition directories), their string round trip for each type, pruning on them, path pruners, log sizes and the manifest column. Listing through `read_delta` (pinned version, path decoding, shuffle, resume) is tested in #67890's `test_delta_v2.py`. - `pytest python/ray/data/tests/unit/datasource_v2/`: 933 passed; `tests/unit/datasource/` (pickle gate scan): 110 passed. - 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]>
| @@ -0,0 +1,271 @@ | |||
| """``DataSourceV2`` for Delta Lake tables. | |||
|
|
|||
| The driver opens the table's log without its file list, pins the version and | |||
There was a problem hiding this comment.
On deltalake 1.6+, without_files is ignored, so the driver loads every file's metadata; the module docstring, the table argument's docs and the description say it doesn't.
t = deltalake.DeltaTable("sales", without_files=True)
len(t.file_uris())
# DeprecationWarning: The 'without_files' parameter is deprecated and ignored. Table loading materializes active-file metadata.
# 2| return path | ||
|
|
||
|
|
||
| def test_datasource_lists_the_pinned_version(tmp_path): |
There was a problem hiding this comment.
A few tests repeat each other or 2/7's tests:
test_reads_through_a_filesystem_rooted_at_the_table:test_indexer_and_scanner_survive_worker_serializationalready reads every row through the same default filesystem.test_datasource_paths_and_partitioner: checks a label, the packer's class and that a seed is set.test_scanner_prunes_partitions_with_the_log_values: 2/7'stest_partition_pruner_uses_the_log_valuescovers pruning on the log values, andprune_input_split(...) is manifestchecks a no-op.test_delta_datasource_v2_unsupported_reason: thecolumnMapping.mode: none,randomizeFilePrefixesand timestamp-partition cases take the same path as the first case.
Could the first fallback case use a real table (partitioned_table) instead of the stub? It's the only case that would notice a deltalake version whose protocol(), metadata() or schema().to_arrow() differs; today that sends every table to V1 without a failure.
| import pyarrow.fs as pa_fs | ||
| from deltalake.fs import DeltaStorageHandler | ||
|
|
||
| return pa_fs.PyFileSystem(DeltaStorageHandler(table_uri, options=storage_options)) |
There was a problem hiding this comment.
On S3, V2 sends a HEAD request every time it opens a data file, three per file; V1 sends none because it gives deltalake's filesystem the file sizes from the log (known_sizes).
Requests to the data files of a 4-file table (local moto S3):
V1 HEAD=0 GET=8
V2 HEAD=12 GET=12
| return DeltaPartitionPruner(self.partition_predicate, self.schema) | ||
|
|
||
| @override | ||
| def prune_input_split(self, input_split: FileManifest) -> FileManifest: |
There was a problem hiding this comment.
nit: this override returns what FileScanner.prune_input_split already returns
| return f"PyArrow scan options were passed: {sorted(arrow_parquet_args)}" | ||
| try: | ||
| protocol = table.protocol() | ||
| if protocol.min_reader_version not in (1, 3): |
There was a problem hiding this comment.
nit: why is 2 excluded?
| ) | ||
| _validate_shuffle_arg(shuffle) | ||
| # ``_read_datasource_v2`` gives a metadata-backed source's listing no | ||
| # file shuffle, so the indexer applies it. As for file sources, the seed |
There was a problem hiding this comment.
nit: file sources draw this seed at planning (_shuffle_config_factory in read_api.py), not at creation
|
Closing in favor of #68282. Richard asked for the remaining Delta DSv2 PRs as a GitHub stack, so they now live on branches in The stack is https://github.com/ray-project/ray/stack/68288, and each PR now shows only its own diff. Earlier review comments stay here for reference. Please leave new comments on #68282. |
…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]>

Summary
Adds
DeltaDatasourceV2andDeltaScanner, the DataSourceV2 datasource for one version of a Delta Lake table. Third part of the split of #67676. Stacked on #67888; review only the top commit (daec722780).read_deltastarts using it in #67890.Before
No DataSourceV2 datasource for Delta.
After
DeltaDatasourceV2(DataSourceWithMetadata): the driver opens the log without its file list, pins the version and takes the schema and partition columns from the log._get_file_indexer()returns theDeltaFileIndexerfrom [Data][Delta DSv2][2/7] List Delta Lake table files from the log #67888, andget_file_partitioner()the shared row-group bin packer from [Data][Delta DSv2][1/7] Shared Parquet and partitioning fixes for the Delta reader #67887.DeltaScanner(ParquetScanner): data files are Parquet, so filter, column and limit pushdown are inherited. Partition columns keep their place in the output; aninclude_pathspathcolumn comes last, as with the V1 reader.DeltaScannerprunes partitions withDeltaPartitionPruner, which the listing applies to each file's values, and reads withDeltaFileReader, aParquetFileReaderthat takes each file's values from the manifest's__delta_partition_valuescolumn.ParquetScannergets areader_classclass attribute so the scanner can swap the reader without copyingcreate_reader.DeltaTable.to_pyarrow_datasetdoes: the caller's, ordeltalake'sDeltaStorageHandlerbuilt fromstorage_options. Credentials andaz://behave as in V1.delta_datasource_v2_unsupported_reason()says when a table must stay on the V1 reader: reader features beyondtimestampNtz(deletion vectors, ...), column mapping, a partition column type whose values don't round-trip through a string (binary, ...), PyArrow scan options, or a tablepathcolumn withinclude_paths. It fails closed.generated_id_columnrejects Delta reads for now ([Data][Delta DSv2][6/7] Support generated_id_column checkpointing for Delta reads #67678 adds support).delta_datasource_v2.py: its only match is a schema.to_arrow(); data files are read and gated inParquetFileReader.flowchart LR subgraph IO["Subsystem: IO"] subgraph D["Component: datasource_v2/formats/delta"] DS["driver: DeltaDatasourceV2<br/>log schema, pinned version"] --> IX["DeltaFileIndexer (2/7)"] DS --> BP["make_row_group_bin_packer (1/7)"] 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 addedSubsystems
IO (
_internal/datasource_v2/formats/delta/;ParquetScanner.reader_class). Ripple: logical planning (bind_generated_id_column.pyrejects 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: 25 passed. Covers the schema and partitioning from the log, version pinning, column order andpathplacement, unknown-column errors, shuffle seeding, pickling the indexer and scanner and reading through the unpickled reader (with each file's partition values from the log), partition pruning on the log values, the table-rooted filesystem, every fallback reason, and thegenerated_id_columnrejection.pytest python/ray/data/tests/unit/datasource_v2/: 958 passed;tests/unit/datasource/(pickle gate scan): 112 passed.AI assistance was used (Claude Code); every line has been reviewed by the submitter.
🤖 Generated with Claude Code