diff --git a/c_glib/arrow-glib/compute.cpp b/c_glib/arrow-glib/compute.cpp index 78fe9e20915..3527724d40a 100644 --- a/c_glib/arrow-glib/compute.cpp +++ b/c_glib/arrow-glib/compute.cpp @@ -296,8 +296,12 @@ G_BEGIN_DECLS * #GArrowPairwiseOptions is a class to customize the pairwise * functions such as `pairwise_diff` and `pairwise_diff_checked`. * - * #GArrowReplaceSliceOptions is a class to customize the `utf8_replace_slice` and - * `binary_replace_slice` functions. + * #GArrowReplaceSliceOptions is a class to customize the + * `utf8_replace_slice` and `binary_replace_slice` functions. + * + * + * #GArrowPartitionNthOptions is a class to customize the + * `partition_nth_indices` function. * * There are many functions to compute data on an array. */ @@ -8494,6 +8498,129 @@ garrow_replace_slice_options_new(void) g_object_new(GARROW_TYPE_REPLACE_SLICE_OPTIONS, nullptr)); } +enum { + PROP_PARTITION_NTH_OPTIONS_PIVOT = 1, + PROP_PARTITION_NTH_OPTIONS_NULL_PLACEMENT, +}; + +G_DEFINE_TYPE(GArrowPartitionNthOptions, + garrow_partition_nth_options, + GARROW_TYPE_FUNCTION_OPTIONS) + +static void +garrow_partition_nth_options_set_property(GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec) +{ + auto options = + garrow_partition_nth_options_get_raw(GARROW_PARTITION_NTH_OPTIONS(object)); + + switch (prop_id) { + case PROP_PARTITION_NTH_OPTIONS_PIVOT: + options->pivot = g_value_get_int64(value); + break; + case PROP_PARTITION_NTH_OPTIONS_NULL_PLACEMENT: + options->null_placement = + static_cast(g_value_get_enum(value)); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec); + break; + } +} + +static void +garrow_partition_nth_options_get_property(GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec) +{ + auto options = + garrow_partition_nth_options_get_raw(GARROW_PARTITION_NTH_OPTIONS(object)); + + switch (prop_id) { + case PROP_PARTITION_NTH_OPTIONS_PIVOT: + g_value_set_int64(value, options->pivot); + break; + case PROP_PARTITION_NTH_OPTIONS_NULL_PLACEMENT: + g_value_set_enum(value, static_cast(options->null_placement)); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec); + break; + } +} + +static void +garrow_partition_nth_options_init(GArrowPartitionNthOptions *object) +{ + auto priv = GARROW_FUNCTION_OPTIONS_GET_PRIVATE(object); + priv->options = static_cast( + new arrow::compute::PartitionNthOptions()); +} + +static void +garrow_partition_nth_options_class_init(GArrowPartitionNthOptionsClass *klass) +{ + auto gobject_class = G_OBJECT_CLASS(klass); + + gobject_class->set_property = garrow_partition_nth_options_set_property; + gobject_class->get_property = garrow_partition_nth_options_get_property; + + arrow::compute::PartitionNthOptions options; + + GParamSpec *spec; + /** + * GArrowPartitionNthOptions:pivot: + * + * The index into the equivalent sorted array of the partition pivot element. + * + * Since: 23.0.0 + */ + spec = g_param_spec_int64( + "pivot", + "Pivot", + "The index into the equivalent sorted array of the partition pivot element", + 0, + G_MAXINT64, + options.pivot, + static_cast(G_PARAM_READWRITE)); + g_object_class_install_property(gobject_class, PROP_PARTITION_NTH_OPTIONS_PIVOT, spec); + + /** + * GArrowPartitionNthOptions:null-placement: + * + * Whether nulls and NaNs are partitioned at the start or at the end. + * + * Since: 23.0.0 + */ + spec = + g_param_spec_enum("null-placement", + "Null placement", + "Whether nulls and NaNs are partitioned at the start or at the end", + GARROW_TYPE_NULL_PLACEMENT, + static_cast(options.null_placement), + static_cast(G_PARAM_READWRITE)); + g_object_class_install_property(gobject_class, + PROP_PARTITION_NTH_OPTIONS_NULL_PLACEMENT, + spec); +} + +/** + * garrow_partition_nth_options_new: + * + * Returns: A newly created #GArrowPartitionNthOptions. + * + * Since: 23.0.0 + */ +GArrowPartitionNthOptions * +garrow_partition_nth_options_new(void) +{ + return GARROW_PARTITION_NTH_OPTIONS( + g_object_new(GARROW_TYPE_PARTITION_NTH_OPTIONS, nullptr)); +} + G_END_DECLS arrow::Result @@ -8703,6 +8830,11 @@ garrow_function_options_new_raw(const arrow::compute::FunctionOptions *arrow_opt static_cast(arrow_options); auto options = garrow_replace_slice_options_new_raw(arrow_replace_slice_options); return GARROW_FUNCTION_OPTIONS(options); + } else if (arrow_type_name == "PartitionNthOptions") { + const auto arrow_partition_nth_options = + static_cast(arrow_options); + auto options = garrow_partition_nth_options_new_raw(arrow_partition_nth_options); + return GARROW_FUNCTION_OPTIONS(options); } else { auto options = g_object_new(GARROW_TYPE_FUNCTION_OPTIONS, NULL); return GARROW_FUNCTION_OPTIONS(options); @@ -9548,3 +9680,23 @@ garrow_replace_slice_options_get_raw(GArrowReplaceSliceOptions *options) return static_cast( garrow_function_options_get_raw(GARROW_FUNCTION_OPTIONS(options))); } + +GArrowPartitionNthOptions * +garrow_partition_nth_options_new_raw( + const arrow::compute::PartitionNthOptions *arrow_options) +{ + return GARROW_PARTITION_NTH_OPTIONS( + g_object_new(GARROW_TYPE_PARTITION_NTH_OPTIONS, + "pivot", + arrow_options->pivot, + "null-placement", + static_cast(arrow_options->null_placement), + nullptr)); +} + +arrow::compute::PartitionNthOptions * +garrow_partition_nth_options_get_raw(GArrowPartitionNthOptions *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 e5315a07caa..c79c704b40b 100644 --- a/c_glib/arrow-glib/compute.h +++ b/c_glib/arrow-glib/compute.h @@ -1491,4 +1491,20 @@ GARROW_AVAILABLE_IN_23_0 GArrowReplaceSliceOptions * garrow_replace_slice_options_new(void); +#define GARROW_TYPE_PARTITION_NTH_OPTIONS (garrow_partition_nth_options_get_type()) +GARROW_AVAILABLE_IN_23_0 +G_DECLARE_DERIVABLE_TYPE(GArrowPartitionNthOptions, + garrow_partition_nth_options, + GARROW, + PARTITION_NTH_OPTIONS, + GArrowFunctionOptions) +struct _GArrowPartitionNthOptionsClass +{ + GArrowFunctionOptionsClass parent_class; +}; + +GARROW_AVAILABLE_IN_23_0 +GArrowPartitionNthOptions * +garrow_partition_nth_options_new(void); + G_END_DECLS diff --git a/c_glib/arrow-glib/compute.hpp b/c_glib/arrow-glib/compute.hpp index bb07bc764ca..692ec45b4e8 100644 --- a/c_glib/arrow-glib/compute.hpp +++ b/c_glib/arrow-glib/compute.hpp @@ -262,3 +262,9 @@ garrow_replace_slice_options_new_raw( const arrow::compute::ReplaceSliceOptions *arrow_options); arrow::compute::ReplaceSliceOptions * garrow_replace_slice_options_get_raw(GArrowReplaceSliceOptions *options); + +GArrowPartitionNthOptions * +garrow_partition_nth_options_new_raw( + const arrow::compute::PartitionNthOptions *arrow_options); +arrow::compute::PartitionNthOptions * +garrow_partition_nth_options_get_raw(GArrowPartitionNthOptions *options); diff --git a/c_glib/test/test-partition-nth-options.rb b/c_glib/test/test-partition-nth-options.rb new file mode 100644 index 00000000000..aaf46db6356 --- /dev/null +++ b/c_glib/test/test-partition-nth-options.rb @@ -0,0 +1,51 @@ +# 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 TestPartitionNthOptions < Test::Unit::TestCase + include Helper::Buildable + + def setup + @options = Arrow::PartitionNthOptions.new + end + + def test_pivot_property + assert_equal(0, @options.pivot) + @options.pivot = 2 + assert_equal(2, @options.pivot) + end + + def test_null_placement_property + assert_equal(Arrow::NullPlacement::AT_END, @options.null_placement) + @options.null_placement = :at_start + assert_equal(Arrow::NullPlacement::AT_START, @options.null_placement) + @options.null_placement = :at_end + assert_equal(Arrow::NullPlacement::AT_END, @options.null_placement) + end + + def test_partition_nth_indices_function + args = [ + Arrow::ArrayDatum.new(build_int32_array([5, 1, 4, 2, nil, 3])), + ] + @options.pivot = 2 + partition_nth_indices_function = Arrow::Function.find("partition_nth_indices") + result = partition_nth_indices_function.execute(args, @options).value + assert_equal(5, result.get_value(2)) + @options.null_placement = :at_start + result = partition_nth_indices_function.execute(args, @options).value + assert_equal(5, result.get_value(3)) + end +end