Skip to content

[Data][Delta DSv2][7/11] Apply partition_filters in the Delta DataSourceV2 reader - #68330

Open
abhishekverma-ray wants to merge 2 commits into
abhishek/delta-dsv2-6-checkpointfrom
abhishek/delta-dsv2-6b-partition-filters
Open

abhishekverma-ray wants to merge 2 commits into
abhishek/delta-dsv2-6-checkpointfrom
abhishek/delta-dsv2-6b-partition-filters

Conversation

@abhishekverma-ray

@abhishekverma-ray abhishekverma-ray commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Summary

read_delta sends reads that pass partition_filters to the V1 reader (#68283), because the DataSourceV2 reader didn't apply them. This layer applies them in V2, so these reads stay on V2 once #68286 makes it the default.

This PR is part of a GitHub stack, so its diff shows only this layer.

Before

A read with partition_filters used the V1 reader, even with the DataSourceV2 reader switched on.

After

  • DeltaTableSnapshot carries the filters, and its add_actions() keeps only the files they select. The row-group listing, the whole-file listing (count()) and the log-statistics pruning ([Data][Delta DSv2][5/11] Skip Delta data files using the statistics in the log #68284) all start from that list.
  • deltalake selects the files with DeltaTable.file_uris(...): file_pruning_predicate= on 1.6.6 and later, the positional filters before. That's the same selection to_pyarrow_dataset makes for the V1 reader, so both readers read the same files, with the same rules ('' matches a null partition; values compare by the column's type).
  • file_uris returns URIs. The listing maps them back to the log's paths through one prefix, which it checks against every file of the version by looking each URI's suffix up in the set of log paths. A URI form it doesn't expect fails the read, rather than dropping files.
  • read_delta checks the filters on the driver with V1's checks, so invalid filters still fail when it's called. OR groups still need deltalake 1.6.6, as for V1.
flowchart LR
    subgraph IO["Subsystem: IO"]
        subgraph RA["Component: read_delta (read_api.py)"]
            R["partition_filters checked as for V1,<br/>passed to DeltaDatasourceV2"]
        end
        subgraph D["Component: datasource_v2/formats/delta"]
            S["DeltaTableSnapshot.add_actions():<br/>files deltalake selects (file_uris)"] --> L["listing tasks (row groups, count,<br/>log-statistics pruning)"]
        end
        R --> S
    end
    classDef changed fill:#c8e6c9,stroke:#2e7d32,color:#000
    class R,S changed
Loading

Subsystems

IO (_internal/datasource_v2/formats/delta/, read_delta in read_api.py).

Not a duplicate

Part of the Delta DataSourceV2 stack. No other open PR applies partition_filters in the Delta V2 reader.

Testing

Ray nightly of Nov 18 with CI pins (pyarrow 23.0.1, pandas 2.3.3, deltalake 1.5.0):

  • pytest over test_delta.py, test_delta_v2.py and the Delta unit tests: 220 passed. test_delta.py's partition_filters tests (=, in, a range, AND, OR groups, invalid filters, read_unity_catalog), which [Data][Delta DSv2][4/11] Read Delta Lake tables through DataSourceV2 (opt-in) #68283 runs with V1 only, now also run through V2. Their file-list helper runs the V2 listing, because input_files() is empty on DSv2 until the read runs. test_partition_filters_match_v1 compares V2 with V1 on special-character values, in, the null partition and an AND with an integer comparison.
  • With the listing ignoring the filters, all 9 V2 cases above fail.
  • tests/unit/datasource_v2/ and tests/unit/datasource/: 1733 passed (I skipped test_text_datasource_v2.py, which needs snappy, not installed locally). test_partition_filters_fail_on_uris_that_dont_match_the_log checks the read fails instead of dropping files.
  • On moto S3 (a manual check, not a test): a table under a path with a space and partition values with special characters returns the same rows from V2 and V1, through deltalake's filesystem and through a caller's SubTreeFileSystem.
  • At the top of the stack ([Data][Delta DSv2][11/11] Read Delta Lake tables with v2 checkpoints through DataSourceV2 #68287): test_delta.py, test_delta_v2.py and test_write_delta.py, 228 passed; unit 1605 passed.
  • pre-commit passes. Locally pyrefly reports missing packaging and requests imports on unchanged lines of test_delta_v2.py; the CI environment has them.

AI assistance was used (Claude Code); every line has been reviewed by the submitter.

🤖 Generated with Claude Code

@abhishekverma-ray
abhishekverma-ray marked this pull request as ready for review November 19, 2026 04:28
@abhishekverma-ray
abhishekverma-ray requested review from a team as code owners November 19, 2026 04:28
@abhishekverma-ray abhishekverma-ray added data Ray Data-related issues go add ONLY when ready to merge, run all tests labels Nov 19, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request implements support for partition_filters in the Delta DataSourceV2 reader, removing the previous fallback to the V1 reader when filters were applied. It updates DeltaDatasourceV2 and DeltaTableSnapshot to filter data files using deltalake's file selection, and updates corresponding tests to verify matching behavior. Feedback on the changes suggests optimizing the file path prefix matching logic in _files_selected_by_partition_filters to avoid inefficient string concatenations and set comparisons on large tables.

Comment on lines +167 to +172
expected = set(all_uris)
for path in file_paths:
if all_uris[0].endswith("/" + path):
prefix = all_uris[0][: -len(path)]
if {prefix + p for p in file_paths} == expected:
return {uri[len(prefix) :] for uri in selected_uris}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Constructing a new set of concatenated strings {prefix + p for p in file_paths} and comparing it to expected can be highly inefficient and memory-intensive for large tables with many files.

We can optimize this by checking lengths and using a set of file_paths for $O(1)$ lookups, avoiding $O(N)$ string concatenations entirely.

    for path in file_paths:
        if all_uris[0].endswith("/" + path):
            prefix = all_uris[0][: -len(path)]
            file_paths_set = set(file_paths)
            if len(all_uris) == len(file_paths) and all(
                uri.startswith(prefix) and uri[len(prefix) :] in file_paths_set
                for uri in all_uris
            ):
                return {uri[len(prefix) :] for uri in selected_uris}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Taken: the check now looks each URI's suffix up in the set of log paths instead of building a second set of N joined strings. Same result; the existing partition_filters tests, including the one where the URIs don't match the log, cover it.

@cursor cursor Bot 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.

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 48acb08. Configure here.

@abhishekverma-ray
abhishekverma-ray force-pushed the abhishek/delta-dsv2-6b-partition-filters branch from 48acb08 to 7312cf2 Compare November 19, 2026 04:49
@abhishekverma-ray
abhishekverma-ray force-pushed the abhishek/delta-dsv2-6b-partition-filters branch from 7312cf2 to 197a188 Compare November 20, 2026 17:28
@abhishekverma-ray
abhishekverma-ray removed this pull request from stack #68331 November 20, 2026 17:29
@abhishekverma-ray abhishekverma-ray changed the title [Data][Delta DSv2][7/9] Apply partition_filters in the Delta DataSourceV2 reader [Data][Delta DSv2][7/11] Apply partition_filters in the Delta DataSourceV2 reader Nov 20, 2026
@abhishekverma-ray
abhishekverma-ray force-pushed the abhishek/delta-dsv2-6b-partition-filters branch from 197a188 to daed759 Compare November 23, 2026 10:15
@abhishekverma-ray
abhishekverma-ray force-pushed the abhishek/delta-dsv2-6b-partition-filters branch from daed759 to b13c6fe Compare November 23, 2026 10:26
@sampan-s-nayak
sampan-s-nayak force-pushed the abhishek/delta-dsv2-6b-partition-filters branch from b13c6fe to f10a76d Compare November 23, 2026 11:13
@abhishekverma-ray
abhishekverma-ray force-pushed the abhishek/delta-dsv2-6b-partition-filters branch from f10a76d to afc893f Compare November 23, 2026 14:41
@abhishekverma-ray
abhishekverma-ray force-pushed the abhishek/delta-dsv2-6b-partition-filters branch from afc893f to a5fcd2b Compare November 24, 2026 03:09
@abhishekverma-ray
abhishekverma-ray force-pushed the abhishek/delta-dsv2-6b-partition-filters branch from a5fcd2b to 6cd0106 Compare November 24, 2026 05:57
@abhishekverma-ray
abhishekverma-ray force-pushed the abhishek/delta-dsv2-6b-partition-filters branch from 6cd0106 to 26a5bcf Compare November 25, 2026 02:54
@abhishekverma-ray
abhishekverma-ray force-pushed the abhishek/delta-dsv2-6b-partition-filters branch from 26a5bcf to 971c548 Compare November 26, 2026 03:34
goutamvenkat-anyscale pushed a commit that referenced this pull request Nov 27, 2026
…(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]>
abhishekverma-ray and others added 2 commits November 27, 2026 10:12
read_delta sent reads that pass partition_filters to the V1 reader,
because the DataSourceV2 reader didn't apply them. Now the V2 listing
reads only the data files they select. deltalake selects them with
DeltaTable.file_uris, the same selection to_pyarrow_dataset makes for
the V1 reader, so both readers read the same files. The URIs are mapped
back to the log's paths through one prefix, checked against every file,
so a URI form the listing doesn't expect fails the read instead of
dropping files. read_delta checks the filters on the driver as for V1,
so invalid ones still fail when it's called.

test_delta.py's partition_filters tests now run through V2 for the v2
reader. Their file-list helper runs the V2 listing, which happens in a
Ray task when the read runs.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Abhishek Verma <[email protected]>
…w strings

Checking that every URI deltalake returns is one prefix followed by a log
path built a second set of N joined strings. Look each URI's suffix up in the
set of log paths instead. Same result; the existing partition_filters tests,
including the one where the URIs don't match the log, cover it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Abhishek Verma <[email protected]>
@goutamvenkat-anyscale
goutamvenkat-anyscale force-pushed the abhishek/delta-dsv2-6b-partition-filters branch from 971c548 to 41fe40f Compare November 27, 2026 18:12

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

data Ray Data-related issues go add ONLY when ready to merge, run all tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant