Skip to content
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

[Data] Re-implement APIs like select_columns with PyArrow batch format #48140

Merged

Conversation

ArturNiederfahrenhorst
Copy link
Contributor

@ArturNiederfahrenhorst ArturNiederfahrenhorst commented Oct 21, 2024

Related issue number

Closes #48090

Prerequisite: #48575

@ArturNiederfahrenhorst
Copy link
Contributor Author

Looking at the failed test...

python/ray/data/dataset.py Outdated Show resolved Hide resolved
@ArturNiederfahrenhorst
Copy link
Contributor Author

I'll rebase once the fix is in and mongodb test should pass

Copy link
Contributor

@alexeykudinkin alexeykudinkin left a comment

Choose a reason for hiding this comment

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

@ArturNiederfahrenhorst please hold on landing this one

python/ray/data/dataset.py Outdated Show resolved Hide resolved
python/ray/data/dataset.py Outdated Show resolved Hide resolved
)

assert ds.count() == 5
assert ds.schema().names == ["_id", "float_field", "int_field"]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Made these changes to decouple them from the string representation which may vary over versions. On my local environment, it was different then here/CI.

Copy link
Contributor

Choose a reason for hiding this comment

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

great!

Copy link
Member

Choose a reason for hiding this comment

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

Nice

Copy link
Contributor

Choose a reason for hiding this comment

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

@ArturNiederfahrenhorst that's a nice change.

Let's however not reduce strictness of the check itself -- let's keep asserting on the full schema, not just the column names

@@ -362,7 +383,7 @@ def test_drop_columns(ray_start_regular_shared, tmp_path):
assert ds.drop_columns(["col2"]).take(1) == [{"col1": 1, "col3": 3}]
assert ds.drop_columns(["col1", "col3"]).take(1) == [{"col2": 2}]
assert ds.drop_columns([]).take(1) == [{"col1": 1, "col2": 2, "col3": 3}]
assert ds.drop_columns(["col1", "col2", "col3"]).take(1) == [{}]
assert ds.drop_columns(["col1", "col2", "col3"]).take(1) == []
Copy link
Contributor Author

Choose a reason for hiding this comment

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

As discussed offline, this behavior is arbitrary and probably has little practical relevance.
Since our pyarrow implementation of the drop operation returns an empty list, we decided to just change the test in this case.

def add_column(batch: "pandas.DataFrame") -> "pandas.DataFrame":
batch.loc[:, col] = fn(batch)
return batch
def add_column(batch: "pyarrow.Table") -> "pyarrow.Table":
Copy link
Contributor

Choose a reason for hiding this comment

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

the typing here is off - batch is DataBatch type right? for example if it is pandas

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks!

Comment on lines 781 to 789
if batch_format not in [
"pandas",
"pyarrow",
]:
raise ValueError(
f"batch_format argument must be 'pandas' or 'pyarrow', "
f"got: {batch_format}"
)

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think you need to validate here, should happen in map_batches

Comment on lines 843 to 877
# Historically, we have also accepted lists with duplicate column names.
# This is not tolerated by the underlying pyarrow.Table.drop_columns method.
cols_without_duplicates = list(set(cols))

Copy link
Contributor

Choose a reason for hiding this comment

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

i think we should just enforce this via validation / raise an error

Copy link
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 breaking change then!
Still?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it's fine, yes.

python/ray/data/dataset.py Outdated Show resolved Hide resolved
Comment on lines 781 to 788
if batch_format not in [
"pandas",
"pyarrow",
]:
raise ValueError(
f"batch_format argument must be 'pandas' or 'pyarrow', "
f"got: {batch_format}"
)
Copy link
Member

Choose a reason for hiding this comment

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

Any reason we can't support the numpy batch format?

Comment on lines 775 to 776
# Create a new table with the updated column
return batch.set_column(column_idx, col, column)
Copy link
Member

Choose a reason for hiding this comment

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

Should we either error or emit a warning here? Overriding a column might be unexpected

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@bveeramani Does Ray Data have existing helpers to log this without spamming?
I'd do the same for numpy, pandas and arrow then.

Copy link
Contributor

Choose a reason for hiding this comment

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

+1

Since API is called add_column, i think we should assert that the column does not exist

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since @bveeramani is ok with an error or a warning and @alexeykudinkin prefers an error, I've made this case an error.

)

assert ds.count() == 5
assert ds.schema().names == ["_id", "float_field", "int_field"]
Copy link
Member

Choose a reason for hiding this comment

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

Nice

@richardliaw richardliaw added the go add ONLY when ready to merge, run all tests label Nov 19, 2024
python/ray/data/dataset.py Outdated Show resolved Hide resolved
python/ray/data/dataset.py Show resolved Hide resolved
# Create a new table with the updated column
return batch.set_column(column_idx, col, column)
else:
# batch format is assumed to be numpy
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's not assume and instead add explicit conditional (for unsupported format throw an exception)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

While I'm fine with that, this collides with #48140 (comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll revert the change I made for Richard then, assuming that I should follow the recommendation of the Ray Data team here.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, let's not assume the format -- UDF has to match the format and hence we need to be careful with an assumptions like that

Comment on lines 775 to 776
# Create a new table with the updated column
return batch.set_column(column_idx, col, column)
Copy link
Contributor

Choose a reason for hiding this comment

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

+1

Since API is called add_column, i think we should assert that the column does not exist


# Test with pyarrow batch format
ds = ray.data.range(5).add_column(
"foo", lambda x: pa.array([1] * x.num_rows), batch_format="pyarrow"
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's also test with pa.chunked_array

)

assert ds.count() == 5
assert ds.schema().names == ["_id", "float_field", "int_field"]
Copy link
Contributor

Choose a reason for hiding this comment

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

@ArturNiederfahrenhorst that's a nice change.

Let's however not reduce strictness of the check itself -- let's keep asserting on the full schema, not just the column names

python/ray/data/dataset.py Show resolved Hide resolved
python/ray/data/dataset.py Outdated Show resolved Hide resolved

def add_column(
batch: DataBatch,
) -> Union["pyarrow.Array", "pandas.Series", Dict[str, "np.ndarray"]]:
Copy link
Contributor

Choose a reason for hiding this comment

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

This should also return DataBatch

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ooof, good one!

@ArturNiederfahrenhorst ArturNiederfahrenhorst enabled auto-merge (squash) November 21, 2024 00:21
@github-actions github-actions bot disabled auto-merge November 21, 2024 15:38
ArturNiederfahrenhorst and others added 18 commits November 21, 2024 23:46
Co-authored-by: Balaji Veeramani <bveeramani@berkeley.edu>
Signed-off-by: Artur Niederfahrenhorst <attaismyname@googlemail.com>
Co-authored-by: Alexey Kudinkin <alexey.kudinkin@gmail.com>
Signed-off-by: Artur Niederfahrenhorst <attaismyname@googlemail.com>
Co-authored-by: Alexey Kudinkin <alexey.kudinkin@gmail.com>
Signed-off-by: Artur Niederfahrenhorst <attaismyname@googlemail.com>
@ArturNiederfahrenhorst ArturNiederfahrenhorst enabled auto-merge (squash) November 22, 2024 00:01
@ArturNiederfahrenhorst ArturNiederfahrenhorst merged commit 335bd66 into ray-project:master Nov 22, 2024
6 checks passed
MortalHappiness pushed a commit to MortalHappiness/ray that referenced this pull request Nov 22, 2024
ray-project#48140)

## Related issue number

Closes ray-project#48090 

Prerequisite: ray-project#48575

---------

Signed-off-by: Artur Niederfahrenhorst <attaismyname@googlemail.com>
Co-authored-by: Balaji Veeramani <bveeramani@berkeley.edu>
Co-authored-by: Alexey Kudinkin <alexey.kudinkin@gmail.com>
bveeramani added a commit that referenced this pull request Nov 25, 2024
Previously, you could add a column with a list like this:
```
ds.add_column("zeros", lambda batch: [0] * len(batch))
```

However, after #48140, this
behavior isn't supported.

To avoid breaking tests and user code, this PR re-adds support for
lists.

---------

Signed-off-by: Balaji Veeramani <bveeramani@berkeley.edu>
jecsand838 pushed a commit to jecsand838/ray that referenced this pull request Dec 4, 2024
ray-project#48140)

## Related issue number

Closes ray-project#48090

Prerequisite: ray-project#48575

---------

Signed-off-by: Artur Niederfahrenhorst <attaismyname@googlemail.com>
Co-authored-by: Balaji Veeramani <bveeramani@berkeley.edu>
Co-authored-by: Alexey Kudinkin <alexey.kudinkin@gmail.com>
Signed-off-by: Connor Sanders <connor@elastiflow.com>
jecsand838 pushed a commit to jecsand838/ray that referenced this pull request Dec 4, 2024
Previously, you could add a column with a list like this:
```
ds.add_column("zeros", lambda batch: [0] * len(batch))
```

However, after ray-project#48140, this
behavior isn't supported.

To avoid breaking tests and user code, this PR re-adds support for
lists.

---------

Signed-off-by: Balaji Veeramani <bveeramani@berkeley.edu>
Signed-off-by: Connor Sanders <connor@elastiflow.com>
dentiny pushed a commit to dentiny/ray that referenced this pull request Dec 7, 2024
ray-project#48140)

## Related issue number

Closes ray-project#48090 

Prerequisite: ray-project#48575

---------

Signed-off-by: Artur Niederfahrenhorst <attaismyname@googlemail.com>
Co-authored-by: Balaji Veeramani <bveeramani@berkeley.edu>
Co-authored-by: Alexey Kudinkin <alexey.kudinkin@gmail.com>
Signed-off-by: hjiang <dentinyhao@gmail.com>
dentiny pushed a commit to dentiny/ray that referenced this pull request Dec 7, 2024
Previously, you could add a column with a list like this:
```
ds.add_column("zeros", lambda batch: [0] * len(batch))
```

However, after ray-project#48140, this
behavior isn't supported.

To avoid breaking tests and user code, this PR re-adds support for
lists.

---------

Signed-off-by: Balaji Veeramani <bveeramani@berkeley.edu>
Signed-off-by: hjiang <dentinyhao@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
go add ONLY when ready to merge, run all tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Data] Re-implement APIs like select_columns with PyArrow batch format
4 participants