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/04/14 19:51:47 UTC

[arrow] branch master updated: ARROW-5168: [GLib] Add garrow_array_take()

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 d8fda48  ARROW-5168: [GLib] Add garrow_array_take()
d8fda48 is described below

commit d8fda482b282bd5dfe0ebd6126bbcb2a9b9ada28
Author: Yosuke Shiro <yo...@gmail.com>
AuthorDate: Mon Apr 15 04:51:28 2019 +0900

    ARROW-5168: [GLib] Add garrow_array_take()
    
    Author: Yosuke Shiro <yo...@gmail.com>
    
    Closes #4149 from shiro615/glib-take and squashes the following commits:
    
    d9ef374b <Yosuke Shiro> Add a test for indices with NULL
    9676f750 <Yosuke Shiro> Add GArrowTakeOptions
    2ee7630d <Yosuke Shiro>  Add garrow_array_take()
---
 c_glib/arrow-glib/compute.cpp             | 94 +++++++++++++++++++++++++++++++
 c_glib/arrow-glib/compute.h               | 21 +++++++
 c_glib/arrow-glib/compute.hpp             |  3 +
 c_glib/arrow-glib/version.h.in            | 23 ++++++++
 c_glib/doc/arrow-glib/arrow-glib-docs.xml |  4 ++
 c_glib/test/test-take.rb                  | 43 ++++++++++++++
 6 files changed, 188 insertions(+)

diff --git a/c_glib/arrow-glib/compute.cpp b/c_glib/arrow-glib/compute.cpp
index 8effe69..b339834 100644
--- a/c_glib/arrow-glib/compute.cpp
+++ b/c_glib/arrow-glib/compute.cpp
@@ -329,6 +329,44 @@ garrow_count_options_new(void)
 }
 
 
+typedef struct GArrowTakeOptionsPrivate_ {
+  arrow::compute::TakeOptions options;
+} GArrowTakeOptionsPrivate;
+
+G_DEFINE_TYPE_WITH_PRIVATE(GArrowTakeOptions,
+                           garrow_take_options,
+                           G_TYPE_OBJECT)
+
+#define GARROW_TAKE_OPTIONS_GET_PRIVATE(object)        \
+  static_cast<GArrowTakeOptionsPrivate *>(             \
+    garrow_take_options_get_instance_private(          \
+      GARROW_TAKE_OPTIONS(object)))
+
+static void
+garrow_take_options_init(GArrowTakeOptions *object)
+{
+}
+
+static void
+garrow_take_options_class_init(GArrowTakeOptionsClass *klass)
+{
+}
+
+/**
+ * garrow_take_options_new:
+ *
+ * Returns: A newly created #GArrowTakeOptions.
+ *
+ * Since: 0.14.0
+ */
+GArrowTakeOptions *
+garrow_take_options_new(void)
+{
+  auto take_options = g_object_new(GARROW_TYPE_TAKE_OPTIONS, NULL);
+  return GARROW_TAKE_OPTIONS(take_options);
+}
+
+
 /**
  * garrow_array_cast:
  * @array: A #GArrowArray.
@@ -902,6 +940,55 @@ garrow_double_array_sum(GArrowDoubleArray *array,
                                                      0);
 }
 
+/**
+ * garrow_array_take:
+ * @array: A #GArrowArray.
+ * @indices: The indices of values to take.
+ * @options: (nullable): A #GArrowTakeOptions.
+ * @error: (nullable): Return location for a #GError or %NULL.
+ *
+ * Returns: (nullable) (transfer full): The #GArrowArray taken from
+ *   an array of values at indices in input array or %NULL on error.
+ *
+ * Since: 0.14.0
+ */
+GArrowArray *
+garrow_array_take(GArrowArray *array,
+                  GArrowArray *indices,
+                  GArrowTakeOptions *options,
+                  GError **error)
+{
+  auto arrow_array = garrow_array_get_raw(array);
+  auto arrow_array_raw = arrow_array.get();
+  auto arrow_indices = garrow_array_get_raw(indices);
+  auto arrow_indices_raw = arrow_indices.get();
+  auto memory_pool = arrow::default_memory_pool();
+  arrow::compute::FunctionContext context(memory_pool);
+  std::shared_ptr<arrow::Array> taken_array;
+  arrow::Status status;
+  if (options) {
+    auto arrow_options = garrow_take_options_get_raw(options);
+    status = arrow::compute::Take(&context,
+                                  *arrow_array_raw,
+                                  *arrow_indices_raw,
+                                  *arrow_options,
+                                  &taken_array);
+  } else {
+    arrow::compute::TakeOptions arrow_options;
+    status = arrow::compute::Take(&context,
+                                  *arrow_array_raw,
+                                  *arrow_indices_raw,
+                                  arrow_options,
+                                  &taken_array);
+  }
+
+  if (garrow_error_check(error, status, "[array][take]")) {
+    return garrow_array_new_raw(&taken_array);
+  } else {
+    return NULL;
+  }
+}
+
 
 G_END_DECLS
 
@@ -941,3 +1028,10 @@ garrow_count_options_get_raw(GArrowCountOptions *count_options)
   auto priv = GARROW_COUNT_OPTIONS_GET_PRIVATE(count_options);
   return &(priv->options);
 }
+
+arrow::compute::TakeOptions *
+garrow_take_options_get_raw(GArrowTakeOptions *take_options)
+{
+  auto priv = GARROW_TAKE_OPTIONS_GET_PRIVATE(take_options);
+  return &(priv->options);
+}
diff --git a/c_glib/arrow-glib/compute.h b/c_glib/arrow-glib/compute.h
index 72bb0cb..d7bdd43 100644
--- a/c_glib/arrow-glib/compute.h
+++ b/c_glib/arrow-glib/compute.h
@@ -65,6 +65,22 @@ GArrowCountOptions *
 garrow_count_options_new(void);
 
 
+#define GARROW_TYPE_TAKE_OPTIONS (garrow_take_options_get_type())
+G_DECLARE_DERIVABLE_TYPE(GArrowTakeOptions,
+                         garrow_take_options,
+                         GARROW,
+                         TAKE_OPTIONS,
+                         GObject)
+struct _GArrowTakeOptionsClass
+{
+  GObjectClass parent_class;
+};
+
+GARROW_AVAILABLE_IN_0_14
+GArrowTakeOptions *
+garrow_take_options_new(void);
+
+
 GArrowArray *garrow_array_cast(GArrowArray *array,
                                GArrowDataType *target_data_type,
                                GArrowCastOptions *options,
@@ -131,5 +147,10 @@ gdouble garrow_float_array_sum(GArrowFloatArray *array,
 GARROW_AVAILABLE_IN_0_13
 gdouble garrow_double_array_sum(GArrowDoubleArray *array,
                                 GError **error);
+GARROW_AVAILABLE_IN_0_14
+GArrowArray *garrow_array_take(GArrowArray *array,
+                               GArrowArray *indices,
+                               GArrowTakeOptions *options,
+                               GError **error);
 
 G_END_DECLS
diff --git a/c_glib/arrow-glib/compute.hpp b/c_glib/arrow-glib/compute.hpp
index 495b3f6..a3ce56c 100644
--- a/c_glib/arrow-glib/compute.hpp
+++ b/c_glib/arrow-glib/compute.hpp
@@ -30,3 +30,6 @@ GArrowCountOptions *
 garrow_count_options_new_raw(arrow::compute::CountOptions *arrow_count_options);
 arrow::compute::CountOptions *
 garrow_count_options_get_raw(GArrowCountOptions *count_options);
+
+arrow::compute::TakeOptions *
+garrow_take_options_get_raw(GArrowTakeOptions *take_options);
diff --git a/c_glib/arrow-glib/version.h.in b/c_glib/arrow-glib/version.h.in
index 827b9c9..43a89dc 100644
--- a/c_glib/arrow-glib/version.h.in
+++ b/c_glib/arrow-glib/version.h.in
@@ -111,6 +111,15 @@
 #endif
 
 /**
+ * GARROW_VERSION_0_14:
+ *
+ * You can use this macro value for compile time API version check.
+ *
+ * Since: 0.14.0
+ */
+#define GARROW_VERSION_0_14 G_ENCODE_VERSION(0, 14)
+
+/**
  * GARROW_VERSION_0_13:
  *
  * You can use this macro value for compile time API version check.
@@ -184,6 +193,20 @@
 
 #define GARROW_AVAILABLE_IN_ALL
 
+#if GARROW_VERSION_MIN_REQUIRED >= GARROW_VERSION_0_14
+#  define GARROW_DEPRECATED_IN_0_14               GARROW_DEPRECATED
+#  define GARROW_DEPRECATED_IN_0_14_FOR(function) GARROW_DEPRECATED_FOR(function)
+#else
+#  define GARROW_DEPRECATED_IN_0_14
+#  define GARROW_DEPRECATED_IN_0_14_FOR(function)
+#endif
+
+#if GARROW_VERSION_MAX_ALLOWED < GARROW_VERSION_0_14
+#  define GARROW_AVAILABLE_IN_0_14 GARROW_UNAVAILABLE(0, 14)
+#else
+#  define GARROW_AVAILABLE_IN_0_14
+#endif
+
 #if GARROW_VERSION_MIN_REQUIRED >= GARROW_VERSION_0_13
 #  define GARROW_DEPRECATED_IN_0_13               GARROW_DEPRECATED
 #  define GARROW_DEPRECATED_IN_0_13_FOR(function) GARROW_DEPRECATED_FOR(function)
diff --git a/c_glib/doc/arrow-glib/arrow-glib-docs.xml b/c_glib/doc/arrow-glib/arrow-glib-docs.xml
index 3dcc7fe..7429a22 100644
--- a/c_glib/doc/arrow-glib/arrow-glib-docs.xml
+++ b/c_glib/doc/arrow-glib/arrow-glib-docs.xml
@@ -160,6 +160,10 @@
     <title>Index of deprecated API</title>
     <xi:include href="xml/api-index-deprecated.xml"><xi:fallback /></xi:include>
   </index>
+  <index id="api-index-0-14-0" role="0.14.0">
+    <title>Index of new symbols in 0.14.0</title>
+    <xi:include href="xml/api-index-0.14.0.xml"><xi:fallback /></xi:include>
+  </index>
   <index id="api-index-0-13-0" role="0.13.0">
     <title>Index of new symbols in 0.13.0</title>
     <xi:include href="xml/api-index-0.13.0.xml"><xi:fallback /></xi:include>
diff --git a/c_glib/test/test-take.rb b/c_glib/test/test-take.rb
new file mode 100644
index 0000000..50b53dc
--- /dev/null
+++ b/c_glib/test/test-take.rb
@@ -0,0 +1,43 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+class TestTake < Test::Unit::TestCase
+  include Helper::Buildable
+  include Helper::Omittable
+
+  def test_no_null
+    require_gi(1, 42, 0)
+    indices = build_int16_array([1, 0, 2])
+    assert_equal(build_int16_array([0, 1, 2]),
+                 build_int16_array([1, 0 ,2]).take(indices))
+  end
+
+  def test_null
+    require_gi(1, 42, 0)
+    indices = build_int16_array([2, nil, 0])
+    assert_equal(build_int16_array([2, nil, 1]),
+                 build_int16_array([1, 0, 2]).take(indices))
+  end
+
+  def test_out_of_index
+    require_gi(1, 42, 0)
+    indices = build_int16_array([1, 2, 3])
+    assert_raise(Arrow::Error::Invalid) do
+      build_int16_array([0, 1, 2]).take(indices)
+    end
+  end
+end