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

ARROW-5992: [C++][Python] Support String->Binary in Array::View. Add Python bindings for Array::View #5125

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 4 additions & 3 deletions cpp/src/arrow/array.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1021,7 +1021,8 @@ struct ViewDataImpl {
return;
}
}
if (in_layouts[in_layout_idx].bit_widths[in_buffer_idx] > 0) {
auto bit_width = in_layouts[in_layout_idx].bit_widths[in_buffer_idx];
if (bit_width > 0 || bit_width == DataTypeLayout::kVariableSizeBuffer) {
return;
}
// Skip always-null input buffers
Expand Down Expand Up @@ -1128,8 +1129,8 @@ struct ViewDataImpl {
}

RETURN_NOT_OK(CheckInputAvailable());
if (out_bit_width == DataTypeLayout::kVariableSizeBuffer ||
out_bit_width != in_layouts[in_layout_idx].bit_widths[in_buffer_idx]) {
auto in_bit_width = in_layouts[in_layout_idx].bit_widths[in_buffer_idx];
if (out_bit_width != in_bit_width) {
return InvalidView("incompatible layouts");
}
// Copy input buffer
Expand Down
7 changes: 7 additions & 0 deletions cpp/src/arrow/array_view_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,13 @@ TEST(TestArrayView, PrimitiveAsFixedSizeBinary) {
CheckView(expected, arr);
}

TEST(TestArrayView, StringAsBinary) {
auto arr = ArrayFromJSON(utf8(), R"(["foox", "barz", null])");
auto expected = ArrayFromJSON(binary(), R"(["foox", "barz", null])");
CheckView(arr, expected);
CheckView(expected, arr);
}

TEST(TestArrayView, PrimitiveWrongSize) {
auto arr = ArrayFromJSON(int16(), "[0, -1, 42]");
CheckViewFails(arr, int8());
Expand Down
6 changes: 3 additions & 3 deletions cpp/src/arrow/testing/random.cc
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,9 @@ std::shared_ptr<arrow::Array> RandomArrayGenerator::BinaryWithRepeats(
double null_probability) {
auto strings =
StringWithRepeats(size, unique, min_length, max_length, null_probability);
auto data = strings->data()->Copy();
data->type = binary();
return MakeArray(data);
std::shared_ptr<Array> out;
ABORT_NOT_OK(strings->View(binary(), &out));
return out;
}

std::shared_ptr<arrow::Array> RandomArrayGenerator::StringWithRepeats(
Expand Down
19 changes: 19 additions & 0 deletions python/pyarrow/array.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,25 @@ cdef class Array(_PandasConvertible):

return pyarrow_wrap_array(result)

def view(self, object target_type):
"""Return zero-copy "view" of array as another data type. The data
types must have compatible columnar buffer layouts

Parameters
----------
target_type : DataType
Type to construct view as

Returns
-------
view : Array
"""
cdef DataType type = ensure_type(target_type)
cdef shared_ptr[CArray] result
with nogil:
check_status(self.ap.View(type.sp_type, &result))
return pyarrow_wrap_array(result)

def sum(self):
"""
Sum the values in a numerical array.
Expand Down
2 changes: 2 additions & 0 deletions python/pyarrow/includes/libarrow.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ cdef extern from "arrow/api.h" namespace "arrow" nogil:
shared_ptr[CArray] Slice(int64_t offset, int64_t length)

CStatus Validate() const
CStatus View(const shared_ptr[CDataType]& type,
shared_ptr[CArray]* out)

shared_ptr[CArray] MakeArray(const shared_ptr[CArrayData]& data)

Expand Down
9 changes: 9 additions & 0 deletions python/pyarrow/tests/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,15 @@ def test_cast_from_null():
in_arr.cast(out_type)


def test_view():
# ARROW-5992
arr = pa.array(['foo', 'bar', 'baz'], type=pa.utf8())
expected = pa.array(['foo', 'bar', 'baz'], type=pa.binary())

assert arr.view(pa.binary()).equals(expected)
assert arr.view('binary').equals(expected)


def test_unique_simple():
cases = [
(pa.array([1, 2, 3, 1, 2, 3]), pa.array([1, 2, 3])),
Expand Down