Repository navigation
[Data] DSv2 Reorg [2/6]: Make datasource_v2 independent of the legacy datasource package - #66357
Conversation
… datasource package Co-Authored-By: Claude Fable 5.1 <[email protected]> Signed-off-by: Aarrya <[email protected]>
193e601 to
6dbb9d7
Compare
There was a problem hiding this comment.
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.
| metadata = fragment.metadata | ||
| column_indices = ( | ||
| _resolve_leaf_column_indices(metadata, columns) | ||
| if columns is not None and metadata.num_row_groups > 0 | ||
| else None | ||
| ) |
There was a problem hiding this comment.
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.
| 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 |
There was a problem hiding this comment.
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).
| self._file_extensions = file_extensions or PARQUET_FILE_EXTENSIONS | |
| self._file_extensions = file_extensions or list(PARQUET_FILE_EXTENSIONS) |
| PartitionPredicatePruner, | ||
| ) | ||
| from ray.data._internal.datasource_v2.logical_optimizers import ( | ||
| from ray.data._internal.datasource_v2.pushdown import ( |
There was a problem hiding this comment.
let's not change this module. Leave it as logical optimizers
|
|
||
| # Arrow's nested type chunking limit | ||
| # See: https://github.com/apache/arrow/issues/21526 (ARROW-5030) | ||
| _ARROW_CHUNK_LIMIT = 2 * 1024**3 # 2GB |
There was a problem hiding this comment.
This is a code move not a code change. Are we sure we want to use this constant and change from GB to GiB?
…nstead of renaming it to pushdown.py Co-Authored-By: Claude Fable 5.1 <[email protected]> Signed-off-by: Aarrya <[email protected]>
Co-Authored-By: Claude Fable 5.1 <[email protected]> Signed-off-by: Aarrya <[email protected]>
## 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]>
… 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]>
…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]>
Description
Third PR of the DSv2 Reorg series, on top of #66312 (
[1.5/6]).datasource_v2still imports helpers from the legacyparquet_datasource.py.This PR moves them into
datasource_v2, points the legacy module at their newhomes, 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
sizing, leaf-column resolution, row-group size, the Arrow chunk limit) move
from the legacy module into new
datasource_v2/parquet_utils.py.combine_predicatesand_split_predicate_by_columnsmove from the legacy module into
datasource_v2/pushdown.py, which islogical_optimizers.pyrenamed because it now holds all pushdown code._compute_row_hashesmoves from the legacy module intodatasource_v2/readers/file_reader.py.parquet_datasource.pyimports all of the above back from V2.ci/lint/check_datasource_v2_imports.pyfails on any V2 import ofthe legacy package, module-level or function-level, wired into pre-commit
and
lint.sh.Code changes summarized
PARQUET_FILE_EXTENSIONSinparquet_utils.py; bothParquetDatasourceV2and the legacyParquetDatasource._FILE_EXTENSIONSread it from there.check_for_legacy_tensor_typegained type annotations on the move.parquet_file_reader.pyimports the moved helpers at module level insteadof 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.pyimports onlytypingandpyarrow, so the reason isgone. 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_hasheslives infile_reader.pyrather thanparquet_utils.pybecause it is not Parquet code: it makes therow_hashcolumn for every format, and the generic
FileReaderis its only V2 caller.ray.data.expressionsimport shrinks fromBinaryExpr, Expr, OperationtoExpr. The two dropped names were used onlyby the moved
_split_predicate_by_columns;Exprstill annotates a legacymethod parameter.
test_parquet.pytest patches the nested-type helper at both the legacypath 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 notthese modules.
Checks run locally from this branch (macOS,
~/ray/.venv):ruff,blackandpydoclintat the pre-commit pins are clean;pyreflyreports 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