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

Avoid PyArrow type optimization if it fails #3234

Merged
merged 10 commits into from
Nov 10, 2021
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
10 changes: 10 additions & 0 deletions src/datasets/arrow_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ def __arrow_array__(self, type=None):
trying_type = True
else:
type = self.type
trying_int_optimization = False
try:
if isinstance(type, _ArrayXDExtensionType):
if isinstance(self.data, np.ndarray):
Expand Down Expand Up @@ -130,6 +131,7 @@ def __arrow_array__(self, type=None):
"Specified try_type alters data. Please check that the type/feature that you provided match the type/features of the data."
)
if self.optimized_int_type and self.type is None and self.try_type is None:
trying_int_optimization = True
if pa.types.is_int64(out.type):
out = out.cast(self.optimized_int_type)
elif pa.types.is_list(out.type):
Expand All @@ -154,6 +156,10 @@ def __arrow_array__(self, type=None):
type_(self.data), e
)
) from None
elif trying_int_optimization and "not in range" in str(e):
optimized_int_type_str = np.dtype(self.optimized_int_type.to_pandas_dtype()).name
logger.info(f"Failed to cast a sequence to {optimized_int_type_str}. Falling back to int64.")
return out
mariosasko marked this conversation as resolved.
Show resolved Hide resolved
else:
raise
elif "overflow" in str(e):
Expand All @@ -162,6 +168,10 @@ def __arrow_array__(self, type=None):
type_(self.data), e
)
) from None
elif trying_int_optimization and "not in range" in str(e):
optimized_int_type_str = np.dtype(self.optimized_int_type.to_pandas_dtype()).name
logger.info(f"Failed to cast a sequence to {optimized_int_type_str}. Falling back to int64.")
return out
mariosasko marked this conversation as resolved.
Show resolved Hide resolved
else:
raise

Expand Down
19 changes: 19 additions & 0 deletions tests/test_arrow_writer.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import copy
import os
import tempfile
from unittest import TestCase

import numpy as np
import pyarrow as pa
import pytest

Expand Down Expand Up @@ -211,6 +213,13 @@ def get_base_dtype(arr_type):
return arr_type


def change_first_primitive_element_in_list(lst, value):
if isinstance(lst[0], list):
change_first_primitive_element_in_list(lst[0], value)
else:
lst[0] = value


@pytest.mark.parametrize("optimized_int_type, expected_dtype", [(None, pa.int64()), (pa.int32(), pa.int32())])
@pytest.mark.parametrize("sequence", [[1, 2, 3], [[1, 2, 3]], [[[1, 2, 3]]]])
def test_optimized_int_type_for_typed_sequence(sequence, optimized_int_type, expected_dtype):
Expand All @@ -230,9 +239,19 @@ def test_optimized_int_type_for_typed_sequence(sequence, optimized_int_type, exp
)
@pytest.mark.parametrize("sequence", [[1, 2, 3], [[1, 2, 3]], [[[1, 2, 3]]]])
def test_optimized_typed_sequence(sequence, col, expected_dtype):
# in range
arr = pa.array(OptimizedTypedSequence(sequence, col=col))
assert get_base_dtype(arr.type) == expected_dtype

# not in range
if col != "other":
# avoids errors due to in-place modifications
sequence = copy.deepcopy(sequence)
value = np.iinfo(expected_dtype.to_pandas_dtype()).max + 1
change_first_primitive_element_in_list(sequence, value)
arr = pa.array(OptimizedTypedSequence(sequence, col=col))
assert get_base_dtype(arr.type) == pa.int64()


@pytest.mark.parametrize("raise_exception", [False, True])
def test_arrow_writer_closes_stream(raise_exception, tmp_path):
Expand Down