-
Notifications
You must be signed in to change notification settings - Fork 7.3k
[Data] Compute Expressions-fixed size array #58741
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
bveeramani
merged 28 commits into
ray-project:master
from
400Ping:data/compute-expressions-arr
Jan 14, 2026
Merged
Changes from 6 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
c2b03a8
[data]Compute Expressions-fixed-size-array-operations
400Ping b1615a8
fix prpperty
400Ping 950ef28
add type-checking
400Ping 7819f4b
[data] Improve array namespace docs and tests
400Ping 0bc9400
fix linter
400Ping 7a51d0b
fix
400Ping 6cd7558
Merge branch 'master' into data/compute-expressions-arr
400Ping d666c74
fix
400Ping afb4571
fix linter
400Ping 2957754
fix test
400Ping 040877a
fix test_namespace_expressions
400Ping 0fd0ff4
Merge branch 'master' into data/compute-expressions-arr
400Ping 24f7eaf
Merge branch 'master' into data/compute-expressions-arr
400Ping c9b92a6
fix
400Ping 804c228
Merge branch 'master' into data/compute-expressions-arr
400Ping a7e4520
Update python/ray/data/namespace_expressions/arr_namespace.py
400Ping 70dbbd0
fix format
400Ping 7a6a2fb
fix
400Ping 244e2b0
Merge branch 'master' into data/compute-expressions-arr
400Ping 2829610
fix
400Ping 4717267
update
400Ping bb6fb05
Merge branch 'master' into data/compute-expressions-arr
400Ping c72eb5b
remove flatten()
400Ping 71e4784
Merge branch 'master' into data/compute-expressions-arr
400Ping c46d1fa
Merge remote-tracking branch 'upstream/master' into data/compute-expr…
400Ping c3d3bb2
fix CI error
400Ping aa9ded9
update
400Ping 2a283d8
fix
400Ping File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| """ | ||
| Fixed-size array namespace for expression operations. | ||
|
|
||
| This namespace handles Arrow ``FixedSizeListArray`` columns. It provides | ||
| helper methods for flattening nested arrays and converting them into | ||
| Python lists. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from dataclasses import dataclass | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| import pyarrow | ||
| import pyarrow.compute as pc | ||
|
|
||
| from ray.data.datatype import DataType | ||
| from ray.data.expressions import pyarrow_udf | ||
|
|
||
| if TYPE_CHECKING: | ||
| from ray.data.expressions import Expr, UDFExpr | ||
|
|
||
|
|
||
| def _fixed_size_list_to_list_array(arr: pyarrow.Array) -> pyarrow.Array: | ||
| """Convert a FixedSizeListArray to a ListArray of Python lists. | ||
|
|
||
| This helper keeps nulls in place (``None`` elements remain null in the | ||
| resulting array) and constructs a ``ListArray`` with the same value type | ||
| as the input ``FixedSizeListArray``. | ||
| """ | ||
| # Each element in ``arr`` is either None or a sequence of child values. | ||
| py_lists = [] | ||
| for sub in arr: | ||
| if sub is None: | ||
| py_lists.append(None) | ||
| else: | ||
| # Convert the child values (e.g., FixedSizeListArray[float]) to | ||
| # a plain Python list. | ||
| py_lists.append(list(sub)) | ||
|
|
||
| # Explicitly use a list type with the same value type as the input | ||
| # FixedSizeListArray. PyArrow supports None values in the sequence, which | ||
| # become nulls in the resulting ListArray. | ||
| list_type = pyarrow.list_(arr.type.value_type) | ||
| return pyarrow.array(py_lists, type=list_type) | ||
400Ping marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| @dataclass | ||
| class _ArrayNamespace: | ||
| """Namespace for fixed-size array operations on expression columns. | ||
|
|
||
| Example | ||
| ------- | ||
| >>> from ray.data.expressions import col | ||
| >>> # "features" is a FixedSizeList column, e.g. 3-d embeddings | ||
| >>> expr = col("features").arr.to_list() | ||
| >>> # You can then use this expression inside Dataset.select/with_columns. | ||
| """ | ||
|
|
||
| _expr: "Expr" | ||
|
|
||
| def flatten(self) -> "UDFExpr": | ||
| """Flatten each fixed-size array into a variable-length list. | ||
|
|
||
| For FixedSizeListArray inputs, this returns a column backed by an | ||
| Arrow ``ListArray``. We first try the native ``list_flatten`` compute | ||
| function, and fall back to a pure-Python implementation for older | ||
| Arrow versions that may not support this type. | ||
| """ | ||
| return_dtype = DataType(object) | ||
400Ping marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| @pyarrow_udf(return_dtype=return_dtype) | ||
| def _flatten(arr: pyarrow.Array) -> pyarrow.Array: | ||
| try: | ||
| # PyArrow supports flattening FixedSizeListArray via list_flatten. | ||
| # This returns a ListArray with variable-length lists. | ||
| return pc.list_flatten(arr) | ||
| except (TypeError, NotImplementedError): | ||
| # On older Arrow versions or unsupported types, fall back to a | ||
| # simple Python implementation that preserves nulls and shapes. | ||
| # If this fallback fails, its exception is propagated directly | ||
| # so we don't hide bugs in the fallback. | ||
| return _fixed_size_list_to_list_array(arr) | ||
|
|
||
| return _flatten(self._expr) | ||
400Ping marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| def to_list(self) -> "UDFExpr": | ||
| """Convert each fixed-size array into a Python list. | ||
|
|
||
| The output is a variable-length list column (Arrow ListArray) that can | ||
| be consumed as Python lists when materialized from a Dataset. | ||
| """ | ||
| return_dtype = DataType(object) | ||
400Ping marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| @pyarrow_udf(return_dtype=return_dtype) | ||
| def _to_list(arr: pyarrow.Array) -> pyarrow.Array: | ||
| return _fixed_size_list_to_list_array(arr) | ||
|
|
||
| return _to_list(self._expr) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.