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-6346: [GLib] Add garrow_array_view() #5187

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions c_glib/arrow-glib/basic-array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,34 @@ garrow_array_to_string(GArrowArray *array, GError **error)
}
}

/**
* garrow_array_view:
* @array: A #GArrowArray.
* @return_type: A #GArrowDataType of the returned view.
* @error: (nullable): Return location for a #GError or %NULL.
*
* Returns: (nullable) (transfer full): A zero-copy view of this array
* with the given type. This method checks if the `return_type` are
* layout-compatible.
*
* Since: 0.15.0
*/
GArrowArray *
garrow_array_view(GArrowArray *array,
GArrowDataType *return_type,
GError **error)
{
auto arrow_array_raw = garrow_array_get_raw(array);
auto arrow_return_type = garrow_data_type_get_raw(return_type);
std::shared_ptr<arrow::Array> arrow_array;
auto status = arrow_array_raw->View(arrow_return_type, &arrow_array);
if (garrow_error_check(error, status, "[array][view]")) {
return garrow_array_new_raw(&arrow_array);
} else {
return NULL;
}
}


G_DEFINE_TYPE(GArrowNullArray,
garrow_null_array,
Expand Down
4 changes: 4 additions & 0 deletions c_glib/arrow-glib/basic-array.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ GArrowArray *garrow_array_slice (GArrowArray *array,
gint64 length);
gchar *garrow_array_to_string (GArrowArray *array,
GError **error);
GARROW_AVAILABLE_IN_0_15
GArrowArray *garrow_array_view(GArrowArray *array,
GArrowDataType *return_type,
GError **error);


#define GARROW_TYPE_NULL_ARRAY (garrow_null_array_get_type())
Expand Down
16 changes: 16 additions & 0 deletions c_glib/test/test-array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,20 @@ def test_to_s
]
CONTENT
end

sub_test_case("#view") do
def test_valid
assert_equal(build_float_array([0.0, 1.5, -2.5, nil]),
build_int32_array([0, 1069547520, -1071644672, nil]).view(Arrow::FloatDataType.new))
end

def test_invalid
message = "[array][view]: Invalid: " +
"Can't view array of type int16 as int8: incompatible layouts"
error = assert_raise(Arrow::Error::Invalid) do
build_int16_array([0, -1, 3]).view(Arrow::Int8DataType.new)
end
assert_equal(message, error.message.lines.first.chomp)
end
end
end