You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by ko...@apache.org on 2019/08/25 05:04:03 UTC

[arrow] branch master updated: ARROW-6346: [GLib] Add garrow_array_view()

This is an automated email from the ASF dual-hosted git repository.

kou pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow.git


The following commit(s) were added to refs/heads/master by this push:
     new 9fbbc73  ARROW-6346: [GLib] Add garrow_array_view()
9fbbc73 is described below

commit 9fbbc73f5c91a3d02d3cd1f838b774bec7b8b8d6
Author: Yosuke Shiro <yo...@gmail.com>
AuthorDate: Sun Aug 25 14:03:43 2019 +0900

    ARROW-6346: [GLib] Add garrow_array_view()
    
    Closes #5187 from shiro615/glib-view and squashes the following commits:
    
    bd341ee67 <Sutou Kouhei> Ensure removing error context
    03598e602 <Yosuke Shiro> Add garrow_array_view()
    
    Lead-authored-by: Yosuke Shiro <yo...@gmail.com>
    Co-authored-by: Sutou Kouhei <ko...@clear-code.com>
    Signed-off-by: Sutou Kouhei <ko...@clear-code.com>
---
 c_glib/arrow-glib/basic-array.cpp | 28 ++++++++++++++++++++++++++++
 c_glib/arrow-glib/basic-array.h   |  4 ++++
 c_glib/test/test-array.rb         | 16 ++++++++++++++++
 3 files changed, 48 insertions(+)

diff --git a/c_glib/arrow-glib/basic-array.cpp b/c_glib/arrow-glib/basic-array.cpp
index b051c97..db04660 100644
--- a/c_glib/arrow-glib/basic-array.cpp
+++ b/c_glib/arrow-glib/basic-array.cpp
@@ -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,
diff --git a/c_glib/arrow-glib/basic-array.h b/c_glib/arrow-glib/basic-array.h
index d10abea..cb50859 100644
--- a/c_glib/arrow-glib/basic-array.h
+++ b/c_glib/arrow-glib/basic-array.h
@@ -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())
diff --git a/c_glib/test/test-array.rb b/c_glib/test/test-array.rb
index 3befde3..1b6294e 100644
--- a/c_glib/test/test-array.rb
+++ b/c_glib/test/test-array.rb
@@ -115,4 +115,20 @@ class TestArray < Test::Unit::TestCase
 ]
     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