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

shiro 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 c707822  ARROW-4887: [GLib] Add garrow_array_count()
c707822 is described below

commit c707822d01ccba02c823d14426188943ce7a4cdb
Author: Kouhei Sutou <k...@clear-code.com>
AuthorDate: Sat Mar 16 19:51:38 2019 +0900

    ARROW-4887: [GLib] Add garrow_array_count()
    
    Author: Kouhei Sutou <k...@clear-code.com>
    
    Closes #3912 from kou/glib-count and squashes the following commits:
    
    8132b636 <Kouhei Sutou> Fix document
    93b2004c <Kouhei Sutou>  Add garrow_array_count()
---
 c_glib/arrow-glib/basic-array.cpp |  45 ++++++++++++++
 c_glib/arrow-glib/basic-array.h   |   5 ++
 c_glib/arrow-glib/compute.cpp     | 124 +++++++++++++++++++++++++++++++++++++-
 c_glib/arrow-glib/compute.h       |  29 +++++++++
 c_glib/arrow-glib/compute.hpp     |   5 ++
 c_glib/test/test-count.rb         |  40 ++++++++++++
 cpp/src/arrow/compute/api.h       |   1 +
 7 files changed, 246 insertions(+), 3 deletions(-)

diff --git a/c_glib/arrow-glib/basic-array.cpp 
b/c_glib/arrow-glib/basic-array.cpp
index c201f9d..79ba1ab 100644
--- a/c_glib/arrow-glib/basic-array.cpp
+++ b/c_glib/arrow-glib/basic-array.cpp
@@ -643,6 +643,51 @@ garrow_array_dictionary_encode(GArrowArray *array,
   return garrow_array_new_raw(&arrow_dictionary_encoded_array);
 }
 
+/**
+ * garrow_array_count:
+ * @array: A #GArrowArray.
+ * @options: (nullable): A #GArrowCountOptions.
+ * @error: (nullable): Return location for a #GError or %NULL.
+ *
+ * Returns: The number of target values on success. If an error is occurred,
+ *   the returned value is untrustful value.
+ *
+ * Since: 0.13.0
+ */
+gint64
+garrow_array_count(GArrowArray *array,
+                   GArrowCountOptions *options,
+                   GError **error)
+{
+  auto arrow_array = garrow_array_get_raw(array);
+  auto arrow_array_raw = arrow_array.get();
+  auto memory_pool = arrow::default_memory_pool();
+  arrow::compute::FunctionContext context(memory_pool);
+  arrow::compute::Datum counted_datum;
+  arrow::Status status;
+  if (options) {
+    auto arrow_options = garrow_count_options_get_raw(options);
+    status = arrow::compute::Count(&context,
+                                   *arrow_options,
+                                   *arrow_array_raw,
+                                   &counted_datum);
+  } else {
+    arrow::compute::CountOptions 
arrow_options(arrow::compute::CountOptions::COUNT_ALL);
+    status = arrow::compute::Count(&context,
+                                   arrow_options,
+                                   *arrow_array_raw,
+                                   &counted_datum);
+  }
+
+  if (garrow_error_check(error, status, "[array][count]")) {
+    using ScalarType = typename 
arrow::TypeTraits<arrow::Int64Type>::ScalarType;
+    auto counted_scalar = 
std::dynamic_pointer_cast<ScalarType>(counted_datum.scalar());
+    return counted_scalar->value;
+  } else {
+    return 0;
+  }
+}
+
 
 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 1dde2f2..c91de5a 100644
--- a/c_glib/arrow-glib/basic-array.h
+++ b/c_glib/arrow-glib/basic-array.h
@@ -70,6 +70,11 @@ GArrowArray   *garrow_array_unique      (GArrowArray *array,
                                          GError **error);
 GArrowArray   *garrow_array_dictionary_encode(GArrowArray *array,
                                               GError **error);
+GARROW_AVAILABLE_IN_0_13
+gint64
+garrow_array_count(GArrowArray *array,
+                   GArrowCountOptions *options,
+                   GError **error);
 
 #define GARROW_TYPE_NULL_ARRAY                  \
   (garrow_null_array_get_type())
diff --git a/c_glib/arrow-glib/compute.cpp b/c_glib/arrow-glib/compute.cpp
index a9f6721..3dd226e 100644
--- a/c_glib/arrow-glib/compute.cpp
+++ b/c_glib/arrow-glib/compute.cpp
@@ -22,6 +22,7 @@
 #endif
 
 #include <arrow-glib/compute.hpp>
+#include <arrow-glib/enums.h>
 
 G_BEGIN_DECLS
 
@@ -31,7 +32,9 @@ G_BEGIN_DECLS
  * @title: Classes for computation
  * @include: arrow-glib/arrow-glib.h
  *
- * #GArrowCastOptions is a class to custom garrow_array_cast().
+ * #GArrowCastOptions is a class to customize garrow_array_cast().
+ *
+ * #GArrowCountOptions is a class to customize garrow_array_count().
  */
 
 typedef struct GArrowCastOptionsPrivate_ {
@@ -39,8 +42,7 @@ typedef struct GArrowCastOptionsPrivate_ {
 } GArrowCastOptionsPrivate;
 
 enum {
-  PROP_0,
-  PROP_ALLOW_INT_OVERFLOW,
+  PROP_ALLOW_INT_OVERFLOW = 1,
   PROP_ALLOW_TIME_TRUNCATE,
   PROP_ALLOW_FLOAT_TRUNCATE,
   PROP_ALLOW_INVALID_UTF8,
@@ -194,6 +196,105 @@ garrow_cast_options_new(void)
   return GARROW_CAST_OPTIONS(cast_options);
 }
 
+
+typedef struct GArrowCountOptionsPrivate_ {
+  arrow::compute::CountOptions options;
+} GArrowCountOptionsPrivate;
+
+enum {
+  PROP_MODE = 1,
+};
+
+G_DEFINE_TYPE_WITH_PRIVATE(GArrowCountOptions,
+                           garrow_count_options,
+                           G_TYPE_OBJECT)
+
+#define GARROW_COUNT_OPTIONS_GET_PRIVATE(object)        \
+  static_cast<GArrowCountOptionsPrivate *>(             \
+    garrow_count_options_get_instance_private(          \
+      GARROW_COUNT_OPTIONS(object)))
+
+static void
+garrow_count_options_set_property(GObject *object,
+                                  guint prop_id,
+                                  const GValue *value,
+                                  GParamSpec *pspec)
+{
+  auto priv = GARROW_COUNT_OPTIONS_GET_PRIVATE(object);
+
+  switch (prop_id) {
+  case PROP_MODE:
+    priv->options.count_mode =
+      static_cast<arrow::compute::CountOptions::mode>(g_value_get_enum(value));
+    break;
+  default:
+    G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
+    break;
+  }
+}
+
+static void
+garrow_count_options_get_property(GObject *object,
+                                 guint prop_id,
+                                 GValue *value,
+                                 GParamSpec *pspec)
+{
+  auto priv = GARROW_COUNT_OPTIONS_GET_PRIVATE(object);
+
+  switch (prop_id) {
+  case PROP_MODE:
+    g_value_set_enum(value, priv->options.count_mode);
+    break;
+  default:
+    G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
+    break;
+  }
+}
+
+static void
+garrow_count_options_init(GArrowCountOptions *object)
+{
+}
+
+static void
+garrow_count_options_class_init(GArrowCountOptionsClass *klass)
+{
+  auto gobject_class = G_OBJECT_CLASS(klass);
+
+  gobject_class->set_property = garrow_count_options_set_property;
+  gobject_class->get_property = garrow_count_options_get_property;
+
+  GParamSpec *spec;
+  /**
+   * GArrowCountOptions:mode:
+   *
+   * How to count values.
+   *
+   * Since: 0.13.0
+   */
+  spec = g_param_spec_enum("mode",
+                           "Mode",
+                           "How to count values",
+                           GARROW_TYPE_COUNT_MODE,
+                           GARROW_COUNT_ALL,
+                           static_cast<GParamFlags>(G_PARAM_READWRITE));
+  g_object_class_install_property(gobject_class, PROP_MODE, spec);
+}
+
+/**
+ * garrow_count_options_new:
+ *
+ * Returns: A newly created #GArrowCountOptions.
+ *
+ * Since: 0.13.0
+ */
+GArrowCountOptions *
+garrow_count_options_new(void)
+{
+  auto count_options = g_object_new(GARROW_TYPE_COUNT_OPTIONS, NULL);
+  return GARROW_COUNT_OPTIONS(count_options);
+}
+
 G_END_DECLS
 
 GArrowCastOptions *
@@ -215,3 +316,20 @@ garrow_cast_options_get_raw(GArrowCastOptions 
*cast_options)
   auto priv = GARROW_CAST_OPTIONS_GET_PRIVATE(cast_options);
   return &(priv->options);
 }
+
+GArrowCountOptions *
+garrow_count_options_new_raw(arrow::compute::CountOptions *arrow_count_options)
+{
+  auto count_options =
+    g_object_new(GARROW_TYPE_COUNT_OPTIONS,
+                 "mode", arrow_count_options->count_mode,
+                 NULL);
+  return GARROW_COUNT_OPTIONS(count_options);
+}
+
+arrow::compute::CountOptions *
+garrow_count_options_get_raw(GArrowCountOptions *count_options)
+{
+  auto priv = GARROW_COUNT_OPTIONS_GET_PRIVATE(count_options);
+  return &(priv->options);
+}
diff --git a/c_glib/arrow-glib/compute.h b/c_glib/arrow-glib/compute.h
index cd9dd2b..88ba270 100644
--- a/c_glib/arrow-glib/compute.h
+++ b/c_glib/arrow-glib/compute.h
@@ -20,6 +20,7 @@
 #pragma once
 
 #include <arrow-glib/gobject-type.h>
+#include <arrow-glib/version.h>
 
 G_BEGIN_DECLS
 
@@ -36,4 +37,32 @@ struct _GArrowCastOptionsClass
 
 GArrowCastOptions *garrow_cast_options_new(void);
 
+
+/**
+ * GArrowCountMode:
+ * @GARROW_COUNT_ALL: Count all non-null values.
+ * @GARROW_COUNT_NULL: Count all null values.
+ *
+ * They are corresponding to `arrow::compute::CountOptions::mode` values.
+ */
+typedef enum {
+  GARROW_COUNT_ALL,
+  GARROW_COUNT_NULL,
+} GArrowCountMode;
+
+#define GARROW_TYPE_COUNT_OPTIONS (garrow_count_options_get_type())
+G_DECLARE_DERIVABLE_TYPE(GArrowCountOptions,
+                         garrow_count_options,
+                         GARROW,
+                         COUNT_OPTIONS,
+                         GObject)
+struct _GArrowCountOptionsClass
+{
+  GObjectClass parent_class;
+};
+
+GARROW_AVAILABLE_IN_0_13
+GArrowCountOptions *
+garrow_count_options_new(void);
+
 G_END_DECLS
diff --git a/c_glib/arrow-glib/compute.hpp b/c_glib/arrow-glib/compute.hpp
index bdeaad2..495b3f6 100644
--- a/c_glib/arrow-glib/compute.hpp
+++ b/c_glib/arrow-glib/compute.hpp
@@ -25,3 +25,8 @@
 
 GArrowCastOptions *garrow_cast_options_new_raw(arrow::compute::CastOptions 
*arrow_cast_options);
 arrow::compute::CastOptions *garrow_cast_options_get_raw(GArrowCastOptions 
*cast_options);
+
+GArrowCountOptions *
+garrow_count_options_new_raw(arrow::compute::CountOptions 
*arrow_count_options);
+arrow::compute::CountOptions *
+garrow_count_options_get_raw(GArrowCountOptions *count_options);
diff --git a/c_glib/test/test-count.rb b/c_glib/test/test-count.rb
new file mode 100644
index 0000000..4f0f228
--- /dev/null
+++ b/c_glib/test/test-count.rb
@@ -0,0 +1,40 @@
+# 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 TestCount < Test::Unit::TestCase
+  include Helper::Buildable
+  include Helper::Omittable
+
+  sub_test_case("mode") do
+    def test_default
+      require_gi(1, 42, 0)
+      assert_equal(2, build_int32_array([1, nil, 3]).count)
+    end
+
+    def test_all
+      options = Arrow::CountOptions.new
+      options.mode = :all
+      assert_equal(2, build_int32_array([1, nil, 3]).count(options))
+    end
+
+    def test_null
+      options = Arrow::CountOptions.new
+      options.mode = :null
+      assert_equal(1, build_int32_array([1, nil, 3]).count(options))
+    end
+  end
+end
diff --git a/cpp/src/arrow/compute/api.h b/cpp/src/arrow/compute/api.h
index 42839ce..fb2b3c9 100644
--- a/cpp/src/arrow/compute/api.h
+++ b/cpp/src/arrow/compute/api.h
@@ -23,6 +23,7 @@
 
 #include "arrow/compute/kernels/boolean.h"  // IWYU pragma: export
 #include "arrow/compute/kernels/cast.h"     // IWYU pragma: export
+#include "arrow/compute/kernels/count.h"    // IWYU pragma: export
 #include "arrow/compute/kernels/hash.h"     // IWYU pragma: export
 #include "arrow/compute/kernels/mean.h"     // IWYU pragma: export
 

Reply via email to