Skip to content

[Data][Delta DSv2][5/11] Skip Delta data files using the statistics in the log - #68284

Open
abhishekverma-ray wants to merge 3 commits into
masterfrom
abhishek/delta-dsv2-5-log-pruning
Open

abhishekverma-ray wants to merge 3 commits into
masterfrom
abhishek/delta-dsv2-5-log-pruning

Conversation

@abhishekverma-ray

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

Copy link
Copy Markdown
Contributor

Summary

The DataSourceV2 Delta reader now leaves out data files whose per-file statistics in the Delta log rule out the read's predicate, before their footers are read (#61547).

Replaces #67677, 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

Every data file of the pinned version is listed and its footer read; only row groups are pruned, from the footers.

After

DeltaFileIndexer evaluates the pushed-down predicate against each add action's min.<col> / max.<col> and lists only the files that may match. Skipping is conservative: anything unprovable keeps the file, and the reader still filters every row. Only bounds that are exact for their type, or made so, are used — string maximums can be truncated by writers; timestamp statistics are stored in milliseconds (Spark and Databricks cut off the rest), so timestamp bounds are widened by 1 ms, as V1 widens the maximum; float bounds can be NaN; and deltalake writes decimal statistics through float64, so they are used only for decimals of at most 15 digits — which the original pruning in #65142 didn't account for. Supports <, <=, >, >=, == (literal on either side), is_in, &, |.

flowchart LR
    A["add actions @ pinned version"] --> P["statistics_keep_mask(predicate)"]
    P --> F["footer reads (kept files only)"]
    F --> B["OnlineBinPacker"]
    classDef added fill:#c8e6c9,stroke:#2e7d32,color:#000
    class P added
Loading

Subsystems

IO (_internal/datasource_v2/formats/delta/).

Testing

  • test_delta_file_pruning.py — 53 passed, including a randomized check that a file with a matching row is never dropped, the 15-digit decimal boundary (15 used; 16 and 38 not), and millisecond timestamp statistics for timestamp and timestamp_ntz: a file logged at 12:00:00.000 with a row at 12:00:00.000500 is kept for ts > 12:00:00.000100 (fails without the margin) and skipped for ts > 12:00:00.002 (fails with timestamp statistics off).
  • test_millisecond_timestamp_statistics_keep_matching_rows rewrites a real log's timestamp statistics to milliseconds and reads the 12:00:00.000500 row back through read_delta; it fails without the margin.
  • test_delta_file_indexer.py — listing skips exactly the ruled-out files.
  • test_delta_v2.py — 14 predicates on a Z-ordered-style table (int, string, date, float, and a decimal(38, 0) column beyond int64) return the same rows as an unfiltered read filtered in memory. It doesn't compare with V1, which skips files with the same statistics. The decimal case returned no rows before the 15-digit rule.
  • Each module in its own pytest process: test_delta.py 54, test_delta_v2.py 50, test_delta_footer_listing.py 2, the Delta unit tests 99, all passed.
  • Removing each guard (string max, the timestamp margin, NaN, decimal precision, unknown stats, OR with an unprovable side, literal-on-left flip, EQ bounds, the indexer's use of the predicate) fails a test.
  • pre-commit, pyrefly (the new module joins the pyarrow.compute exclusions), API param coverage pass.

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

🤖 Generated with Claude Code

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

@abhishekverma-ray abhishekverma-ray added data Ray Data-related issues go add ONLY when ready to merge, run all tests labels Nov 18, 2026
@abhishekverma-ray
abhishekverma-ray force-pushed the abhishek/delta-dsv2-5-log-pruning branch from a307942 to 086e6d9 Compare November 18, 2026 04:36
value_type = values.type
if (
pa.types.is_integer(value_type)
or pa.types.is_decimal(value_type)

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.

Decimal statistics in the log aren't exact bounds: deltalake's writer rounds them, so the file is skipped.

write_deltalake(path, pa.table({"d": pa.array([Decimal(12345678901234567891)], pa.decimal128(38, 0))}))
# log: min.d = max.d = 9223372036854775807
ray.data.read_delta(path).filter(expr=col("d") == Decimal(12345678901234567891)).take_all()
# []   (read_parquet on the same file returns the row)

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.

Thanks, good catch: the skip was wrong. deltalake writes decimal statistics through float64. I checked what it writes on 1.5.0:

column real max max in the log
decimal(10, 2), (15, 0) exact exact
decimal(17, 0) 12345678901234567 12345678901234568
decimal(18, 4) 12345678901234.5678 12345678901234.568
decimal(20, 0) and (38, 0) 12345678901234567891 9223372036854775807 (int64 max)

(A decimal(16, 0) column holding 9999999999999999 even fails the deltalake write: the rounded statistic overflows the type.)

So decimal bounds are now used only when the column has at most 15 digits. float64 holds every number of up to 15 digits exactly, so those statistics are true bounds. For wider decimals the file is kept, and the reader's filter decides, as for timestamps. I chose that cut-off over dropping decimal skipping entirely: money-style columns such as decimal(10, 2) or (15, 2) are the common case, and their statistics are exact. Integer statistics don't go through float64 (2**62 + 1 comes back exact), so they stay.

Your example now returns the row. The V1 reader skips the same file, so this is a V1 bug too; V2 no longer has it.

],
ids=str,
)
def test_log_statistics_pruning_matches_v1(clustered_table, predicate):

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.

V1 skips files with the same log statistics, so matching V1 can't catch a wrong skip (the decimal case returns [] on both). Compare with the unfiltered read, filtered in memory. Also, test_int_statistics needs one literal-on-left case and one is_in case, not three and two.

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.

Agreed: V1 makes the same skip, so "matches V1" passed while both returned []. test_log_statistics_pruning_keeps_every_matching_row now compares with the rows that a filter on the materialized table keeps, where nothing is pushed into the read. Its table gained a decimal(38, 0) column beyond int64, and the new col("big") == ... case fails without the fix. A unit test pins the 15-digit boundary (15 used, 16 and 38 not).

test_int_statistics keeps one literal-on-the-left case and one is_in case.

@abhishekverma-ray
abhishekverma-ray force-pushed the abhishek/delta-dsv2-5-log-pruning branch from 086e6d9 to deb32d0 Compare November 19, 2026 03:48

@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 deb32d0. Configure here.

or pa.types.is_date(value_type)
or pa.types.is_boolean(value_type)
):
return values

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Decimal stats skip matching files

High Severity

_bound treats decimal min/max as exact, but Delta writers round or clamp those statistics (often through f64 or i64). A file whose real values sit outside the logged range can be dropped before the reader sees it, so matching rows never come back.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit deb32d0. Configure here.

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.

Fixed with the thread above: decimal statistics are used only for columns of at most 15 digits, which float64 holds exactly.

@abhishekverma-ray
abhishekverma-ray removed this pull request from stack #68288 November 19, 2026 04:25
@abhishekverma-ray abhishekverma-ray changed the title [Data][Delta DSv2][5/8] Skip Delta data files using the statistics in the log [Data][Delta DSv2][5/9] Skip Delta data files using the statistics in the log Nov 19, 2026
@abhishekverma-ray
abhishekverma-ray force-pushed the abhishek/delta-dsv2-5-log-pruning branch from deb32d0 to 74e27c7 Compare November 19, 2026 04:49
@abhishekverma-ray
abhishekverma-ray force-pushed the abhishek/delta-dsv2-5-log-pruning branch from 74e27c7 to 7abd9db 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][5/9] Skip Delta data files using the statistics in the log [Data][Delta DSv2][5/11] Skip Delta data files using the statistics in the log Nov 20, 2026
@abhishekverma-ray
abhishekverma-ray force-pushed the abhishek/delta-dsv2-5-log-pruning branch 2 times, most recently from 743e3ed to 7139fd1 Compare November 23, 2026 10:26
@sampan-s-nayak
sampan-s-nayak force-pushed the abhishek/delta-dsv2-5-log-pruning branch from 7139fd1 to d55312d Compare November 23, 2026 11:13
@abhishekverma-ray
abhishekverma-ray force-pushed the abhishek/delta-dsv2-5-log-pruning branch 2 times, most recently from e966f0d to 65a8ac2 Compare November 24, 2026 03:09
@abhishekverma-ray
abhishekverma-ray force-pushed the abhishek/delta-dsv2-5-log-pruning branch from 67f72fd to 154f6ea Compare November 25, 2026 02:54

Not every statistic is a true bound. Writers truncate long strings, which can
leave ``max`` below the real maximum (a truncated ``min`` is still a lower
bound), and Delta stores timestamps in milliseconds, so either bound can be

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.

Why are timestamp columns excluded? V1 skips files on them and still returns 12:00:00.000500 for ts > 12:00:00.000100, though the log stores it as 12:00:00.000.

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.

Good point, there was no reason to drop them entirely. Fixed in 9387c58: timestamp statistics are used again, with each bound widened by 1 ms (the maximum up, the minimum down).

  • Spark and Databricks log timestamps cut to milliseconds, so your file is logged with a maximum of 12:00:00.000. Widened to 12:00:00.001, it's kept for ts > 12:00:00.000100, as V1 keeps it.
  • V1 adds the 1 ms only when the logged maximum has no sub-millisecond digits. Here it's added always, and the minimum gets it too, so the bounds hold whichever way a writer cuts or rounds. That costs at most 1 ms of range per file.
  • A timestamp unit other than ms/us/ns, or a bound within 1 ms of the end of the range, keeps the file.

Tests:

  • test_timestamp_statistics_get_a_millisecond_margin runs your case for timestamp and timestamp_ntz. It fails without the margin (the matching file is skipped), and with timestamp statistics off (ts > 12:00:00.002 no longer skips anything).
  • test_millisecond_timestamp_statistics_keep_matching_rows rewrites a real log's statistics to milliseconds and reads the 12:00:00.000500 row back through read_delta. It fails without the margin.

Base automatically changed from abhishek/delta-dsv2-4-read-delta to master November 27, 2026 18:12
abhishekverma-ray and others added 3 commits November 27, 2026 10:12
The DataSourceV2 Delta reader lists every data file of the pinned
version and reads every footer. Each `add` action in the log carries the
file's per-column minimum and maximum, so a file whose range proves no
row can match the read's predicate is now left out of the listing,
before its footer is read (#61547).

Skipping is conservative: anything unprovable keeps the file, and the
reader still applies the predicate to every row. Only bounds that are
exact for their type are used: string maximums can be truncated by the
writer, timestamp statistics are stored in milliseconds, and float
bounds may be NaN. Supports comparisons with a literal on either side,
IN, AND and OR.

Co-authored-by: tobby168 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Abhishek Verma <[email protected]>
deltalake writes decimal min/max statistics through float64. A decimal of
more than 15 digits comes back rounded, and one beyond the int64 range is
clamped to it, so the logged range can miss the file's real values and the
file was skipped although it held matching rows (a decimal(38, 0) equality
filter returned no rows). Use decimal statistics only when the column has
at most 15 digits, which float64 holds exactly.

Tests:
- The end-to-end pruning test compares with a filter applied to the
  materialized table instead of with the V1 reader, which skips files with
  the same statistics and so can't catch a wrong skip. Its table gains a
  decimal(38, 0) column beyond int64, which returned no rows before.
- A unit test pins the 15-digit boundary; the exact-type test drops its
  decimal case, now covered there.
- test_int_statistics keeps one literal-on-the-left case and one is_in case.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Abhishek Verma <[email protected]>
… a 1 ms margin

Timestamp statistics were not used at all, because Spark and Databricks log
them cut to milliseconds: a file whose only row is at 12:00:00.000500 can be
logged with a maximum of 12:00:00.000, and trusting that maximum skips the
file for `ts > 12:00:00.000100`.

Use them again with each bound widened by 1 ms (the maximum up, the minimum
down), which makes them true bounds whichever way a writer cuts. The V1
reader (DeltaTable.to_pyarrow_dataset) also adds 1 ms to the maximum.
Time-filtered reads of event tables skip files again.

Tests: the unit test runs that case for timestamp and timestamp_ntz; it
fails both without the margin (the matching file is skipped) and with
timestamp statistics off (nothing is skipped). An end-to-end test rewrites a
real log's statistics to milliseconds and reads the 12:00:00.000500 row back;
it fails without the margin.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Abhishek Verma <[email protected]>
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]>
@goutamvenkat-anyscale
goutamvenkat-anyscale force-pushed the abhishek/delta-dsv2-5-log-pruning branch from 9387c58 to 6d7892f 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.

2 participants