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

[Datasets] [Arrow 7+ Support - 3/N] Add support for Arrow 8, 9, 10, and nightly. #29999

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions .buildkite/pipeline.ml.yml
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,46 @@
# Dask tests and examples.
- bazel test --config=ci $(./ci/run/bazel_export_options) --build_tests_only --test_tag_filters=-client python/ray/util/dask/...

- label: "Dataset tests (Arrow nightly)"
conditions: ["NO_WHEELS_REQUIRED", "RAY_CI_PYTHON_AFFECTED", "RAY_CI_DATA_AFFECTED"]
instance_size: medium
commands:
- cleanup() { if [ "${BUILDKITE_PULL_REQUEST}" = "false" ]; then ./ci/build/upload_build_info.sh; fi }; trap cleanup EXIT
- DATA_PROCESSING_TESTING=1 ARROW_VERSION=nightly ./ci/env/install-dependencies.sh
- ./ci/env/env_info.sh
- bazel test --config=ci $(./ci/run/bazel_export_options) --build_tests_only python/ray/data/...
- bazel test --config=ci $(./ci/run/bazel_export_options) --build_tests_only --test_tag_filters=ray_data python/ray/air/...

- label: "Dataset tests (Arrow 10)"
conditions: ["NO_WHEELS_REQUIRED", "RAY_CI_PYTHON_AFFECTED", "RAY_CI_DATA_AFFECTED"]
instance_size: medium
commands:
- cleanup() { if [ "${BUILDKITE_PULL_REQUEST}" = "false" ]; then ./ci/build/upload_build_info.sh; fi }; trap cleanup EXIT
- DATA_PROCESSING_TESTING=1 ARROW_VERSION=10.* ./ci/env/install-dependencies.sh
- ./ci/env/env_info.sh
- bazel test --config=ci $(./ci/run/bazel_export_options) --build_tests_only python/ray/data/...
- bazel test --config=ci $(./ci/run/bazel_export_options) --build_tests_only --test_tag_filters=ray_data python/ray/air/...

- label: "Dataset tests (Arrow 9)"
conditions: ["NO_WHEELS_REQUIRED", "RAY_CI_PYTHON_AFFECTED", "RAY_CI_DATA_AFFECTED"]
instance_size: medium
commands:
- cleanup() { if [ "${BUILDKITE_PULL_REQUEST}" = "false" ]; then ./ci/build/upload_build_info.sh; fi }; trap cleanup EXIT
- DATA_PROCESSING_TESTING=1 ARROW_VERSION=9.* ./ci/env/install-dependencies.sh
- ./ci/env/env_info.sh
- bazel test --config=ci $(./ci/run/bazel_export_options) --build_tests_only python/ray/data/...
- bazel test --config=ci $(./ci/run/bazel_export_options) --build_tests_only --test_tag_filters=ray_data python/ray/air/...

- label: "Dataset tests (Arrow 8)"
conditions: ["NO_WHEELS_REQUIRED", "RAY_CI_PYTHON_AFFECTED", "RAY_CI_DATA_AFFECTED"]
instance_size: medium
commands:
- cleanup() { if [ "${BUILDKITE_PULL_REQUEST}" = "false" ]; then ./ci/build/upload_build_info.sh; fi }; trap cleanup EXIT
- DATA_PROCESSING_TESTING=1 ARROW_VERSION=8.* ./ci/env/install-dependencies.sh
- ./ci/env/env_info.sh
- bazel test --config=ci $(./ci/run/bazel_export_options) --build_tests_only python/ray/data/...
- bazel test --config=ci $(./ci/run/bazel_export_options) --build_tests_only --test_tag_filters=ray_data python/ray/air/...

- label: "Dataset tests (Arrow 7)"
conditions: ["NO_WHEELS_REQUIRED", "RAY_CI_PYTHON_AFFECTED", "RAY_CI_DATA_AFFECTED"]
instance_size: medium
Expand Down
4 changes: 4 additions & 0 deletions python/ray/_private/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from typing import TYPE_CHECKING, List, Optional

from ray._private.client_mode_hook import client_mode_hook
from ray._private.utils import _add_creatable_buckets_param_if_s3_uri

if TYPE_CHECKING:
import pyarrow.fs
Expand Down Expand Up @@ -368,6 +369,9 @@ def _init_filesystem(create_valid_file: bool = False, check_valid_file: bool = T
fs_creator = _load_class(parsed_uri.netloc)
_filesystem, _storage_prefix = fs_creator(parsed_uri.path)
else:
# Arrow's S3FileSystem doesn't allow creating buckets by default, so we add a
# query arg enabling bucket creation if an S3 URI is provided.
_storage_uri = _add_creatable_buckets_param_if_s3_uri(_storage_uri)
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure the exact semantics of ray.init(storage=uri), but is this desired to always create bucket?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Those were the old semantics, and we have tests relying on that behavior, so this is just preserving the existing semantics of creating a bucket if necessary.

_filesystem, _storage_prefix = pyarrow.fs.FileSystem.from_uri(_storage_uri)

if os.name == "nt":
Expand Down
68 changes: 68 additions & 0 deletions python/ray/_private/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import tempfile
import threading
import time
from urllib.parse import urlencode, unquote, urlparse, parse_qsl, urlunparse
import uuid
import warnings
from inspect import signature
Expand Down Expand Up @@ -1599,6 +1600,73 @@ def split_address(address: str) -> Tuple[str, str]:
return (module_string, inner_address)


def _add_url_query_params(url: str, params: Dict[str, str]) -> str:
"""Add params to the provided url as query parameters.

If url already contains query parameters, they will be merged with params, with the
existing query parameters overriding any in params with the same parameter name.

Args:
url: The URL to add query parameters to.
params: The query parameters to add.

Returns:
URL with params added as query parameters.
"""
# Unquote URL first so we don't lose existing args.
url = unquote(url)
# Parse URL.
parsed_url = urlparse(url)
# Merge URL query string arguments dict with new params.
base_params = params
params = dict(parse_qsl(parsed_url.query))
base_params.update(params)
# bool and dict values should be converted to json-friendly values.
base_params.update(
{
k: json.dumps(v)
for k, v in base_params.items()
if isinstance(v, (bool, dict))
}
)

# Convert URL arguments to proper query string.
encoded_params = urlencode(base_params, doseq=True)
# Replace query string in parsed URL with updated query string.
parsed_url = parsed_url._replace(query=encoded_params)
# Convert back to URL.
return urlunparse(parsed_url)


def _add_creatable_buckets_param_if_s3_uri(uri: str) -> str:
"""If the provided URI is an S3 URL, add allow_bucket_creation=true as a query
parameter. For pyarrow >= 9.0.0, this is required in order to allow
``S3FileSystem.create_dir()`` to create S3 buckets.

If the provided URI is not an S3 URL or if pyarrow < 9.0.0 is installed, we return
the URI unchanged.

Args:
uri: The URI that we'll add the query parameter to, if it's an S3 URL.

Returns:
A URI with the added allow_bucket_creation=true query parameter, if the provided
URI is an S3 URL; uri will be returned unchanged otherwise.
"""
from pkg_resources._vendor.packaging.version import parse as parse_version

pyarrow_version = _get_pyarrow_version()
if pyarrow_version is not None:
pyarrow_version = parse_version(pyarrow_version)
if pyarrow_version is not None and pyarrow_version < parse_version("9.0.0"):
# This bucket creation query parameter is not required for pyarrow < 9.0.0.
return uri
parsed_uri = urlparse(uri)
if parsed_uri.scheme == "s3":
uri = _add_url_query_params(uri, {"allow_bucket_creation": True})
return uri


def _get_pyarrow_version() -> Optional[str]:
"""Get the version of the installed pyarrow package, returned as a tuple of ints.
Returns None if the package is not found.
Expand Down
64 changes: 56 additions & 8 deletions python/ray/air/tests/test_tensor_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import numpy as np
import pandas as pd
from pkg_resources._vendor.packaging.version import parse as parse_version
import pyarrow as pa
import pytest

Expand All @@ -12,6 +13,7 @@
ArrowVariableShapedTensorType,
)
from ray.air.util.tensor_extensions.pandas import TensorArray, TensorDtype
from ray._private.utils import _get_pyarrow_version


def test_tensor_array_validation():
Expand Down Expand Up @@ -343,8 +345,20 @@ def test_arrow_tensor_array_getitem(chunked):
if chunked:
t_arr = pa.chunked_array(t_arr)

for idx in range(outer_dim):
np.testing.assert_array_equal(t_arr[idx], arr[idx])
pyarrow_version = parse_version(_get_pyarrow_version())
if (
chunked
and pyarrow_version >= parse_version("8.0.0")
and pyarrow_version < parse_version("9.0.0")
):
for idx in range(outer_dim):
item = t_arr[idx]
assert isinstance(item, pa.ExtensionScalar)
item = item.type._extension_scalar_to_ndarray(item)
np.testing.assert_array_equal(item, arr[idx])
else:
for idx in range(outer_dim):
np.testing.assert_array_equal(t_arr[idx], arr[idx])

# Test __iter__.
for t_subarr, subarr in zip(t_arr, arr):
Expand All @@ -368,8 +382,19 @@ def test_arrow_tensor_array_getitem(chunked):

np.testing.assert_array_equal(t_arr2_npy, arr[1:])

for idx in range(1, outer_dim):
np.testing.assert_array_equal(t_arr2[idx - 1], arr[idx])
if (
chunked
and pyarrow_version >= parse_version("8.0.0")
and pyarrow_version < parse_version("9.0.0")
):
for idx in range(1, outer_dim):
item = t_arr2[idx - 1]
assert isinstance(item, pa.ExtensionScalar)
item = item.type._extension_scalar_to_ndarray(item)
np.testing.assert_array_equal(item, arr[idx])
else:
for idx in range(1, outer_dim):
np.testing.assert_array_equal(t_arr2[idx - 1], arr[idx])


@pytest.mark.parametrize("chunked", [False, True])
Expand All @@ -387,8 +412,20 @@ def test_arrow_variable_shaped_tensor_array_getitem(chunked):
if chunked:
t_arr = pa.chunked_array(t_arr)

for idx in range(outer_dim):
np.testing.assert_array_equal(t_arr[idx], arr[idx])
pyarrow_version = parse_version(_get_pyarrow_version())
if (
chunked
and pyarrow_version >= parse_version("8.0.0")
and pyarrow_version < parse_version("9.0.0")
):
for idx in range(outer_dim):
item = t_arr[idx]
assert isinstance(item, pa.ExtensionScalar)
item = item.type._extension_scalar_to_ndarray(item)
np.testing.assert_array_equal(item, arr[idx])
else:
for idx in range(outer_dim):
np.testing.assert_array_equal(t_arr[idx], arr[idx])

# Test __iter__.
for t_subarr, subarr in zip(t_arr, arr):
Expand All @@ -414,8 +451,19 @@ def test_arrow_variable_shaped_tensor_array_getitem(chunked):
for t_subarr, subarr in zip(t_arr2_npy, arr[1:]):
np.testing.assert_array_equal(t_subarr, subarr)

for idx in range(1, outer_dim):
np.testing.assert_array_equal(t_arr2[idx - 1], arr[idx])
if (
chunked
and pyarrow_version >= parse_version("8.0.0")
and pyarrow_version < parse_version("9.0.0")
):
for idx in range(1, outer_dim):
item = t_arr2[idx - 1]
assert isinstance(item, pa.ExtensionScalar)
item = item.type._extension_scalar_to_ndarray(item)
np.testing.assert_array_equal(item, arr[idx])
else:
for idx in range(1, outer_dim):
np.testing.assert_array_equal(t_arr2[idx - 1], arr[idx])


@pytest.mark.parametrize(
Expand Down
Loading