Skip to content

Commit

Permalink
Add garrow_array_view()
Browse files Browse the repository at this point in the history
  • Loading branch information
shiro615 committed Aug 24, 2019
1 parent 6cbaf72 commit 03598e6
Show file tree
Hide file tree
Showing 3 changed files with 47 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
15 changes: 15 additions & 0 deletions c_glib/test/test-array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,19 @@ 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"
assert_raise(Arrow::Error::Invalid.new(message)) do
build_int16_array([0, -1, 3]).view(Arrow::Int8DataType.new)
end
end
end
end

0 comments on commit 03598e6

Please sign in to comment.