Skip to content

[Data] DSv2 Reorg [2/6]: Make datasource_v2 independent of the legacy datasource package - #66357

Merged
goutamvenkat-anyscale merged 4 commits into
ray-project:masterfrom
AarryaSaraf:dsv2-drop-legacy-imports
Sep 22, 2026
Merged

goutamvenkat-anyscale merged 4 commits into
ray-project:masterfrom
AarryaSaraf:dsv2-drop-legacy-imports

Conversation

@AarryaSaraf

Copy link
Copy Markdown
Contributor

Description

Third PR of the DSv2 Reorg series, on top of #66312 ([1.5/6]).

datasource_v2 still imports helpers from the legacy parquet_datasource.py.
This PR moves them into datasource_v2, points the legacy module at their new
homes, and adds a lint so the dependency only ever points one way: legacy may
depend on V2, never the reverse. No behavior change.

What moved where

  1. Eight Parquet-only helpers (legacy tensor check, nested-type fallback
    sizing, leaf-column resolution, row-group size, the Arrow chunk limit) move
    from the legacy module into new datasource_v2/parquet_utils.py.
  2. The predicate helpers combine_predicates and _split_predicate_by_columns
    move from the legacy module into datasource_v2/pushdown.py, which is
    logical_optimizers.py renamed because it now holds all pushdown code.
  3. _compute_row_hashes moves from the legacy module into
    datasource_v2/readers/file_reader.py.
  4. The legacy parquet_datasource.py imports all of the above back from V2.
  5. New lint ci/lint/check_datasource_v2_imports.py fails on any V2 import of
    the legacy package, module-level or function-level, wired into pre-commit
    and lint.sh.

Code changes summarized

  • The Parquet extension list moves to PARQUET_FILE_EXTENSIONS in
    parquet_utils.py; both ParquetDatasourceV2 and the legacy
    ParquetDatasource._FILE_EXTENSIONS read it from there.
  • check_for_legacy_tensor_type gained type annotations on the move.
  • parquet_file_reader.py imports the moved helpers at module level instead
    of inside the fallback method. The function-level import existed to dodge an
    import cycle through the legacy module and its twenty Ray imports;
    parquet_utils.py imports only typing and pyarrow, so the reason is
    gone. Import errors now surface at load time rather than on the first
    oversized nested-type file, and tests can patch the names on the reader
    module.
  • _compute_row_hashes lives in file_reader.py rather than
    parquet_utils.py because it is not Parquet code: it makes the row_hash
    column for every format, and the generic FileReader is its only V2 caller.
  • The legacy module's ray.data.expressions import shrinks from
    BinaryExpr, Expr, Operation to Expr. The two dropped names were used only
    by the moved _split_predicate_by_columns; Expr still annotates a legacy
    method parameter.
  • One test_parquet.py test patches the nested-type helper at both the legacy
    path and the reader path, since each reader now resolves it through its own
    module.

Additional information

Not a duplicate: no open PR moves these helpers or adds an import boundary for
datasource_v2. #65743 (CSV on DatasourceV2) touches the package but not
these modules.

Checks run locally from this branch (macOS, ~/ray/.venv):

python ci/lint/check_datasource_v2_imports.py                                  # clean
pytest -q tests/unit/datasource_v2 tests/unit/test_parquet_predicate_split.py  # 159 passed
pytest -q tests/datasource/test_read_parquet_v2.py                             # 37 passed
pytest -q tests/test_execution_optimizer_limit_pushdown.py                     # 28 passed

ruff, black and pydoclint at the pre-commit pins are clean; pyrefly
reports the same 60 pre-existing errors as master.

Written with Claude Code; I reviewed every changed line and ran the tests above.

🤖 Generated with Claude Code

@AarryaSaraf AarryaSaraf added the data Ray Data-related issues label Sep 21, 2026
… datasource package

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Signed-off-by: Aarrya <[email protected]>
@AarryaSaraf
AarryaSaraf force-pushed the dsv2-drop-legacy-imports branch from 193e601 to 6dbb9d7 Compare September 21, 2026 20:41
@AarryaSaraf
AarryaSaraf marked this pull request as ready for review September 21, 2026 22:38
@AarryaSaraf
AarryaSaraf requested a review from a team as a code owner September 21, 2026 22:38

@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 refactors and decouples the V2 Parquet datasource from the legacy implementation by moving shared Parquet helper functions, predicate splitting logic, and row hashing utilities into dedicated modules under datasource_v2 (such as parquet_utils.py, pushdown.py, and file_reader.py). Imports and tests have been updated accordingly to prevent import cycles. The review feedback highlights two important improvements: adding a defensive check in _needs_nested_type_fallback to handle cases where fragment.metadata is None and avoiding an AttributeError, and copying the mutable PARQUET_FILE_EXTENSIONS list to prevent unintended side effects from shared references.

Comment on lines +185 to +190
metadata = fragment.metadata
column_indices = (
_resolve_leaf_column_indices(metadata, columns)
if columns is not None and metadata.num_row_groups > 0
else None
)

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.

high

In PyArrow, fragment.metadata can be None if the fragment was not initialized with metadata. If metadata is None, accessing metadata.num_row_groups will raise an AttributeError. We should add a defensive check to return False early if metadata is None.

Suggested change
metadata = fragment.metadata
column_indices = (
_resolve_leaf_column_indices(metadata, columns)
if columns is not None and metadata.num_row_groups > 0
else None
)
metadata = fragment.metadata
if metadata is None:
return False
column_indices = (
_resolve_leaf_column_indices(metadata, columns)
if columns is not None and metadata.num_row_groups > 0
else None
)

self._filesystem = resolved_filesystem
self._partitioning = partitioning
self._file_extensions = file_extensions or ParquetDatasource._FILE_EXTENSIONS
self._file_extensions = file_extensions or PARQUET_FILE_EXTENSIONS

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

PARQUET_FILE_EXTENSIONS is a module-level mutable list. Assigning it directly to self._file_extensions shares the same list reference across all instances. If any instance or external code mutates self._file_extensions, it will modify the global list. It is safer to copy the list using list(PARQUET_FILE_EXTENSIONS).

Suggested change
self._file_extensions = file_extensions or PARQUET_FILE_EXTENSIONS
self._file_extensions = file_extensions or list(PARQUET_FILE_EXTENSIONS)

@AarryaSaraf AarryaSaraf added the go add ONLY when ready to merge, run all tests label Sep 21, 2026
PartitionPredicatePruner,
)
from ray.data._internal.datasource_v2.logical_optimizers import (
from ray.data._internal.datasource_v2.pushdown import (

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.

let's not change this module. Leave it as logical optimizers

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.

Sure.


# Arrow's nested type chunking limit
# See: https://github.com/apache/arrow/issues/21526 (ARROW-5030)
_ARROW_CHUNK_LIMIT = 2 * 1024**3 # 2GB

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.

Use 2 * GiB

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.

This is a code move not a code change. Are we sure we want to use this constant and change from GB to GiB?

@goutamvenkat-anyscale
goutamvenkat-anyscale enabled auto-merge (squash) September 22, 2026 22:26
@goutamvenkat-anyscale
goutamvenkat-anyscale merged commit 8801261 into ray-project:master Sep 22, 2026
7 checks passed
dstrodtman added a commit that referenced this pull request Sep 24, 2026
## Description

Since #66357, `import ray.data` loads `pyarrow.dataset` and its native
extensions. The legacy `parquet_datasource.py` now imports
`_compute_row_hashes` from the V2 readers package at module level, and
the V2 reader modules import `pyarrow.dataset` at load time. Before
#66357, the dependency ran the other way and `ray.data` never reached
`pyarrow.dataset` on import.

This PR moves the `_compute_row_hashes` import into
`_read_batches_from`, its only caller. That matches the file's existing
function-level `pyarrow.dataset` imports. The import points at
`readers/synthesized_columns.py`, where the helper has lived since
#66390, rather than at the `file_reader` re-export. Importing from
`synthesized_columns` at module level isn't enough on its own, because
`read_units.py` also imports `pyarrow.dataset` at the top level.

It also adds `test_import_does_not_load_pyarrow_dataset` to
`python/ray/data/tests/test_import.py`. The test asserts that a fresh
driver's `import ray.data` loads no `pyarrow._dataset*` modules.

### Why this matters

The Ray docs Read the Docs build has been segfaulting intermittently, in
about 31% of builds since 2026-09-23. The crash is in native extension
init while autosummary imports RLlib → Tune → AIR → `ray.data`. A
`PYTHONFAULTHANDLER=1` traceback (#66452) puts the crash inside `from
pyarrow._dataset_parquet import ...`, which is reached from
`parquet_datasource.py` → `file_reader.py` → `import pyarrow.dataset`.
The loaded-extensions list in that traceback ends with exactly the
modules this import adds.

This change takes the crashing import off the `import ray.data` path. It
doesn't fix the underlying native crash. I couldn't reproduce it with a
bare `import pyarrow.dataset` loop in `linux/amd64` Docker: 0 of 300
runs failed, with numpy 2.2.6 and pyarrow 23.0.1 from the doc lock. So
it seems to depend on process state or on the build host. The docs build
and `requirements_compiled.txt` both pin pyarrow 23.0.1, and neither pin
changed in the failure window.

## Related issues

Related to #66357 and #66452.

## Additional information

### Not a duplicate

Searched open PRs for `_compute_row_hashes`, the DSv2 reorg series, and
lazy-import and segfault keywords. #66450 (DSv2 Reorg [4/6]) and #66201
are the nearest open PRs, and neither touches this import or
`test_import.py`.

### Tests run

These were run locally on macOS arm64, Python 3.11, pyarrow 23.0.1,
numpy 2.2.6. They used the Ray nightly wheel with `ray/data` and
`ray/tests` linked from this branch.

* `pytest python/ray/data/tests/test_import.py`: 2 passed. With the
`parquet_datasource.py` change reverted, the new test fails and lists
`pyarrow._dataset`, `_dataset_orc`, `_dataset_parquet`, and
`_dataset_parquet_encryption`.
* `pytest python/ray/data/tests/datasource/test_parquet.py -k row_hash`:
18 passed.
* `pytest python/ray/data/tests/datasource/test_parquet.py` (full
module): 242 passed, 13 skipped, 13 failed, 34 errors. Rerunning the 47
failing tests with master's `parquet_datasource.py` gave an identical
failing set, so none come from this change. They come from the local
environment:
* The S3 fixtures error in `fsspec`'s HTTP backend, because their test
dependencies aren't installed.
* Several local-path tests assert `string` columns and get
`large_string`, which is pandas/pyarrow version skew against CI's pins.

  CI is the authoritative run for this module.
* `pre-commit run --files
python/ray/data/_internal/datasource/parquet_datasource.py`: all hooks
passed, including ruff, black, pydoclint, and the import-order check.

### AI assistance

AI assistance (Claude Code) was used to investigate the import graph,
write the change and test, and draft this description. This PR stays in
draft until the submitter has reviewed every changed line.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Signed-off-by: Douglas Strodtman <[email protected]>
Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
nadongjun pushed a commit to nadongjun/ray that referenced this pull request Oct 6, 2026
… datasource package (ray-project#66357)

## Description

Third PR of the DSv2 Reorg series, on top of ray-project#66312 (`[1.5/6]`).

`datasource_v2` still imports helpers from the legacy
`parquet_datasource.py`.
This PR moves them into `datasource_v2`, points the legacy module at
their new
homes, and adds a lint so the dependency only ever points one way:
legacy may
depend on V2, never the reverse. No behavior change.

### What moved where

1. Eight Parquet-only helpers (legacy tensor check, nested-type fallback
sizing, leaf-column resolution, row-group size, the Arrow chunk limit)
move
   from the legacy module into new `datasource_v2/parquet_utils.py`.
2. The predicate helpers `combine_predicates` and
`_split_predicate_by_columns`
move from the legacy module into `datasource_v2/pushdown.py`, which is
`logical_optimizers.py` renamed because it now holds all pushdown code.
3. `_compute_row_hashes` moves from the legacy module into
   `datasource_v2/readers/file_reader.py`.
4. The legacy `parquet_datasource.py` imports all of the above back from
V2.
5. New lint `ci/lint/check_datasource_v2_imports.py` fails on any V2
import of
the legacy package, module-level or function-level, wired into
pre-commit
   and `lint.sh`.

### Code changes summarized

- The Parquet extension list moves to `PARQUET_FILE_EXTENSIONS` in
  `parquet_utils.py`; both `ParquetDatasourceV2` and the legacy
  `ParquetDatasource._FILE_EXTENSIONS` read it from there.
- `check_for_legacy_tensor_type` gained type annotations on the move.
- `parquet_file_reader.py` imports the moved helpers at module level
instead
of inside the fallback method. The function-level import existed to
dodge an
  import cycle through the legacy module and its twenty Ray imports;
`parquet_utils.py` imports only `typing` and `pyarrow`, so the reason is
  gone. Import errors now surface at load time rather than on the first
oversized nested-type file, and tests can patch the names on the reader
  module.
- `_compute_row_hashes` lives in `file_reader.py` rather than
`parquet_utils.py` because it is not Parquet code: it makes the
`row_hash`
column for every format, and the generic `FileReader` is its only V2
caller.
- The legacy module's `ray.data.expressions` import shrinks from
`BinaryExpr, Expr, Operation` to `Expr`. The two dropped names were used
only
by the moved `_split_predicate_by_columns`; `Expr` still annotates a
legacy
  method parameter.
- One `test_parquet.py` test patches the nested-type helper at both the
legacy
path and the reader path, since each reader now resolves it through its
own
  module.

## Additional information

Not a duplicate: no open PR moves these helpers or adds an import
boundary for
`datasource_v2`. ray-project#65743 (CSV on DatasourceV2) touches the package but
not
these modules.

Checks run locally from this branch (macOS, `~/ray/.venv`):

```
python ci/lint/check_datasource_v2_imports.py                                  # clean
pytest -q tests/unit/datasource_v2 tests/unit/test_parquet_predicate_split.py  # 159 passed
pytest -q tests/datasource/test_read_parquet_v2.py                             # 37 passed
pytest -q tests/test_execution_optimizer_limit_pushdown.py                     # 28 passed
```

`ruff`, `black` and `pydoclint` at the pre-commit pins are clean;
`pyrefly`
reports the same 60 pre-existing errors as master.

Written with Claude Code; I reviewed every changed line and ran the
tests above.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Signed-off-by: Aarrya <[email protected]>
Co-authored-by: Claude Fable 5.1 <[email protected]>
nadongjun pushed a commit to nadongjun/ray that referenced this pull request Oct 6, 2026
…6463)

## Description

Since ray-project#66357, `import ray.data` loads `pyarrow.dataset` and its native
extensions. The legacy `parquet_datasource.py` now imports
`_compute_row_hashes` from the V2 readers package at module level, and
the V2 reader modules import `pyarrow.dataset` at load time. Before
ray-project#66357, the dependency ran the other way and `ray.data` never reached
`pyarrow.dataset` on import.

This PR moves the `_compute_row_hashes` import into
`_read_batches_from`, its only caller. That matches the file's existing
function-level `pyarrow.dataset` imports. The import points at
`readers/synthesized_columns.py`, where the helper has lived since
ray-project#66390, rather than at the `file_reader` re-export. Importing from
`synthesized_columns` at module level isn't enough on its own, because
`read_units.py` also imports `pyarrow.dataset` at the top level.

It also adds `test_import_does_not_load_pyarrow_dataset` to
`python/ray/data/tests/test_import.py`. The test asserts that a fresh
driver's `import ray.data` loads no `pyarrow._dataset*` modules.

### Why this matters

The Ray docs Read the Docs build has been segfaulting intermittently, in
about 31% of builds since 2026-09-23. The crash is in native extension
init while autosummary imports RLlib → Tune → AIR → `ray.data`. A
`PYTHONFAULTHANDLER=1` traceback (ray-project#66452) puts the crash inside `from
pyarrow._dataset_parquet import ...`, which is reached from
`parquet_datasource.py` → `file_reader.py` → `import pyarrow.dataset`.
The loaded-extensions list in that traceback ends with exactly the
modules this import adds.

This change takes the crashing import off the `import ray.data` path. It
doesn't fix the underlying native crash. I couldn't reproduce it with a
bare `import pyarrow.dataset` loop in `linux/amd64` Docker: 0 of 300
runs failed, with numpy 2.2.6 and pyarrow 23.0.1 from the doc lock. So
it seems to depend on process state or on the build host. The docs build
and `requirements_compiled.txt` both pin pyarrow 23.0.1, and neither pin
changed in the failure window.

## Related issues

Related to ray-project#66357 and ray-project#66452.

## Additional information

### Not a duplicate

Searched open PRs for `_compute_row_hashes`, the DSv2 reorg series, and
lazy-import and segfault keywords. ray-project#66450 (DSv2 Reorg [4/6]) and ray-project#66201
are the nearest open PRs, and neither touches this import or
`test_import.py`.

### Tests run

These were run locally on macOS arm64, Python 3.11, pyarrow 23.0.1,
numpy 2.2.6. They used the Ray nightly wheel with `ray/data` and
`ray/tests` linked from this branch.

* `pytest python/ray/data/tests/test_import.py`: 2 passed. With the
`parquet_datasource.py` change reverted, the new test fails and lists
`pyarrow._dataset`, `_dataset_orc`, `_dataset_parquet`, and
`_dataset_parquet_encryption`.
* `pytest python/ray/data/tests/datasource/test_parquet.py -k row_hash`:
18 passed.
* `pytest python/ray/data/tests/datasource/test_parquet.py` (full
module): 242 passed, 13 skipped, 13 failed, 34 errors. Rerunning the 47
failing tests with master's `parquet_datasource.py` gave an identical
failing set, so none come from this change. They come from the local
environment:
* The S3 fixtures error in `fsspec`'s HTTP backend, because their test
dependencies aren't installed.
* Several local-path tests assert `string` columns and get
`large_string`, which is pandas/pyarrow version skew against CI's pins.

  CI is the authoritative run for this module.
* `pre-commit run --files
python/ray/data/_internal/datasource/parquet_datasource.py`: all hooks
passed, including ruff, black, pydoclint, and the import-order check.

### AI assistance

AI assistance (Claude Code) was used to investigate the import graph,
write the change and test, and draft this description. This PR stays in
draft until the submitter has reviewed every changed line.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Signed-off-by: Douglas Strodtman <[email protected]>
Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
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