Skip to content

Commit

Permalink
ARROW-6346: [GLib] Add garrow_array_view()
Browse files Browse the repository at this point in the history
Closes apache#5187 from shiro615/glib-view and squashes the following commits:

bd341ee <Sutou Kouhei> Ensure removing error context
03598e6 <Yosuke Shiro> Add garrow_array_view()

Lead-authored-by: Yosuke Shiro <[email protected]>
Co-authored-by: Sutou Kouhei <[email protected]>
Signed-off-by: Sutou Kouhei <[email protected]>
  • Loading branch information
shiro615 and kou committed Aug 25, 2019
1 parent 921a916 commit 9fbbc73
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
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

0 comments on commit 9fbbc73

Please sign in to comment.