Skip to content

Commit 627720b

Browse files
committed
ARROW-15216: [GLib] Add Arrow::RoundToMultipleOptions
Closes apache#12060 from kou/glib-round-to-multiple-options Authored-by: Sutou Kouhei <kou@clear-code.com> Signed-off-by: Sutou Kouhei <kou@clear-code.com>
1 parent edab145 commit 627720b

4 files changed

Lines changed: 249 additions & 0 deletions

File tree

c_glib/arrow-glib/compute.cpp

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <arrow-glib/error.hpp>
2929
#include <arrow-glib/reader.hpp>
3030
#include <arrow-glib/record-batch.hpp>
31+
#include <arrow-glib/scalar.hpp>
3132
#include <arrow-glib/schema.hpp>
3233
#include <arrow-glib/table.hpp>
3334

@@ -173,6 +174,11 @@ G_BEGIN_DECLS
173174
* #GArrowVarianceOptions is a class to customize the `stddev` function
174175
* and `variance` function.
175176
*
177+
* #GArrowRoundOptions is a class to customize the `round` function.
178+
*
179+
* #GArrowRoundToMultipleOptions is a class to customize the
180+
* `round_to_multiple` function.
181+
*
176182
* There are many functions to compute data on an array.
177183
*/
178184

@@ -3136,6 +3142,179 @@ garrow_round_options_new(void)
31363142
}
31373143

31383144

3145+
typedef struct GArrowRoundToMultipleOptionsPrivate_ {
3146+
GArrowScalar *multiple;
3147+
} GArrowRoundToMultipleOptionsPrivate;
3148+
3149+
enum {
3150+
PROP_ROUND_TO_MULTIPLE_OPTIONS_MULTIPLE = 1,
3151+
PROP_ROUND_TO_MULTIPLE_OPTIONS_MODE,
3152+
};
3153+
3154+
G_DEFINE_TYPE_WITH_PRIVATE(GArrowRoundToMultipleOptions,
3155+
garrow_round_to_multiple_options,
3156+
GARROW_TYPE_FUNCTION_OPTIONS)
3157+
3158+
#define GARROW_ROUND_TO_MULTIPLE_OPTIONS_GET_PRIVATE(object) \
3159+
static_cast<GArrowRoundToMultipleOptionsPrivate *>( \
3160+
garrow_round_to_multiple_options_get_instance_private( \
3161+
GARROW_ROUND_TO_MULTIPLE_OPTIONS(object)))
3162+
3163+
static void
3164+
garrow_round_to_multiple_options_dispose(GObject *object)
3165+
{
3166+
auto priv = GARROW_ROUND_TO_MULTIPLE_OPTIONS_GET_PRIVATE(object);
3167+
3168+
if (priv->multiple) {
3169+
g_object_unref(priv->multiple);
3170+
priv->multiple = NULL;
3171+
}
3172+
3173+
G_OBJECT_CLASS(garrow_round_to_multiple_options_parent_class)->dispose(object);
3174+
}
3175+
3176+
static void
3177+
garrow_round_to_multiple_options_set_property(GObject *object,
3178+
guint prop_id,
3179+
const GValue *value,
3180+
GParamSpec *pspec)
3181+
{
3182+
auto priv = GARROW_ROUND_TO_MULTIPLE_OPTIONS_GET_PRIVATE(object);
3183+
auto options =
3184+
garrow_round_to_multiple_options_get_raw(
3185+
GARROW_ROUND_TO_MULTIPLE_OPTIONS(object));
3186+
3187+
switch (prop_id) {
3188+
case PROP_ROUND_TO_MULTIPLE_OPTIONS_MULTIPLE:
3189+
{
3190+
auto multiple = g_value_get_object(value);
3191+
if (priv->multiple != multiple) {
3192+
if (priv->multiple) {
3193+
g_object_unref(priv->multiple);
3194+
}
3195+
if (multiple) {
3196+
priv->multiple = GARROW_SCALAR(multiple);
3197+
g_object_ref(priv->multiple);
3198+
options->multiple = garrow_scalar_get_raw(priv->multiple);
3199+
} else {
3200+
priv->multiple = NULL;
3201+
options->multiple = std::make_shared<arrow::NullScalar>();
3202+
}
3203+
}
3204+
}
3205+
break;
3206+
case PROP_ROUND_TO_MULTIPLE_OPTIONS_MODE:
3207+
options->round_mode =
3208+
static_cast<arrow::compute::RoundMode>(g_value_get_enum(value));
3209+
break;
3210+
default:
3211+
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
3212+
break;
3213+
}
3214+
}
3215+
3216+
static void
3217+
garrow_round_to_multiple_options_get_property(GObject *object,
3218+
guint prop_id,
3219+
GValue *value,
3220+
GParamSpec *pspec)
3221+
{
3222+
auto priv = GARROW_ROUND_TO_MULTIPLE_OPTIONS_GET_PRIVATE(object);
3223+
auto options =
3224+
garrow_round_to_multiple_options_get_raw(
3225+
GARROW_ROUND_TO_MULTIPLE_OPTIONS(object));
3226+
3227+
switch (prop_id) {
3228+
case PROP_ROUND_TO_MULTIPLE_OPTIONS_MULTIPLE:
3229+
g_value_set_object(value, priv->multiple);
3230+
break;
3231+
case PROP_ROUND_TO_MULTIPLE_OPTIONS_MODE:
3232+
g_value_set_enum(value, static_cast<GArrowRoundMode>(options->round_mode));
3233+
break;
3234+
default:
3235+
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
3236+
break;
3237+
}
3238+
}
3239+
3240+
static void
3241+
garrow_round_to_multiple_options_init(GArrowRoundToMultipleOptions *object)
3242+
{
3243+
auto function_options_priv = GARROW_FUNCTION_OPTIONS_GET_PRIVATE(object);
3244+
auto options = new arrow::compute::RoundToMultipleOptions();
3245+
function_options_priv->options =
3246+
static_cast<arrow::compute::FunctionOptions *>(options);
3247+
auto priv = GARROW_ROUND_TO_MULTIPLE_OPTIONS_GET_PRIVATE(object);
3248+
priv->multiple = garrow_scalar_new_raw(&(options->multiple));
3249+
}
3250+
3251+
static void
3252+
garrow_round_to_multiple_options_class_init(
3253+
GArrowRoundToMultipleOptionsClass *klass)
3254+
{
3255+
auto gobject_class = G_OBJECT_CLASS(klass);
3256+
gobject_class->dispose = garrow_round_to_multiple_options_dispose;
3257+
gobject_class->set_property = garrow_round_to_multiple_options_set_property;
3258+
gobject_class->get_property = garrow_round_to_multiple_options_get_property;
3259+
3260+
3261+
arrow::compute::RoundToMultipleOptions options;
3262+
3263+
GParamSpec *spec;
3264+
/**
3265+
* GArrowRoundToMultipleOptions:multiple:
3266+
*
3267+
* The rounding scale (multiple to round to).
3268+
*
3269+
* Should be a scalar of a type compatible with the argument to be rounded.
3270+
* For example, rounding a decimal value means a decimal multiple is
3271+
* required. Rounding a floating point or integer value means a floating
3272+
* point scalar is required.
3273+
*
3274+
* Since: 7.0.0
3275+
*/
3276+
spec = g_param_spec_object("multiple",
3277+
"Multiple to round to",
3278+
"The round scale",
3279+
GARROW_TYPE_SCALAR,
3280+
static_cast<GParamFlags>(G_PARAM_READWRITE));
3281+
g_object_class_install_property(gobject_class,
3282+
PROP_ROUND_TO_MULTIPLE_OPTIONS_MULTIPLE,
3283+
spec);
3284+
3285+
/**
3286+
* GArrowRoundToMultipleOptions:mode:
3287+
*
3288+
* The rounding and tie-breaking mode.
3289+
*
3290+
* Since: 7.0.0
3291+
*/
3292+
spec = g_param_spec_enum("mode",
3293+
"Mode",
3294+
"The rounding and tie-breaking mode",
3295+
GARROW_TYPE_ROUND_MODE,
3296+
static_cast<GArrowRoundMode>(options.round_mode),
3297+
static_cast<GParamFlags>(G_PARAM_READWRITE));
3298+
g_object_class_install_property(gobject_class,
3299+
PROP_ROUND_TO_MULTIPLE_OPTIONS_MODE,
3300+
spec);
3301+
}
3302+
3303+
/**
3304+
* garrow_round_to_multiple_options_new:
3305+
*
3306+
* Returns: A newly created #GArrowRoundToMultipleOptions.
3307+
*
3308+
* Since: 7.0.0
3309+
*/
3310+
GArrowRoundToMultipleOptions *
3311+
garrow_round_to_multiple_options_new(void)
3312+
{
3313+
return GARROW_ROUND_TO_MULTIPLE_OPTIONS(
3314+
g_object_new(GARROW_TYPE_ROUND_TO_MULTIPLE_OPTIONS, NULL));
3315+
}
3316+
3317+
31393318
/**
31403319
* garrow_array_cast:
31413320
* @array: A #GArrowArray.
@@ -4538,3 +4717,11 @@ garrow_round_options_get_raw(GArrowRoundOptions *options)
45384717
return static_cast<arrow::compute::RoundOptions *>(
45394718
garrow_function_options_get_raw(GARROW_FUNCTION_OPTIONS(options)));
45404719
}
4720+
4721+
4722+
arrow::compute::RoundToMultipleOptions *
4723+
garrow_round_to_multiple_options_get_raw(GArrowRoundToMultipleOptions *options)
4724+
{
4725+
return static_cast<arrow::compute::RoundToMultipleOptions *>(
4726+
garrow_function_options_get_raw(GARROW_FUNCTION_OPTIONS(options)));
4727+
}

c_glib/arrow-glib/compute.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,7 @@ typedef enum {
558558
GARROW_ROUND_HALF_TO_ODD,
559559
} GArrowRoundMode;
560560

561+
561562
#define GARROW_TYPE_ROUND_OPTIONS (garrow_round_options_get_type())
562563
G_DECLARE_DERIVABLE_TYPE(GArrowRoundOptions,
563564
garrow_round_options,
@@ -574,6 +575,23 @@ GArrowRoundOptions *
574575
garrow_round_options_new(void);
575576

576577

578+
#define GARROW_TYPE_ROUND_TO_MULTIPLE_OPTIONS \
579+
(garrow_round_to_multiple_options_get_type())
580+
G_DECLARE_DERIVABLE_TYPE(GArrowRoundToMultipleOptions,
581+
garrow_round_to_multiple_options,
582+
GARROW,
583+
ROUND_TO_MULTIPLE_OPTIONS,
584+
GArrowFunctionOptions)
585+
struct _GArrowRoundToMultipleOptionsClass
586+
{
587+
GArrowFunctionOptionsClass parent_class;
588+
};
589+
590+
GARROW_AVAILABLE_IN_7_0
591+
GArrowRoundToMultipleOptions *
592+
garrow_round_to_multiple_options_new(void);
593+
594+
577595
GArrowArray *garrow_array_cast(GArrowArray *array,
578596
GArrowDataType *target_data_type,
579597
GArrowCastOptions *options,

c_glib/arrow-glib/compute.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,7 @@ garrow_variance_options_get_raw(GArrowVarianceOptions *options);
112112

113113
arrow::compute::RoundOptions *
114114
garrow_round_options_get_raw(GArrowRoundOptions *options);
115+
116+
117+
arrow::compute::RoundToMultipleOptions *
118+
garrow_round_to_multiple_options_get_raw(GArrowRoundToMultipleOptions *options);
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
class TestRoundToMultipleOptions < Test::Unit::TestCase
19+
include Helper::Buildable
20+
21+
def setup
22+
@options = Arrow::RoundToMultipleOptions.new
23+
end
24+
25+
def test_multiple
26+
assert_equal(Arrow::DoubleScalar.new(1.0),
27+
@options.multiple)
28+
data_type = Arrow::Decimal256DataType.new(8, 2)
29+
value = Arrow::Decimal256.new("23423445")
30+
multiple = Arrow::Decimal256Scalar.new(data_type, value)
31+
@options.multiple = multiple
32+
assert_equal(multiple, @options.multiple)
33+
end
34+
35+
def test_mode
36+
assert_equal(Arrow::RoundMode::HALF_TO_EVEN, @options.mode)
37+
@options.mode = :down
38+
assert_equal(Arrow::RoundMode::DOWN, @options.mode)
39+
end
40+
end

0 commit comments

Comments
 (0)