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

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


The following commit(s) were added to refs/heads/main by this push:
     new 73d8718f6c GH-48504: [GLib][Ruby] Add SelectKOptions (#48525)
73d8718f6c is described below

commit 73d8718f6c19493cfb0ffd49252f9b435ae0dc6b
Author: Sten Larsson <[email protected]>
AuthorDate: Thu Jan 1 02:02:14 2026 +0100

    GH-48504: [GLib][Ruby] Add SelectKOptions (#48525)
    
    ### Rationale for this change
    
    The `SelectKOptions` class is not available in GLib/Ruby, and it is used 
together with the `select_k_unstable` compute function.
    
    ### What changes are included in this PR?
    
    This adds the `SelectKOptions` class to GLib.
    
    ### Are these changes tested?
    
    Yes, with Ruby unit tests.
    
    ### Are there any user-facing changes?
    
    Yes, a new class.
    
    * GitHub Issue: #48504
    
    Authored-by: Sten Larsson <[email protected]>
    Signed-off-by: Sutou Kouhei <[email protected]>
---
 c_glib/arrow-glib/compute.cpp        | 165 +++++++++++++++++++++++++++++++++++
 c_glib/arrow-glib/compute.h          |  29 ++++++
 c_glib/arrow-glib/compute.hpp        |   5 ++
 c_glib/test/test-select-k-options.rb |  61 +++++++++++++
 4 files changed, 260 insertions(+)

diff --git a/c_glib/arrow-glib/compute.cpp b/c_glib/arrow-glib/compute.cpp
index 164e1e641c..a2879f7b22 100644
--- a/c_glib/arrow-glib/compute.cpp
+++ b/c_glib/arrow-glib/compute.cpp
@@ -318,6 +318,9 @@ G_BEGIN_DECLS
  * #GArrowRoundTemporalOptions is a class to customize the `round_temporal`,
  * `floor_temporal`, and `ceil_temporal` functions.
  *
+ * #GArrowSelectKOptions is a class to customize the
+ * `select_k_unstable` function.
+ *
  * There are many functions to compute data on an array.
  */
 
@@ -9358,6 +9361,146 @@ garrow_round_temporal_options_new(void)
     g_object_new(GARROW_TYPE_ROUND_TEMPORAL_OPTIONS, nullptr));
 }
 
+enum {
+  PROP_SELECT_K_OPTIONS_K = 1,
+};
+
+G_DEFINE_TYPE(GArrowSelectKOptions, garrow_select_k_options, 
GARROW_TYPE_FUNCTION_OPTIONS)
+
+static void
+garrow_select_k_options_set_property(GObject *object,
+                                     guint prop_id,
+                                     const GValue *value,
+                                     GParamSpec *pspec)
+{
+  auto options = 
garrow_select_k_options_get_raw(GARROW_SELECT_K_OPTIONS(object));
+
+  switch (prop_id) {
+  case PROP_SELECT_K_OPTIONS_K:
+    options->k = g_value_get_int64(value);
+    break;
+  default:
+    G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
+    break;
+  }
+}
+
+static void
+garrow_select_k_options_get_property(GObject *object,
+                                     guint prop_id,
+                                     GValue *value,
+                                     GParamSpec *pspec)
+{
+  auto options = 
garrow_select_k_options_get_raw(GARROW_SELECT_K_OPTIONS(object));
+
+  switch (prop_id) {
+  case PROP_SELECT_K_OPTIONS_K:
+    g_value_set_int64(value, options->k);
+    break;
+  default:
+    G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
+    break;
+  }
+}
+
+static void
+garrow_select_k_options_init(GArrowSelectKOptions *object)
+{
+  auto arrow_priv = GARROW_FUNCTION_OPTIONS_GET_PRIVATE(object);
+  arrow_priv->options =
+    static_cast<arrow::compute::FunctionOptions *>(new 
arrow::compute::SelectKOptions());
+}
+
+static void
+garrow_select_k_options_class_init(GArrowSelectKOptionsClass *klass)
+{
+  auto gobject_class = G_OBJECT_CLASS(klass);
+
+  gobject_class->set_property = garrow_select_k_options_set_property;
+  gobject_class->get_property = garrow_select_k_options_get_property;
+
+  arrow::compute::SelectKOptions options;
+
+  GParamSpec *spec;
+  /**
+   * GArrowSelectKOptions:k:
+   *
+   * The number of k elements to keep.
+   *
+   * Since: 23.0.0
+   */
+  spec = g_param_spec_int64("k",
+                            "K",
+                            "The number of k elements to keep",
+                            G_MININT64,
+                            G_MAXINT64,
+                            options.k,
+                            static_cast<GParamFlags>(G_PARAM_READWRITE));
+  g_object_class_install_property(gobject_class, PROP_SELECT_K_OPTIONS_K, 
spec);
+}
+
+/**
+ * garrow_select_k_options_new:
+ *
+ * Returns: A newly created #GArrowSelectKOptions.
+ *
+ * Since: 23.0.0
+ */
+GArrowSelectKOptions *
+garrow_select_k_options_new(void)
+{
+  return GARROW_SELECT_K_OPTIONS(g_object_new(GARROW_TYPE_SELECT_K_OPTIONS, 
nullptr));
+}
+
+/**
+ * garrow_select_k_options_get_sort_keys:
+ * @options: A #GArrowSelectKOptions.
+ *
+ * Returns: (transfer full) (element-type GArrowSortKey):
+ *   The sort keys to be used.
+ *
+ * Since: 23.0.0
+ */
+GList *
+garrow_select_k_options_get_sort_keys(GArrowSelectKOptions *options)
+{
+  auto arrow_options = garrow_select_k_options_get_raw(options);
+  return garrow_sort_keys_new_raw(arrow_options->sort_keys);
+}
+
+/**
+ * garrow_select_k_options_set_sort_keys:
+ * @options: A #GArrowSelectKOptions.
+ * @sort_keys: (element-type GArrowSortKey): The sort keys to be used.
+ *
+ * Set sort keys to be used.
+ *
+ * Since: 23.0.0
+ */
+void
+garrow_select_k_options_set_sort_keys(GArrowSelectKOptions *options, GList 
*sort_keys)
+{
+  auto arrow_options = garrow_select_k_options_get_raw(options);
+  garrow_raw_sort_keys_set(arrow_options->sort_keys, sort_keys);
+}
+
+/**
+ * garrow_select_k_options_add_sort_key:
+ * @options: A #GArrowSelectKOptions.
+ * @sort_key: The sort key to be added.
+ *
+ * Add a sort key to be used.
+ *
+ * Since: 23.0.0
+ */
+void
+garrow_select_k_options_add_sort_key(GArrowSelectKOptions *options,
+                                     GArrowSortKey *sort_key)
+{
+  auto arrow_options = garrow_select_k_options_get_raw(options);
+  garrow_raw_sort_keys_add(arrow_options->sort_keys, sort_key);
+}
+
 G_END_DECLS
 
 arrow::Result<arrow::FieldRef>
@@ -9598,6 +9741,11 @@ garrow_function_options_new_raw(const 
arrow::compute::FunctionOptions *arrow_opt
       static_cast<const arrow::compute::RoundTemporalOptions *>(arrow_options);
     auto options = 
garrow_round_temporal_options_new_raw(arrow_round_temporal_options);
     return GARROW_FUNCTION_OPTIONS(options);
+  } else if (arrow_type_name == "SelectKOptions") {
+    const auto arrow_select_k_options =
+      static_cast<const arrow::compute::SelectKOptions *>(arrow_options);
+    auto options = garrow_select_k_options_new_raw(arrow_select_k_options);
+    return GARROW_FUNCTION_OPTIONS(options);
   } else {
     auto options = g_object_new(GARROW_TYPE_FUNCTION_OPTIONS, NULL);
     return GARROW_FUNCTION_OPTIONS(options);
@@ -10574,3 +10722,20 @@ 
garrow_round_temporal_options_get_raw(GArrowRoundTemporalOptions *options)
   return static_cast<arrow::compute::RoundTemporalOptions *>(
     garrow_function_options_get_raw(GARROW_FUNCTION_OPTIONS(options)));
 }
+
+GArrowSelectKOptions *
+garrow_select_k_options_new_raw(const arrow::compute::SelectKOptions 
*arrow_options)
+{
+  auto options = GARROW_SELECT_K_OPTIONS(
+    g_object_new(GARROW_TYPE_SELECT_K_OPTIONS, "k", arrow_options->k, 
nullptr));
+  auto arrow_new_options = garrow_select_k_options_get_raw(options);
+  arrow_new_options->sort_keys = arrow_options->sort_keys;
+  return options;
+}
+
+arrow::compute::SelectKOptions *
+garrow_select_k_options_get_raw(GArrowSelectKOptions *options)
+{
+  return static_cast<arrow::compute::SelectKOptions *>(
+    garrow_function_options_get_raw(GARROW_FUNCTION_OPTIONS(options)));
+}
diff --git a/c_glib/arrow-glib/compute.h b/c_glib/arrow-glib/compute.h
index 45442bf1aa..c50c6d93e0 100644
--- a/c_glib/arrow-glib/compute.h
+++ b/c_glib/arrow-glib/compute.h
@@ -1649,4 +1649,33 @@ GARROW_AVAILABLE_IN_23_0
 GArrowRoundTemporalOptions *
 garrow_round_temporal_options_new(void);
 
+#define GARROW_TYPE_SELECT_K_OPTIONS (garrow_select_k_options_get_type())
+GARROW_AVAILABLE_IN_23_0
+G_DECLARE_DERIVABLE_TYPE(GArrowSelectKOptions,
+                         garrow_select_k_options,
+                         GARROW,
+                         SELECT_K_OPTIONS,
+                         GArrowFunctionOptions)
+struct _GArrowSelectKOptionsClass
+{
+  GArrowFunctionOptionsClass parent_class;
+};
+
+GARROW_AVAILABLE_IN_23_0
+GArrowSelectKOptions *
+garrow_select_k_options_new(void);
+
+GARROW_AVAILABLE_IN_23_0
+GList *
+garrow_select_k_options_get_sort_keys(GArrowSelectKOptions *options);
+
+GARROW_AVAILABLE_IN_23_0
+void
+garrow_select_k_options_set_sort_keys(GArrowSelectKOptions *options, GList 
*sort_keys);
+
+GARROW_AVAILABLE_IN_23_0
+void
+garrow_select_k_options_add_sort_key(GArrowSelectKOptions *options,
+                                     GArrowSortKey *sort_key);
+
 G_END_DECLS
diff --git a/c_glib/arrow-glib/compute.hpp b/c_glib/arrow-glib/compute.hpp
index 3a8fc0ff2d..d47464fdfd 100644
--- a/c_glib/arrow-glib/compute.hpp
+++ b/c_glib/arrow-glib/compute.hpp
@@ -298,3 +298,8 @@ garrow_round_temporal_options_new_raw(
   const arrow::compute::RoundTemporalOptions *arrow_options);
 arrow::compute::RoundTemporalOptions *
 garrow_round_temporal_options_get_raw(GArrowRoundTemporalOptions *options);
+
+GArrowSelectKOptions *
+garrow_select_k_options_new_raw(const arrow::compute::SelectKOptions 
*arrow_options);
+arrow::compute::SelectKOptions *
+garrow_select_k_options_get_raw(GArrowSelectKOptions *options);
diff --git a/c_glib/test/test-select-k-options.rb 
b/c_glib/test/test-select-k-options.rb
new file mode 100644
index 0000000000..78c17bf1be
--- /dev/null
+++ b/c_glib/test/test-select-k-options.rb
@@ -0,0 +1,61 @@
+# 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 TestSelectKOptions < Test::Unit::TestCase
+  include Helper::Buildable
+
+  def setup
+    @options = Arrow::SelectKOptions.new
+  end
+
+  def test_k
+    assert_equal(-1, @options.k)
+    @options.k = 3
+    assert_equal(3, @options.k)
+  end
+
+  def test_sort_keys
+    sort_keys = [
+      Arrow::SortKey.new("column1", :ascending),
+      Arrow::SortKey.new("column2", :descending),
+    ]
+    @options.sort_keys = sort_keys
+    assert_equal(sort_keys, @options.sort_keys)
+  end
+
+  def test_add_sort_key
+    @options.add_sort_key(Arrow::SortKey.new("column1", :ascending))
+    @options.add_sort_key(Arrow::SortKey.new("column2", :descending))
+    assert_equal([
+                   Arrow::SortKey.new("column1", :ascending),
+                   Arrow::SortKey.new("column2", :descending),
+                 ],
+                 @options.sort_keys)
+  end
+
+  def test_select_k_unstable_function
+    input_array = build_int32_array([5, 2, 8, 1, 9, 3])
+    args = [
+      Arrow::ArrayDatum.new(input_array),
+    ]
+    @options.k = 3
+    @options.add_sort_key(Arrow::SortKey.new("dummy", :descending))
+    select_k_unstable_function = Arrow::Function.find("select_k_unstable")
+    result = select_k_unstable_function.execute(args, @options).value
+    assert_equal(build_uint64_array([4, 2, 0]), result)
+  end
+end

Reply via email to