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 9b03118e83 GH-48501: [GLib][Ruby] Add ReplaceSubstringOptions (#48522)
9b03118e83 is described below

commit 9b03118e834dfdaa0cf9e03595477b499252a9cb
Author: Sten Larsson <[email protected]>
AuthorDate: Wed Dec 31 12:50:11 2025 +0100

    GH-48501: [GLib][Ruby] Add ReplaceSubstringOptions (#48522)
    
    ### Rationale for this change
    
    The `ReplaceSubstringOptions` class is not available in GLib/Ruby, and it 
is used together with the `replace_substring` compute function.
    
    ### What changes are included in this PR?
    
    This adds the `ReplaceSubstringOptions` class to GLib.
    
    ### Are these changes tested?
    
    Yes, with Ruby unit tests.
    
    ### Are there any user-facing changes?
    
    Yes, a new class.
    
    * GitHub Issue: #48501
    
    Lead-authored-by: Sten Larsson <[email protected]>
    Co-authored-by: Sutou Kouhei <[email protected]>
    Signed-off-by: Sutou Kouhei <[email protected]>
---
 c_glib/arrow-glib/compute.cpp                 | 176 ++++++++++++++++++++++++++
 c_glib/arrow-glib/compute.h                   |  17 +++
 c_glib/arrow-glib/compute.hpp                 |   6 +
 c_glib/test/test-replace-substring-options.rb |  67 ++++++++++
 4 files changed, 266 insertions(+)

diff --git a/c_glib/arrow-glib/compute.cpp b/c_glib/arrow-glib/compute.cpp
index 7687d2e6a1..da390781b0 100644
--- a/c_glib/arrow-glib/compute.cpp
+++ b/c_glib/arrow-glib/compute.cpp
@@ -309,6 +309,9 @@ G_BEGIN_DECLS
  * #GArrowRankQuantileOptions is a class to customize the `rank_quantile` and
  * `rank_normal` functions.
  *
+ * #GArrowReplaceSubstringOptions is a class to customize the
+ * `replace_substring` and `replace_substring_regex` functions.
+ *
  * There are many functions to compute data on an array.
  */
 
@@ -8914,6 +8917,151 @@ 
garrow_rank_quantile_options_add_sort_key(GArrowRankQuantileOptions *options,
   garrow_raw_sort_keys_add(arrow_options->sort_keys, sort_key);
 }
 
+enum {
+  PROP_REPLACE_SUBSTRING_OPTIONS_PATTERN = 1,
+  PROP_REPLACE_SUBSTRING_OPTIONS_REPLACEMENT,
+  PROP_REPLACE_SUBSTRING_OPTIONS_MAX_REPLACEMENTS,
+};
+
+G_DEFINE_TYPE(GArrowReplaceSubstringOptions,
+              garrow_replace_substring_options,
+              GARROW_TYPE_FUNCTION_OPTIONS)
+
+static void
+garrow_replace_substring_options_set_property(GObject *object,
+                                              guint prop_id,
+                                              const GValue *value,
+                                              GParamSpec *pspec)
+{
+  auto options =
+    
garrow_replace_substring_options_get_raw(GARROW_REPLACE_SUBSTRING_OPTIONS(object));
+
+  switch (prop_id) {
+  case PROP_REPLACE_SUBSTRING_OPTIONS_PATTERN:
+    options->pattern = g_value_get_string(value);
+    break;
+  case PROP_REPLACE_SUBSTRING_OPTIONS_REPLACEMENT:
+    options->replacement = g_value_get_string(value);
+    break;
+  case PROP_REPLACE_SUBSTRING_OPTIONS_MAX_REPLACEMENTS:
+    options->max_replacements = g_value_get_int64(value);
+    break;
+  default:
+    G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
+    break;
+  }
+}
+
+static void
+garrow_replace_substring_options_get_property(GObject *object,
+                                              guint prop_id,
+                                              GValue *value,
+                                              GParamSpec *pspec)
+{
+  auto options =
+    
garrow_replace_substring_options_get_raw(GARROW_REPLACE_SUBSTRING_OPTIONS(object));
+
+  switch (prop_id) {
+  case PROP_REPLACE_SUBSTRING_OPTIONS_PATTERN:
+    g_value_set_string(value, options->pattern.c_str());
+    break;
+  case PROP_REPLACE_SUBSTRING_OPTIONS_REPLACEMENT:
+    g_value_set_string(value, options->replacement.c_str());
+    break;
+  case PROP_REPLACE_SUBSTRING_OPTIONS_MAX_REPLACEMENTS:
+    g_value_set_int64(value, options->max_replacements);
+    break;
+  default:
+    G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
+    break;
+  }
+}
+
+static void
+garrow_replace_substring_options_init(GArrowReplaceSubstringOptions *object)
+{
+  auto arrow_priv = GARROW_FUNCTION_OPTIONS_GET_PRIVATE(object);
+  arrow_priv->options = static_cast<arrow::compute::FunctionOptions *>(
+    new arrow::compute::ReplaceSubstringOptions());
+}
+
+static void
+garrow_replace_substring_options_class_init(GArrowReplaceSubstringOptionsClass 
*klass)
+{
+  auto gobject_class = G_OBJECT_CLASS(klass);
+
+  gobject_class->set_property = garrow_replace_substring_options_set_property;
+  gobject_class->get_property = garrow_replace_substring_options_get_property;
+
+  arrow::compute::ReplaceSubstringOptions options;
+
+  GParamSpec *spec;
+  /**
+   * GArrowReplaceSubstringOptions:pattern:
+   *
+   * Pattern to match, literal, or regular expression depending on which 
kernel is used.
+   *
+   * Since: 23.0.0
+   */
+  spec = g_param_spec_string(
+    "pattern",
+    "Pattern",
+    "Pattern to match, literal, or regular expression depending on which 
kernel is used",
+    options.pattern.c_str(),
+    static_cast<GParamFlags>(G_PARAM_READWRITE));
+  g_object_class_install_property(gobject_class,
+                                  PROP_REPLACE_SUBSTRING_OPTIONS_PATTERN,
+                                  spec);
+
+  /**
+   * GArrowReplaceSubstringOptions:replacement:
+   *
+   * String to replace the pattern with.
+   *
+   * Since: 23.0.0
+   */
+  spec = g_param_spec_string("replacement",
+                             "Replacement",
+                             "String to replace the pattern with",
+                             options.replacement.c_str(),
+                             static_cast<GParamFlags>(G_PARAM_READWRITE));
+  g_object_class_install_property(gobject_class,
+                                  PROP_REPLACE_SUBSTRING_OPTIONS_REPLACEMENT,
+                                  spec);
+
+  /**
+   * GArrowReplaceSubstringOptions:max-replacements:
+   *
+   * Max number of substrings to replace (-1 means unbounded).
+   *
+   * Since: 23.0.0
+   */
+  spec = g_param_spec_int64("max-replacements",
+                            "Max Replacements",
+                            "Max number of substrings to replace (-1 means 
unbounded)",
+                            -1,
+                            G_MAXINT64,
+                            options.max_replacements,
+                            static_cast<GParamFlags>(G_PARAM_READWRITE));
+  g_object_class_install_property(gobject_class,
+                                  
PROP_REPLACE_SUBSTRING_OPTIONS_MAX_REPLACEMENTS,
+                                  spec);
+}
+
+/**
+ * garrow_replace_substring_options_new:
+ *
+ * Returns: A newly created #GArrowReplaceSubstringOptions.
+ *
+ * Since: 23.0.0
+ */
+GArrowReplaceSubstringOptions *
+garrow_replace_substring_options_new(void)
+{
+  return GARROW_REPLACE_SUBSTRING_OPTIONS(
+    g_object_new(GARROW_TYPE_REPLACE_SUBSTRING_OPTIONS, nullptr));
+}
+
 G_END_DECLS
 
 arrow::Result<arrow::FieldRef>
@@ -9138,6 +9286,12 @@ garrow_function_options_new_raw(const 
arrow::compute::FunctionOptions *arrow_opt
       static_cast<const arrow::compute::RankQuantileOptions *>(arrow_options);
     auto options = 
garrow_rank_quantile_options_new_raw(arrow_rank_quantile_options);
     return GARROW_FUNCTION_OPTIONS(options);
+  } else if (arrow_type_name == "ReplaceSubstringOptions") {
+    const auto arrow_replace_substring_options =
+      static_cast<const arrow::compute::ReplaceSubstringOptions 
*>(arrow_options);
+    auto options =
+      
garrow_replace_substring_options_new_raw(arrow_replace_substring_options);
+    return GARROW_FUNCTION_OPTIONS(options);
   } else {
     auto options = g_object_new(GARROW_TYPE_FUNCTION_OPTIONS, NULL);
     return GARROW_FUNCTION_OPTIONS(options);
@@ -10048,3 +10202,25 @@ 
garrow_rank_quantile_options_get_raw(GArrowRankQuantileOptions *options)
   return static_cast<arrow::compute::RankQuantileOptions *>(
     garrow_function_options_get_raw(GARROW_FUNCTION_OPTIONS(options)));
 }
+
+GArrowReplaceSubstringOptions *
+garrow_replace_substring_options_new_raw(
+  const arrow::compute::ReplaceSubstringOptions *arrow_options)
+{
+  return GARROW_REPLACE_SUBSTRING_OPTIONS(
+    g_object_new(GARROW_TYPE_REPLACE_SUBSTRING_OPTIONS,
+                 "pattern",
+                 arrow_options->pattern.c_str(),
+                 "replacement",
+                 arrow_options->replacement.c_str(),
+                 "max-replacements",
+                 arrow_options->max_replacements,
+                 nullptr));
+}
+
+arrow::compute::ReplaceSubstringOptions *
+garrow_replace_substring_options_get_raw(GArrowReplaceSubstringOptions 
*options)
+{
+  return static_cast<arrow::compute::ReplaceSubstringOptions *>(
+    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 92ec9e86fb..80bfa50661 100644
--- a/c_glib/arrow-glib/compute.h
+++ b/c_glib/arrow-glib/compute.h
@@ -1566,4 +1566,21 @@ void
 garrow_rank_quantile_options_add_sort_key(GArrowRankQuantileOptions *options,
                                           GArrowSortKey *sort_key);
 
+#define GARROW_TYPE_REPLACE_SUBSTRING_OPTIONS                                  
          \
+  (garrow_replace_substring_options_get_type())
+GARROW_AVAILABLE_IN_23_0
+G_DECLARE_DERIVABLE_TYPE(GArrowReplaceSubstringOptions,
+                         garrow_replace_substring_options,
+                         GARROW,
+                         REPLACE_SUBSTRING_OPTIONS,
+                         GArrowFunctionOptions)
+struct _GArrowReplaceSubstringOptionsClass
+{
+  GArrowFunctionOptionsClass parent_class;
+};
+
+GARROW_AVAILABLE_IN_23_0
+GArrowReplaceSubstringOptions *
+garrow_replace_substring_options_new(void);
+
 G_END_DECLS
diff --git a/c_glib/arrow-glib/compute.hpp b/c_glib/arrow-glib/compute.hpp
index 726a1d2e75..bdd27c0a35 100644
--- a/c_glib/arrow-glib/compute.hpp
+++ b/c_glib/arrow-glib/compute.hpp
@@ -280,3 +280,9 @@ garrow_rank_quantile_options_new_raw(
   const arrow::compute::RankQuantileOptions *arrow_options);
 arrow::compute::RankQuantileOptions *
 garrow_rank_quantile_options_get_raw(GArrowRankQuantileOptions *options);
+
+GArrowReplaceSubstringOptions *
+garrow_replace_substring_options_new_raw(
+  const arrow::compute::ReplaceSubstringOptions *arrow_options);
+arrow::compute::ReplaceSubstringOptions *
+garrow_replace_substring_options_get_raw(GArrowReplaceSubstringOptions 
*options);
diff --git a/c_glib/test/test-replace-substring-options.rb 
b/c_glib/test/test-replace-substring-options.rb
new file mode 100644
index 0000000000..14e011d835
--- /dev/null
+++ b/c_glib/test/test-replace-substring-options.rb
@@ -0,0 +1,67 @@
+# 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 TestReplaceSubstringOptions < Test::Unit::TestCase
+  include Helper::Buildable
+
+  def setup
+    @options = Arrow::ReplaceSubstringOptions.new
+  end
+
+  def test_pattern_property
+    assert_equal("", @options.pattern)
+    @options.pattern = "foo"
+    assert_equal("foo", @options.pattern)
+  end
+
+  def test_replacement_property
+    assert_equal("", @options.replacement)
+    @options.replacement = "bar"
+    assert_equal("bar", @options.replacement)
+  end
+
+  def test_max_replacements_property
+    assert_equal(-1, @options.max_replacements)
+    @options.max_replacements = 1
+    assert_equal(1, @options.max_replacements)
+  end
+
+  def test_replace_substring_function
+    args = [
+      Arrow::ArrayDatum.new(build_string_array(["foo", "this foo that foo", 
"bar"])),
+    ]
+    @options.pattern = "foo"
+    @options.replacement = "baz"
+    replace_substring_function = Arrow::Function.find("replace_substring")
+    result = replace_substring_function.execute(args, @options).value
+    expected = build_string_array(["baz", "this baz that baz", "bar"])
+    assert_equal(expected, result)
+  end
+
+  def test_replace_substring_with_max_replacements
+    args = [
+      Arrow::ArrayDatum.new(build_string_array(["this foo that foo"])),
+    ]
+    @options.pattern = "foo"
+    @options.replacement = "baz"
+    @options.max_replacements = 1
+    replace_substring_function = Arrow::Function.find("replace_substring")
+    result = replace_substring_function.execute(args, @options).value
+    expected = build_string_array(["this baz that foo"])
+    assert_equal(expected, result)
+  end
+end

Reply via email to