From 90bd0d0914016b01b536ac0def63062617482bd6 Mon Sep 17 00:00:00 2001 From: Sten Larsson Date: Thu, 4 Dec 2025 16:21:57 +0100 Subject: [PATCH] Add GArrowPairwiseOptions --- c_glib/arrow-glib/compute.cpp | 116 +++++++++++++++++++++++++++ c_glib/arrow-glib/compute.h | 16 ++++ c_glib/arrow-glib/compute.hpp | 5 ++ c_glib/test/test-pairwise-options.rb | 42 ++++++++++ 4 files changed, 179 insertions(+) create mode 100644 c_glib/test/test-pairwise-options.rb diff --git a/c_glib/arrow-glib/compute.cpp b/c_glib/arrow-glib/compute.cpp index 8d16b9abee0..df6fab4f305 100644 --- a/c_glib/arrow-glib/compute.cpp +++ b/c_glib/arrow-glib/compute.cpp @@ -293,6 +293,9 @@ G_BEGIN_DECLS * `utf8_lpad`, `utf8_rpad`, `utf8_center`, `ascii_lpad`, `ascii_rpad`, and * `ascii_center`. * + * #GArrowPairwiseOptions is a class to customize the pairwise + * functions such as `pairwise_diff` and `pairwise_diff_checked`. + * * There are many functions to compute data on an array. */ @@ -8252,6 +8255,100 @@ garrow_pad_options_new(void) return GARROW_PAD_OPTIONS(g_object_new(GARROW_TYPE_PAD_OPTIONS, NULL)); } +enum { + PROP_PAIRWISE_OPTIONS_PERIODS = 1, +}; + +G_DEFINE_TYPE(GArrowPairwiseOptions, + garrow_pairwise_options, + GARROW_TYPE_FUNCTION_OPTIONS) + +static void +garrow_pairwise_options_set_property(GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec) +{ + auto options = garrow_pairwise_options_get_raw(GARROW_PAIRWISE_OPTIONS(object)); + + switch (prop_id) { + case PROP_PAIRWISE_OPTIONS_PERIODS: + options->periods = g_value_get_int64(value); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec); + break; + } +} + +static void +garrow_pairwise_options_get_property(GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec) +{ + auto options = garrow_pairwise_options_get_raw(GARROW_PAIRWISE_OPTIONS(object)); + + switch (prop_id) { + case PROP_PAIRWISE_OPTIONS_PERIODS: + g_value_set_int64(value, options->periods); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec); + break; + } +} + +static void +garrow_pairwise_options_init(GArrowPairwiseOptions *object) +{ + auto priv = GARROW_FUNCTION_OPTIONS_GET_PRIVATE(object); + priv->options = + static_cast(new arrow::compute::PairwiseOptions()); +} + +static void +garrow_pairwise_options_class_init(GArrowPairwiseOptionsClass *klass) +{ + auto gobject_class = G_OBJECT_CLASS(klass); + + gobject_class->set_property = garrow_pairwise_options_set_property; + gobject_class->get_property = garrow_pairwise_options_get_property; + + arrow::compute::PairwiseOptions options; + + GParamSpec *spec; + /** + * GArrowPairwiseOptions:periods: + * + * Periods to shift for applying the binary operation, accepts negative values. + * + * Since: 23.0.0 + */ + spec = g_param_spec_int64( + "periods", + "Periods", + "Periods to shift for applying the binary operation, accepts negative values", + G_MININT64, + G_MAXINT64, + options.periods, + static_cast(G_PARAM_READWRITE)); + g_object_class_install_property(gobject_class, PROP_PAIRWISE_OPTIONS_PERIODS, spec); +} + +/** + * garrow_pairwise_options_new: + * + * Returns: A newly created #GArrowPairwiseOptions. + * + * Since: 23.0.0 + */ +GArrowPairwiseOptions * +garrow_pairwise_options_new(void) +{ + return GARROW_PAIRWISE_OPTIONS(g_object_new(GARROW_TYPE_PAIRWISE_OPTIONS, NULL)); +} + G_END_DECLS arrow::Result @@ -8451,6 +8548,11 @@ garrow_function_options_new_raw(const arrow::compute::FunctionOptions *arrow_opt static_cast(arrow_options); auto options = garrow_pad_options_new_raw(arrow_pad_options); return GARROW_FUNCTION_OPTIONS(options); + } else if (arrow_type_name == "PairwiseOptions") { + const auto arrow_pairwise_options = + static_cast(arrow_options); + auto options = garrow_pairwise_options_new_raw(arrow_pairwise_options); + return GARROW_FUNCTION_OPTIONS(options); } else { auto options = g_object_new(GARROW_TYPE_FUNCTION_OPTIONS, NULL); return GARROW_FUNCTION_OPTIONS(options); @@ -9261,3 +9363,17 @@ garrow_pad_options_get_raw(GArrowPadOptions *options) return static_cast( garrow_function_options_get_raw(GARROW_FUNCTION_OPTIONS(options))); } + +GArrowPairwiseOptions * +garrow_pairwise_options_new_raw(const arrow::compute::PairwiseOptions *arrow_options) +{ + return GARROW_PAIRWISE_OPTIONS( + g_object_new(GARROW_TYPE_PAIRWISE_OPTIONS, "periods", arrow_options->periods, NULL)); +} + +arrow::compute::PairwiseOptions * +garrow_pairwise_options_get_raw(GArrowPairwiseOptions *options) +{ + return static_cast( + 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 14e7e4a7b91..becdaf3a672 100644 --- a/c_glib/arrow-glib/compute.h +++ b/c_glib/arrow-glib/compute.h @@ -1459,4 +1459,20 @@ GARROW_AVAILABLE_IN_23_0 GArrowPadOptions * garrow_pad_options_new(void); +#define GARROW_TYPE_PAIRWISE_OPTIONS (garrow_pairwise_options_get_type()) +GARROW_AVAILABLE_IN_23_0 +G_DECLARE_DERIVABLE_TYPE(GArrowPairwiseOptions, + garrow_pairwise_options, + GARROW, + PAIRWISE_OPTIONS, + GArrowFunctionOptions) +struct _GArrowPairwiseOptionsClass +{ + GArrowFunctionOptionsClass parent_class; +}; + +GARROW_AVAILABLE_IN_23_0 +GArrowPairwiseOptions * +garrow_pairwise_options_new(void); + G_END_DECLS diff --git a/c_glib/arrow-glib/compute.hpp b/c_glib/arrow-glib/compute.hpp index 6f9ae390377..1a9da1c00c5 100644 --- a/c_glib/arrow-glib/compute.hpp +++ b/c_glib/arrow-glib/compute.hpp @@ -251,3 +251,8 @@ GArrowPadOptions * garrow_pad_options_new_raw(const arrow::compute::PadOptions *arrow_options); arrow::compute::PadOptions * garrow_pad_options_get_raw(GArrowPadOptions *options); + +GArrowPairwiseOptions * +garrow_pairwise_options_new_raw(const arrow::compute::PairwiseOptions *arrow_options); +arrow::compute::PairwiseOptions * +garrow_pairwise_options_get_raw(GArrowPairwiseOptions *options); diff --git a/c_glib/test/test-pairwise-options.rb b/c_glib/test/test-pairwise-options.rb new file mode 100644 index 00000000000..98cbdf7cb89 --- /dev/null +++ b/c_glib/test/test-pairwise-options.rb @@ -0,0 +1,42 @@ +# 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 TestPairwiseOptions < Test::Unit::TestCase + include Helper::Buildable + + def setup + @options = Arrow::PairwiseOptions.new + end + + def test_periods_property + assert_equal(1, @options.periods) + @options.periods = 2 + assert_equal(2, @options.periods) + @options.periods = -1 + assert_equal(-1, @options.periods) + end + + def test_pairwise_diff_function + args = [ + Arrow::ArrayDatum.new(build_int32_array([1, 2, 4, 7, 11])), + ] + pairwise_diff_function = Arrow::Function.find("pairwise_diff") + @options.periods = 2 + result = pairwise_diff_function.execute(args, @options).value + assert_equal(build_int32_array([nil, nil, 3, 5, 7]), result) + end +end