Repository navigation
[cherry-pick][2.60.1][Data] Add partition_filters to read_delta and take file sizes from the log (#67900) - #68214
Conversation
…he log (#67900) ## Summary `read_delta` gains `partition_filters`, which lists and reads only the matching partitions' files, and takes file sizes from the Delta log instead of one `get_file_info` call per file. ## Before `read_delta` had no way to read only some partitions: - `DeltaTable.to_pyarrow_dataset()` was called without a filter, so the dataset had a fragment for every file in the table. - `ParquetDatasource.from_pyarrow_dataset` called `get_file_info` on every fragment to size the read tasks: one request per file on object storage, although the sizes are in the Delta log. - A `filter` on a partition column became a PyArrow row filter, so every file was still opened. On a table with 26,085 files, a one-date read needs 60 of them; building the dataset took 26,085 `get_file_info` requests before the read started. ## After - `read_delta(partition_filters=...)` takes deltalake's tuple filters: `(column, op, value)` with `=`, `!=`, `<`, `<=`, `>`, `>=`, `in`, `not in`. A flat list is an AND, and a list of lists is an OR of ANDs. Only the matching partitions' files become fragments, so only they are sized and read. - deltalake 1.6.6 takes the filters as `file_pruning_predicate` (AND or OR of ANDs). Older versions take them as `partitions`, which only supports an AND list (deltalake 1.5.0 and 1.6.3 fail on an OR with `not enough values to unpack`), so an OR of groups raises a clear error asking for 1.6.6 there. - Every column must be a partition column. deltalake would accept others but prune them only by file statistics, so rows outside the filter would come back. Use `Dataset.filter` for those. - `read_unity_catalog` passes it through `reader_kwargs`. - File sizes come from the log's add actions. Their paths are URL-encoded once more than the fragment paths (`p=a%2520b` vs `p=a%20b`), so they're decoded once. `ParquetDatasource.from_pyarrow_dataset` takes them as `known_file_sizes` and calls `get_file_info` only for paths missing from it. ```mermaid flowchart TD subgraph Before subgraph BS[Subsystem: IO] subgraph BC["Component: read_delta → ParquetDatasource.from_pyarrow_dataset"] B1["DeltaTable.to_pyarrow_dataset()<br/>every file"] --> B2["get_file_info per fragment"] B2 --> B3["read tasks: every file"] end end end subgraph After subgraph AS[Subsystem: IO] subgraph AC["Component: read_delta → ParquetDatasource.from_pyarrow_dataset"] A0["partition_filters<br/>(validated: partition columns only)"] --> A1["to_pyarrow_dataset(file_pruning_predicate | partitions)<br/>matching files only"] A1 --> A2["sizes from the Delta log<br/>(get_file_info only for misses)"] A2 --> A3["read tasks: matching files"] end end end classDef added fill:#c8e6c9,stroke:#2e7d32,color:#000 classDef changed fill:#fff9c4,stroke:#f9a825,color:#000 class A0 added class A1,A2 changed ``` ## Subsystems API (`read_api.read_delta`, new `partition_filters` parameter) carries the change; IO for `ParquetDatasource.from_pyarrow_dataset`'s `known_file_sizes`. ## Not a duplicate #62959 ("Add DeltaDatasource with partition predicate pushdown") was closed by the stale bot in May 2026 without review. No open PR adds partition filters to `read_delta`. The DataSourceV2 Delta reader in review (#67888–#67890) prunes partitions from `filter(expr)`; it will send reads with `partition_filters` to this reader until it supports them natively. ## Testing macOS / Python 3.12, env pinned to the CI lock (pyarrow 23.0.1, deltalake 1.5.0): - `pytest python/ray/data/tests/datasource/test_delta.py`: 31 passed on deltalake 1.5.0, and 31 passed with deltalake 1.6.6 on the path. On a table with two files per date partition, `Dataset.input_files()` lists only the matching partitions' files, and the rows match, for: - `=`, `in`, a range, and an AND of two conditions; - an OR of AND groups, which reads on 1.6.6 and raises the clear error on 1.5.0; - invalid filters (a data column, an empty list, a short tuple, an empty group), which raise `ValueError`; - `read_unity_catalog(reader_kwargs={"partition_filters": ...})`. - With a filesystem that counts `info` calls and partition values with special characters (`a b`, `x/y`, `100%`), building the dataset makes 0 `get_file_info` lookups. With the log sizes disabled, it makes 4 (one per file). - `_delta_file_sizes` matches the real file sizes, keyed by the fragment paths deltalake builds, special characters and null partitions included. - `test_catalog.py`: 46 passed; `test_write_delta.py`: 101 passed; `tests/unit/datasource/`: 94 passed. - pre-commit, pyrefly (CI file set) and the API param coverage check 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: Claude Opus 5.5 <[email protected]> (cherry picked from commit 63ff72c) Signed-off-by: Sai Miduthuri <[email protected]>
test_read_unity_catalog_partition_filters from #67900 uses mock.patch.object. On master the import comes from #66275, which isn't on releases/2.60.1. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Sai Miduthuri <[email protected]>
There was a problem hiding this comment.
Code Review
This pull request introduces partition filtering support to ray.data.read_delta and optimizes file size lookups by retrieving file sizes directly from the Delta log, avoiding expensive get_file_info calls on object storage. Feedback on the changes includes importing the missing inspect module to prevent a runtime NameError and deduplicating unknown_paths to avoid redundant network requests when duplicate paths are present.
| Returns: | ||
| Keyword arguments for ``DeltaTable.to_pyarrow_dataset``. | ||
| """ | ||
| from deltalake import DeltaTable |
There was a problem hiding this comment.
The standard library module inspect is used on line 6478 (inspect.signature(...)) but it is not imported within this function or verified to be imported globally in this file. To prevent a potential NameError at runtime, please import inspect locally alongside DeltaTable.
| from deltalake import DeltaTable | |
| import inspect | |
| from deltalake import DeltaTable |
| infos = filesystem.get_file_info(pq_paths) | ||
| file_sizes = [info.size if info.size is not None else 0 for info in infos] | ||
| sizes = dict(known_file_sizes or {}) | ||
| unknown_paths = [path for path in pq_paths if path not in sizes] |
There was a problem hiding this comment.
If pq_paths contains duplicate paths, unknown_paths will also contain duplicates, leading to redundant get_file_info calls (which can be expensive on cloud object storage). Deduplicating unknown_paths using list(set(...)) before querying the filesystem will avoid redundant network requests.
| unknown_paths = [path for path in pq_paths if path not in sizes] | |
| unknown_paths = list(set(path for path in pq_paths if path not in sizes)) |
…mn mapping in read_delta (#67895) (#68213) ## Description Cherry-pick of #67895 (master commit 3094de2) onto `releases/2.60.1`. **Bottom of a 2-PR stack on `releases/2.60.1`:** this PR, then #68214 (the #67900 pick). Merging #68214 with `gh stack merge` lands both in order. `read_delta` reads through `DeltaTable.to_pyarrow_dataset()`, which doesn't apply a Delta table's column mapping. A table with `delta.columnMapping.mode` set to `name` or `id` stores its columns in the data files under physical names such as `col-<uuid>`. With deltalake 1.6.4 to 1.6.6, a reader 2 / writer 5 table passes deltalake's own protocol check, and every mapped column comes back as nulls. `ds.schema()` still shows the right names, so nothing downstream notices. #67895 makes `read_delta`, and `read_unity_catalog` through it, raise `NotImplementedError` for these tables before reading. The message names the mode and points to #64854. Tables without column mapping, including an explicit `mode = none`, read as before. ## Related issues Related to #67895. Reading column-mapped tables is tracked in #64854. ## Additional information ### Cherry-pick details `read_api.py` applied cleanly with `git cherry-pick -x`. `test_delta.py` conflicted only on context. On master, the new tests follow `test_delta_read_deletion_vectors_message_is_actionable`, which #66275 added and which isn't on this branch. I kept only what #67895 adds: `import json`, `_write_column_mapped_table`, `test_delta_read_rejects_column_mapping` and `test_delta_read_column_mapping_mode_none`. The added lines are identical to the master commit's, in the same 2 files, +109/-0. Not a duplicate: neither `releases/2.60.1` nor any open or merged PR against it contains #67895. No open PR on any branch picks it. ### Testing The four suites are `datasource/test_delta.py`, `test_catalog.py`, `datasource/test_write_delta.py` and `unit/datasource/`, under `python/ray/data/tests/`. The last three are the other suites #67895 was tested with on master. `test_catalog.py` skips the whole module without `databricks-sdk`, and this branch's Data CI locks don't include it, so CI skips the file here. I ran it with `databricks-sdk` 0.143.0, in its own pytest process as Bazel would. Sharing a process with the other suites makes 10 of its tests, the ones that start their own Ray cluster, error with "Maybe you called ray.init twice". That happens on `releases/2.60.1` too. | Command | Result | |---|---| | `pre-commit run` on the 2 changed files | Passed | | `pytest datasource/test_delta.py datasource/test_write_delta.py unit/datasource/`, deltalake 1.5.0 | 202 passed. On `releases/2.60.1` without this PR: 199 passed | | `pytest test_catalog.py`, deltalake 1.5.0, databricks-sdk 0.143.0 | 34 passed, the same as on `releases/2.60.1` without this PR | | `pytest datasource/test_delta.py`, deltalake 1.5.0 and 1.6.6 | 18 passed on each. Without this PR: 15 passed | | The 3 new tests (`-k column_mapping`) with `releases/2.60.1`'s `read_api.py`, deltalake 1.5.0 and 1.6.6 | On each, `test_delta_read_rejects_column_mapping[name]` and `[id]` fail and `test_delta_read_column_mapping_mode_none` passes | The environment was Python 3.12.12 with the Ray 2.60.0 release wheel, which was built from `releases/2.60.0` at b7f4259. This branch's `python/ray/data` and `python/ray/tests` were copied over it. The other packages were pinned to the CI lock: pyarrow 23.0.1, pandas 2.3.3, numpy 2.2.6, fsspec 2023.12.1, and deltalake 1.5.0, or 1.6.6 where noted. `databricks-sdk` 0.143.0 was added for `test_catalog.py`. Building Ray from source for `bazel test` wasn't practical on my machine. An earlier version of this description counted `test_catalog.py` among the suites run. My run had skipped it because `databricks-sdk` wasn't installed. AI assistance was used to prepare this cherry-pick. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Signed-off-by: Abhishek Verma <[email protected]> Signed-off-by: Sai Miduthuri <[email protected]> Co-authored-by: abhishekverma-ray <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
…mn mapping in write_delta (#67898) (#68231) ## Description Cherry-pick of #67898 (master commit 1b7a10c) onto `releases/2.60.1`. This was first stacked on #68213 (the #67895 pick), because #67898 moves the `_write_column_mapped_table` test helper that #67895 added to `test_delta.py` into a shared `ray/data/tests/delta_test_utils.py`. #68213 and #68214 are merged now, so this PR is rebased onto `releases/2.60.1`. The rebase had no conflicts. `write_delta` writes Parquet files with the table's column names. A column-mapped Delta table (`delta.columnMapping.mode` = `name` or `id`) stores columns under physical names, so engines that apply the mapping (Databricks, Spark) read every column of those files as null. deltalake 1.6.6 commits such files; 1.5.0 refuses only after the data files are written. #67898 makes `write_delta` raise `NotImplementedError` for an existing table with column mapping, for both `APPEND` and `OVERWRITE`. It checks in `on_write_start` (nothing is written) and again at commit (`on_write_start` doesn't run for an empty dataset, and an empty `OVERWRITE` still commits). Tables with `mode = none` and new tables write as before. ## Related issues Related to #67898 and #67895. Column mapping support is tracked in #64854. ## Additional information ### Cherry-pick details `git cherry-pick -x` on top of #68213. `delta_datasink.py`, `test_write_delta.py` and the new `delta_test_utils.py` applied cleanly. `test_delta.py` conflicted only on context: on master the removed helper sits next to `test_delta_read_deletion_vectors_message_is_actionable`, which isn't on this branch. I removed the helper and didn't add that test. The result matches the master commit: the same 4 files, +164/-68. Not a duplicate: no open or merged PR against `releases/2.60.1` picks #67898. ### Testing | Command | Result | |---|---| | pre-commit (commit hooks) on the 4 changed files | Passed | | `pytest datasource/test_write_delta.py` | 105 passed. On #68213 without this PR: 101 | | `pytest datasource/test_delta.py` | 30 passed (18 from before #68214, plus #68214's new tests) | | `pytest test_catalog.py` (databricks-sdk 0.143.0) | 34 passed | | `pytest unit/datasource/` | 83 passed | | The 4 new tests (`-k column_map`) with #68213's `delta_datasink.py` | 3 fail; the `mode = none` case passes, as it should | The four `pytest` rows were rerun after the rebase onto `releases/2.60.1` (375b268). The revert check ran before the rebase, and the rebase didn't change this PR's diff. Each file ran in its own pytest process. Environment: Python 3.12.10 with the Ray 2.60.0 release wheel, this branch's `python/ray/data` and `python/ray/tests` linked over it, and the CI lock pins: pyarrow 23.0.1, pandas 2.3.3, numpy 2.2.6, deltalake 1.5.0, pytest 7.4.4. AI assistance was used to prepare this cherry-pick; 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: Claude Opus 5.5 <[email protected]>
…dentials to the environment (#67899) (#68232) ## Description Backport of #67899 (master commit 596241a) onto `releases/2.60.1`, **AWS part only**. It's based directly on `releases/2.60.1` and is independent of #68213 / #68214 and the #67898 pick; all of them merge with it without conflicts. On this branch, `DatabricksUnityCatalog.resolve()` writes vended AWS credentials into the driver's `os.environ` and, if Ray isn't running yet, starts Ray with them in `runtime_env`. AWS env vars take precedence over the instance role, so after one Unity Catalog read the driver and every worker use a short-lived credential scoped to that one table. Writes to the job's own buckets (checkpoints, logs) then fail with `ACCESS_DENIED`. With this PR, AWS credentials travel only in the objects that use them, which pickle into the read and write tasks: - An explicit `S3FileSystem` for every reader and mode. Before, AWS Parquet reads used the environment. - For Delta, the AWS keys and region in `storage_options`, for deltalake's log read and the `write_delta` commit. `read_delta`, `write_delta` and the datasink's credential refresh already merge `resolved.storage_options` on this branch. GCS was already environment-free and is unchanged. A response with no known credential type still raises `ValueError`. Behavior changes worth a release note (the same as on master): - AWS Parquet reads through Unity Catalog now need `region`, which the `DatabricksUnityCatalog` docstring already lists as required for AWS. - Code that relied on the vended AWS keys being in the environment afterwards no longer gets them. ## Related issues Related to #67899. ## Additional information ### How this differs from #67899 `releases/2.60.1` doesn't have the Azure Unity Catalog work #67899 edits (#66272–#66276: Azure SAS in `storage_options`, `AzureFileSystem` for Parquet, Azurite tests). So `git cherry-pick` conflicts in `catalog.py` and `test_catalog.py` and tries to bring in that Azure code and the Gravitino catalog. I ported the AWS change by hand instead: - **Azure is unchanged.** It still gets the SAS through `_apply_env` (the environment plus `runtime_env`), because nothing else delivers it on this branch. Removing that would break Azure reads. `_creds_to_env` is gone: Azure calls `_parse_azure_creds` directly, and the unknown-type `ValueError` moved into `_resolve_storage`, as on master. - **Not taken:** master's `delta_datasink.py` and `test_write_delta.py` changes. They're comment-only and say Unity Catalog never touches the environment, which is still false for Azure here. `test_catalog_azure.py` doesn't exist on this branch. - **Tests:** master's strict `isolated_env` fixture (environment must come back unchanged, `ray.init` fails if called) covers every AWS and GCS `resolve()` test. The two Azure tests keep the old lenient fixture, renamed `azure_env`. Master deleted `test_resolve_initializes_ray_with_runtime_env`; here it becomes `test_resolve_azure_initializes_ray_with_runtime_env`, since Azure still does that. As on master, `test_resolve_aws_requires_region` covers both readers, and `test_resolve_rejects_unknown_credential_type` and the `storage_options` asserts in the `write_delta` test are added. Not a duplicate: no open or merged PR against `releases/2.60.1` picks #67899. ### Testing `test_catalog.py` skips the whole module without `databricks-sdk`, and this branch's Data CI locks don't include it, so **CI doesn't run these tests here**. I ran them locally: | Command | Result | |---|---| | pre-commit (commit hooks) on the 2 changed files | Passed | | `pytest test_catalog.py` (databricks-sdk 0.143.0) | 36 passed. On `releases/2.60.1`: 34 | | The same with `releases/2.60.1`'s `catalog.py` | 8 failed and 7 fixture errors: every AWS `resolve()` test, the Parquet region check, and the `write_delta` `storage_options` asserts | | `pytest datasource/test_write_delta.py` | 101 passed | | `pytest datasource/test_delta.py` | 15 passed | | `pytest unit/datasource/` | 83 passed | Each file ran in its own pytest process. Environment: Python 3.12.10 with the Ray 2.60.0 release wheel, this branch's `python/ray/data` and `python/ray/tests` linked over it, and the CI lock pins: pyarrow 23.0.1, pandas 2.3.3, numpy 2.2.6, deltalake 1.5.0, pytest 7.4.4. Not checked on a real Databricks workspace, as with #67899 on master. AI assistance was used to prepare this backport; 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: Claude Opus 5.5 <[email protected]>
Description
Cherry-pick of #67900 (master commit 63ff72c) onto
releases/2.60.1.Top of a 2-PR stack on
releases/2.60.1: #68213 (the #67895 pick), then this PR. Merging this PR withgh stack mergelands both in order.#67900 makes two changes to
read_delta:partition_filters. This new keyword-only parameter takes deltalake's(column, op, value)tuples. A flat list is an AND, and a list of lists is an OR of ANDs. Only the matching partitions' files become fragments, so only they are listed and read. Every column must be a partition column. An OR of groups needs deltalake 1.6.6 and raises a clear error on older versions.read_unity_catalogpasses it throughreader_kwargs. The default,None, keeps the current behavior.ParquetDatasource.from_pyarrow_datasettakes them asknown_file_sizesand callsget_file_infoonly for paths missing from it. Before, it made oneget_file_inforequest per file. On the 26,085-file table in [Data] Add partition_filters to read_delta and take file sizes from the log #67900, that was 26,085 requests before a read started.read_deltais the only caller offrom_pyarrow_dataseton this branch.Related issues
Related to #67900.
Additional information
Cherry-pick details
The #67900 commit applied cleanly with
git cherry-pick -xon top of the #67895 pick. It has the same 3 files, +303/-3, and the samegit patch-id --stableas the master commit. It needs #68213 because itsread_api.pyhunk sits right after the column-mapping check that #67895 adds.A second commit adds
from unittest import mocktotest_delta.py.test_read_unity_catalog_partition_filters, which #67900 adds, usesmock.patch.object. On master the import comes from #66275, which isn't on this branch, so ruff reportsF405and the test would fail with aNameError. The import sits where master has it.Not a duplicate: neither
releases/2.60.1nor any open or merged PR against it contains #67900. No open PR on any branch picks it.Testing
The four suites are
datasource/test_delta.py,test_catalog.py,datasource/test_write_delta.pyandunit/datasource/, underpython/ray/data/tests/. They're the suites #67900 was tested with on master.test_catalog.pyskips the whole module withoutdatabricks-sdk, and this branch's Data CI locks don't include it, so CI skips the file here. I ran it withdatabricks-sdk0.143.0, in its own pytest process as Bazel would. Sharing a process with the other suites makes 10 of its tests, the ones that start their own Ray cluster, error with "Maybe you called ray.init twice". That happens onreleases/2.60.1too. The newread_unity_catalogtest is intest_delta.py, so it runs either way.pre-commit runon the 3 changed filesmockimport. Without it, ruff fails withF405onmockpytest datasource/test_delta.py datasource/test_write_delta.py unit/datasource/, deltalake 1.5.0pytest test_catalog.py, deltalake 1.5.0, databricks-sdk 0.143.0releases/2.60.1pytest datasource/test_delta.py, deltalake 1.5.0 and 1.6.6test_delta_read_partition_filters_or_groupscovers both deltalake paths. It reads an OR of groups throughfile_pruning_predicateon 1.6.6, and checks the "needs deltalake 1.6.6 or later" error on 1.5.0.The environment was Python 3.12.12 with the Ray 2.60.0 release wheel, which was built from
releases/2.60.0at b7f4259. This branch'spython/ray/dataandpython/ray/testswere copied over it. The other packages were pinned to the CI lock: pyarrow 23.0.1, pandas 2.3.3, numpy 2.2.6, fsspec 2023.12.1, and deltalake 1.5.0, or 1.6.6 where noted.databricks-sdk0.143.0 was added fortest_catalog.py. Building Ray from source forbazel testwasn't practical on my machine.An earlier version of this description counted
test_catalog.pyamong the suites run. My run had skipped it becausedatabricks-sdkwasn't installed.AI assistance was used to prepare this cherry-pick.
🤖 Generated with Claude Code