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/24 02:24:12 UTC

[arrow] branch master updated: ARROW-5203: [GLib] Add support for Compare filter

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 e64a4b7  ARROW-5203: [GLib] Add support for Compare filter
e64a4b7 is described below

commit e64a4b7c4fb5b67967e3ac35c2f769b6059be628
Author: Yosuke Shiro <yo...@gmail.com>
AuthorDate: Wed Apr 24 11:23:55 2019 +0900

    ARROW-5203: [GLib] Add support for Compare filter
    
    Author: Yosuke Shiro <yo...@gmail.com>
    
    Closes #4191 from shiro615/glib-compare and squashes the following commits:
    
    56ace4f3 <Yosuke Shiro>  Add support for Compare filter
---
 c_glib/arrow-glib/compute.cpp           | 393 ++++++++++++++++++++++++++++++++
 c_glib/arrow-glib/compute.h             |  96 ++++++++
 c_glib/arrow-glib/compute.hpp           |   3 +
 c_glib/test/test-compare.rb             |  62 +++++
 cpp/src/arrow/compute/api.h             |   1 +
 cpp/src/arrow/compute/kernels/compare.h |   4 +-
 6 files changed, 557 insertions(+), 2 deletions(-)

diff --git a/c_glib/arrow-glib/compute.cpp b/c_glib/arrow-glib/compute.cpp
index b339834..73adccb 100644
--- a/c_glib/arrow-glib/compute.cpp
+++ b/c_glib/arrow-glib/compute.cpp
@@ -55,6 +55,32 @@ garrow_numeric_array_sum(GArrowArrayType array,
   }
 }
 
+template <typename GArrowArrayType, typename VALUE>
+GArrowBooleanArray *
+garrow_numeric_array_compare(GArrowArrayType array,
+                             VALUE value,
+                             GArrowCompareOptions *options,
+                             GError **error,
+                             const gchar *tag)
+{
+  auto arrow_array = garrow_array_get_raw(GARROW_ARRAY(array));
+  auto memory_pool = arrow::default_memory_pool();
+  arrow::compute::FunctionContext context(memory_pool);
+  arrow::compute::Datum compared_datum;
+  auto arrow_options = garrow_compare_options_get_raw(options);
+  auto status = arrow::compute::Compare(&context,
+                                        arrow_array,
+                                        arrow::compute::Datum(value),
+                                        *arrow_options,
+                                        &compared_datum);
+  if (garrow_error_check(error, status, tag)) {
+    auto arrow_compared_array = compared_datum.make_array();
+    return GARROW_BOOLEAN_ARRAY(garrow_array_new_raw(&arrow_compared_array));
+  } else {
+    return NULL;
+  }
+}
+
 G_BEGIN_DECLS
 
 /**
@@ -367,6 +393,105 @@ garrow_take_options_new(void)
 }
 
 
+typedef struct GArrowCompareOptionsPrivate_ {
+  arrow::compute::CompareOptions options;
+} GArrowCompareOptionsPrivate;
+
+enum {
+  PROP_OPERATOR = 1,
+};
+
+G_DEFINE_TYPE_WITH_PRIVATE(GArrowCompareOptions,
+                           garrow_compare_options,
+                           G_TYPE_OBJECT)
+
+#define GARROW_COMPARE_OPTIONS_GET_PRIVATE(object)        \
+  static_cast<GArrowCompareOptionsPrivate *>(             \
+    garrow_compare_options_get_instance_private(          \
+      GARROW_COMPARE_OPTIONS(object)))
+
+static void
+garrow_compare_options_set_property(GObject *object,
+                                    guint prop_id,
+                                    const GValue *value,
+                                    GParamSpec *pspec)
+{
+  auto priv = GARROW_COMPARE_OPTIONS_GET_PRIVATE(object);
+
+  switch (prop_id) {
+  case PROP_OPERATOR:
+    priv->options.op =
+      static_cast<arrow::compute::CompareOperator>(g_value_get_enum(value));
+    break;
+  default:
+    G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
+    break;
+  }
+}
+
+static void
+garrow_compare_options_get_property(GObject *object,
+                                    guint prop_id,
+                                    GValue *value,
+                                    GParamSpec *pspec)
+{
+  auto priv = GARROW_COMPARE_OPTIONS_GET_PRIVATE(object);
+
+  switch (prop_id) {
+  case PROP_OPERATOR:
+    g_value_set_enum(value, priv->options.op);
+    break;
+  default:
+    G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
+    break;
+  }
+}
+
+static void
+garrow_compare_options_init(GArrowCompareOptions *object)
+{
+}
+
+static void
+garrow_compare_options_class_init(GArrowCompareOptionsClass *klass)
+{
+  auto gobject_class = G_OBJECT_CLASS(klass);
+
+  gobject_class->set_property = garrow_compare_options_set_property;
+  gobject_class->get_property = garrow_compare_options_get_property;
+
+  GParamSpec *spec;
+  /**
+   * GArrowCompareOptions:operator:
+   *
+   * How to compare the value.
+   *
+   * Since: 0.14.0
+   */
+  spec = g_param_spec_enum("operator",
+                           "Operator",
+                           "How to compare the value",
+                           GARROW_TYPE_COMPARE_OPERATOR,
+                           0,
+                           static_cast<GParamFlags>(G_PARAM_READWRITE));
+  g_object_class_install_property(gobject_class, PROP_OPERATOR, spec);
+}
+
+/**
+ * garrow_compare_options_new:
+ *
+ * Returns: A newly created #GArrowCompareOptions.
+ *
+ * Since: 0.14.0
+ */
+GArrowCompareOptions *
+garrow_compare_options_new(void)
+{
+  auto compare_options = g_object_new(GARROW_TYPE_COMPARE_OPTIONS, NULL);
+  return GARROW_COMPARE_OPTIONS(compare_options);
+}
+
+
 /**
  * garrow_array_cast:
  * @array: A #GArrowArray.
@@ -990,6 +1115,267 @@ garrow_array_take(GArrowArray *array,
 }
 
 
+/**
+ * garrow_int8_array_compare:
+ * @array: A #GArrowInt8Array.
+ * @value: The value to compare.
+ * @options: A #GArrowCompareOptions.
+ * @error: (nullable): Return location for a #GError or %NULL.
+ *
+ * Returns: (nullable) (transfer full): The #GArrowBooleanArray as
+ *   the result compared a numeric array with a scalar on success,
+ *   %NULL on error.
+ *
+ * Since: 0.14.0
+ */
+GArrowBooleanArray *
+garrow_int8_array_compare(GArrowInt8Array *array,
+                          gint8 value,
+                          GArrowCompareOptions *options,
+                          GError **error)
+{
+  return garrow_numeric_array_compare(array,
+                                      value,
+                                      options,
+                                      error,
+                                      "[int8-array][compare]");
+}
+
+/**
+ * garrow_uint8_array_compare:
+ * @array: A #GArrowUInt8Array.
+ * @value: The value to compare.
+ * @options: A #GArrowCompareOptions.
+ * @error: (nullable): Return location for a #GError or %NULL.
+ *
+ * Returns: (nullable) (transfer full): The #GArrowBooleanArray as
+ *   the result compared a numeric array with a scalar on success,
+ *   %NULL on error.
+ *
+ * Since: 0.14.0
+ */
+GArrowBooleanArray *
+garrow_uint8_array_compare(GArrowUInt8Array *array,
+                           guint8 value,
+                           GArrowCompareOptions *options,
+                           GError **error)
+{
+  return garrow_numeric_array_compare(array,
+                                      value,
+                                      options,
+                                      error,
+                                      "[uint8-array][compare]");
+}
+
+/**
+ * garrow_int16_array_compare:
+ * @array: A #GArrowInt16Array.
+ * @value: The value to compare.
+ * @options: A #GArrowCompareOptions.
+ * @error: (nullable): Return location for a #GError or %NULL.
+ *
+ * Returns: (nullable) (transfer full): The #GArrowBooleanArray as
+ *   the result compared a numeric array with a scalar on success,
+ *   %NULL on error.
+ *
+ * Since: 0.14.0
+ */
+GArrowBooleanArray *
+garrow_int16_array_compare(GArrowInt16Array *array,
+                           gint16 value,
+                           GArrowCompareOptions *options,
+                           GError **error)
+{
+  return garrow_numeric_array_compare(array,
+                                      value,
+                                      options,
+                                      error,
+                                      "[int16-array][compare]");
+}
+
+/**
+ * garrow_uint16_array_compare:
+ * @array: A #GArrowUInt16Array.
+ * @value: The value to compare.
+ * @options: A #GArrowCompareOptions.
+ * @error: (nullable): Return location for a #GError or %NULL.
+ *
+ * Returns: (nullable) (transfer full): The #GArrowBooleanArray as
+ *   the result compared a numeric array with a scalar on success,
+ *   %NULL on error.
+ *
+ * Since: 0.14.0
+ */
+GArrowBooleanArray *
+garrow_uint16_array_compare(GArrowUInt16Array *array,
+                            guint16 value,
+                            GArrowCompareOptions *options,
+                            GError **error)
+{
+  return garrow_numeric_array_compare(array,
+                                      value,
+                                      options,
+                                      error,
+                                      "[uint16-array][compare]");
+}
+
+/**
+ * garrow_int32_array_compare:
+ * @array: A #GArrowUInt32Array.
+ * @value: The value to compare.
+ * @options: A #GArrowCompareOptions.
+ * @error: (nullable): Return location for a #GError or %NULL.
+ *
+ * Returns: (nullable) (transfer full): The #GArrowBooleanArray as
+ *   the result compared a numeric array with a scalar on success,
+ *   %NULL on error.
+ *
+ * Since: 0.14.0
+ */
+GArrowBooleanArray *
+garrow_int32_array_compare(GArrowInt32Array *array,
+                           gint32 value,
+                           GArrowCompareOptions *options,
+                           GError **error)
+{
+  return garrow_numeric_array_compare(array,
+                                      value,
+                                      options,
+                                      error,
+                                      "[int32-array][compare]");
+}
+
+/**
+ * garrow_uint32_array_compare:
+ * @array: A #GArrowUInt32Array.
+ * @value: The value to compare.
+ * @options: A #GArrowCompareOptions.
+ * @error: (nullable): Return location for a #GError or %NULL.
+ *
+ * Returns: (nullable) (transfer full): The #GArrowBooleanArray as
+ *   the result compared a numeric array with a scalar on success,
+ *   %NULL on error.
+ *
+ * Since: 0.14.0
+ */
+GArrowBooleanArray *
+garrow_uint32_array_compare(GArrowUInt32Array *array,
+                            guint32 value,
+                            GArrowCompareOptions *options,
+                            GError **error)
+{
+  return garrow_numeric_array_compare(array,
+                                      value,
+                                      options,
+                                      error,
+                                      "[uint32-array][compare]");
+}
+
+/**
+ * garrow_int64_array_compare:
+ * @array: A #GArrowInt64Array.
+ * @value: The value to compare.
+ * @options: A #GArrowCompareOptions.
+ * @error: (nullable): Return location for a #GError or %NULL.
+ *
+ * Returns: (nullable) (transfer full): The #GArrowBooleanArray as
+ *   the result compared a numeric array with a scalar on success,
+ *   %NULL on error.
+ *
+ * Since: 0.14.0
+ */
+GArrowBooleanArray *
+garrow_int64_array_compare(GArrowInt64Array *array,
+                           gint64 value,
+                           GArrowCompareOptions *options,
+                           GError **error)
+{
+  return garrow_numeric_array_compare(array,
+                                      value,
+                                      options,
+                                      error,
+                                      "[int64-array][compare]");
+}
+
+/**
+ * garrow_uint64_array_compare:
+ * @array: A #GArrowUInt64Array.
+ * @value: The value to compare.
+ * @options: A #GArrowCompareOptions.
+ * @error: (nullable): Return location for a #GError or %NULL.
+ *
+ * Returns: (nullable) (transfer full): The #GArrowBooleanArray as
+ *   the result compared a numeric array with a scalar on success,
+ *   %NULL on error.
+ *
+ * Since: 0.14.0
+ */
+GArrowBooleanArray *
+garrow_uint64_array_compare(GArrowUInt64Array *array,
+                            guint64 value,
+                            GArrowCompareOptions *options,
+                            GError **error)
+{
+  return garrow_numeric_array_compare(array,
+                                      value,
+                                      options,
+                                      error,
+                                      "[uint64-array][compare]");
+}
+
+/**
+ * garrow_float_array_compare:
+ * @array: A #GArrowFloatArray.
+ * @value: The value to compare.
+ * @options: A #GArrowCompareOptions.
+ * @error: (nullable): Return location for a #GError or %NULL.
+ *
+ * Returns: (nullable) (transfer full): The #GArrowBooleanArray as
+ *   the result compared a numeric array with a scalar on success,
+ *   %NULL on error.
+ *
+ * Since: 0.14.0
+ */
+GArrowBooleanArray *
+garrow_float_array_compare(GArrowFloatArray *array,
+                           gfloat value,
+                           GArrowCompareOptions *options,
+                           GError **error)
+{
+  return garrow_numeric_array_compare(array,
+                                      value,
+                                      options,
+                                      error,
+                                      "[float-array][compare]");
+}
+
+/**
+ * garrow_double_array_compare:
+ * @array: A #GArrowDoubleArray.
+ * @value: The value to compare.
+ * @options: A #GArrowCompareOptions.
+ * @error: (nullable): Return location for a #GError or %NULL.
+ *
+ * Returns: (nullable) (transfer full): The #GArrowBooleanArray as
+ *   the result compared a numeric array with a scalar on success,
+ *   %NULL on error.
+ *
+ * Since: 0.14.0
+ */
+GArrowBooleanArray *
+garrow_double_array_compare(GArrowDoubleArray *array,
+                            gdouble value,
+                            GArrowCompareOptions *options,
+                            GError **error)
+{
+  return garrow_numeric_array_compare(array,
+                                      value,
+                                      options,
+                                      error,
+                                      "[double-array][compare]");
+}
+
+
 G_END_DECLS
 
 GArrowCastOptions *
@@ -1035,3 +1421,10 @@ garrow_take_options_get_raw(GArrowTakeOptions *take_options)
   auto priv = GARROW_TAKE_OPTIONS_GET_PRIVATE(take_options);
   return &(priv->options);
 }
+
+arrow::compute::CompareOptions *
+garrow_compare_options_get_raw(GArrowCompareOptions *compare_options)
+{
+  auto priv = GARROW_COMPARE_OPTIONS_GET_PRIVATE(compare_options);
+  return &(priv->options);
+}
diff --git a/c_glib/arrow-glib/compute.h b/c_glib/arrow-glib/compute.h
index d7bdd43..79d4b63 100644
--- a/c_glib/arrow-glib/compute.h
+++ b/c_glib/arrow-glib/compute.h
@@ -81,6 +81,42 @@ GArrowTakeOptions *
 garrow_take_options_new(void);
 
 
+/**
+ * GArrowCompareOperator:
+ * @GARROW_COMPARE_EQUAL: Equal operator.
+ * @GARROW_COMPARE_NOT_EQUAL: Not equal operator.
+ * @GARROW_COMPARE_GREATER: Greater operator.
+ * @GARROW_COMPARE_GREATER_EQUAL: Greater equal operator.
+ * @GARROW_COMPARE_LESS: Less operator.
+ * @GARROW_COMPARE_LESS_EQUAL: Less equal operator.
+ *
+ * They are corresponding to `arrow::compute::CompareOperator` values.
+ */
+typedef enum {
+  GARROW_COMPARE_EQUAL,
+  GARROW_COMPARE_NOT_EQUAL,
+  GARROW_COMPARE_GREATER,
+  GARROW_COMPARE_GREATER_EQUAL,
+  GARROW_COMPARE_LESS,
+  GARROW_COMPARE_LESS_EQUAL
+} GArrowCompareOperator;
+
+#define GARROW_TYPE_COMPARE_OPTIONS (garrow_compare_options_get_type())
+G_DECLARE_DERIVABLE_TYPE(GArrowCompareOptions,
+                         garrow_compare_options,
+                         GARROW,
+                         COMPARE_OPTIONS,
+                         GObject)
+struct _GArrowCompareOptionsClass
+{
+  GObjectClass parent_class;
+};
+
+GARROW_AVAILABLE_IN_0_14
+GArrowCompareOptions *
+garrow_compare_options_new(void);
+
+
 GArrowArray *garrow_array_cast(GArrowArray *array,
                                GArrowDataType *target_data_type,
                                GArrowCastOptions *options,
@@ -152,5 +188,65 @@ GArrowArray *garrow_array_take(GArrowArray *array,
                                GArrowArray *indices,
                                GArrowTakeOptions *options,
                                GError **error);
+GARROW_AVAILABLE_IN_0_14
+GArrowBooleanArray *
+garrow_int8_array_compare(GArrowInt8Array *array,
+                          gint8 value,
+                          GArrowCompareOptions *options,
+                          GError **error);
+GARROW_AVAILABLE_IN_0_14
+GArrowBooleanArray *
+garrow_uint8_array_compare(GArrowUInt8Array *array,
+                           guint8 value,
+                           GArrowCompareOptions *options,
+                           GError **error);
+GARROW_AVAILABLE_IN_0_14
+GArrowBooleanArray *
+garrow_int16_array_compare(GArrowInt16Array *array,
+                           gint16 value,
+                           GArrowCompareOptions *options,
+                           GError **error);
+GARROW_AVAILABLE_IN_0_14
+GArrowBooleanArray *
+garrow_uint16_array_compare(GArrowUInt16Array *array,
+                            guint16 value,
+                            GArrowCompareOptions *options,
+                            GError **error);
+GARROW_AVAILABLE_IN_0_14
+GArrowBooleanArray *
+garrow_int32_array_compare(GArrowInt32Array *array,
+                           gint32 value,
+                           GArrowCompareOptions *options,
+                           GError **error);
+GARROW_AVAILABLE_IN_0_14
+GArrowBooleanArray *
+garrow_uint32_array_compare(GArrowUInt32Array *array,
+                            guint32 value,
+                            GArrowCompareOptions *options,
+                            GError **error);
+GARROW_AVAILABLE_IN_0_14
+GArrowBooleanArray *
+garrow_int64_array_compare(GArrowInt64Array *array,
+                           gint64 value,
+                           GArrowCompareOptions *options,
+                           GError **error);
+GARROW_AVAILABLE_IN_0_14
+GArrowBooleanArray *
+garrow_uint64_array_compare(GArrowUInt64Array *array,
+                            guint64 value,
+                            GArrowCompareOptions *options,
+                            GError **error);
+GARROW_AVAILABLE_IN_0_14
+GArrowBooleanArray *
+garrow_float_array_compare(GArrowFloatArray *array,
+                           gfloat value,
+                           GArrowCompareOptions *options,
+                           GError **error);
+GARROW_AVAILABLE_IN_0_14
+GArrowBooleanArray *
+garrow_double_array_compare(GArrowDoubleArray *array,
+                            gdouble value,
+                            GArrowCompareOptions *options,
+                            GError **error);
 
 G_END_DECLS
diff --git a/c_glib/arrow-glib/compute.hpp b/c_glib/arrow-glib/compute.hpp
index a3ce56c..1251225 100644
--- a/c_glib/arrow-glib/compute.hpp
+++ b/c_glib/arrow-glib/compute.hpp
@@ -33,3 +33,6 @@ garrow_count_options_get_raw(GArrowCountOptions *count_options);
 
 arrow::compute::TakeOptions *
 garrow_take_options_get_raw(GArrowTakeOptions *take_options);
+
+arrow::compute::CompareOptions *
+garrow_compare_options_get_raw(GArrowCompareOptions *compare_options);
diff --git a/c_glib/test/test-compare.rb b/c_glib/test/test-compare.rb
new file mode 100644
index 0000000..cde00e0
--- /dev/null
+++ b/c_glib/test/test-compare.rb
@@ -0,0 +1,62 @@
+# 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 TestComapre < Test::Unit::TestCase
+  include Helper::Buildable
+
+  def setup
+    @options = Arrow::CompareOptions.new
+  end
+
+  sub_test_case("operator") do
+    def test_equal
+      @options.operator = :equal
+      assert_equal(build_boolean_array([true, nil, false]),
+                   build_int32_array([1, nil, 3]).compare(1, @options))
+    end
+
+    def test_not_equal
+      @options.operator = :not_equal
+      assert_equal(build_boolean_array([false, nil, true]),
+                   build_int32_array([1, nil, 3]).compare(1, @options))
+    end
+
+    def test_greater
+      @options.operator = :greater
+      assert_equal(build_boolean_array([false, nil, true]),
+                   build_int32_array([1, nil, 3]).compare(1, @options))
+    end
+
+    def test_greater_equal
+      @options.operator = :greater_equal
+      assert_equal(build_boolean_array([true, nil, true]),
+                   build_int32_array([1, nil, 3]).compare(1, @options))
+    end
+
+    def test_less
+      @options.operator = :less
+      assert_equal(build_boolean_array([false, nil, false]),
+                   build_int32_array([1, nil, 3]).compare(1, @options))
+    end
+
+    def test_less_equal
+      @options.operator = :less_equal
+      assert_equal(build_boolean_array([true, nil, false]),
+                   build_int32_array([1, nil, 3]).compare(1, @options))
+    end
+  end
+end
diff --git a/cpp/src/arrow/compute/api.h b/cpp/src/arrow/compute/api.h
index eb0e789..2a2e79f 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/compare.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
diff --git a/cpp/src/arrow/compute/kernels/compare.h b/cpp/src/arrow/compute/kernels/compare.h
index 06e71d6..a192451 100644
--- a/cpp/src/arrow/compute/kernels/compare.h
+++ b/cpp/src/arrow/compute/kernels/compare.h
@@ -88,7 +88,7 @@ struct CompareOptions {
 /// \param[in] type required to specialize the kernel
 /// \param[in] options required to specify the compare operator
 ///
-/// \since 0.13.0
+/// \since 0.14.0
 /// \note API not yet finalized
 ARROW_EXPORT
 std::shared_ptr<FilterFunction> MakeCompareFilterFunction(FunctionContext* context,
@@ -106,7 +106,7 @@ std::shared_ptr<FilterFunction> MakeCompareFilterFunction(FunctionContext* conte
 ///
 /// Note on floating point arrays, this uses ieee-754 compare semantics.
 ///
-/// \since 0.13.0
+/// \since 0.14.0
 /// \note API not yet finalized
 ARROW_EXPORT
 Status Compare(FunctionContext* context, const Datum& left, const Datum& right,