Skip to content

Commit d3fcd0a

Browse files
committed
Add GArrowRoundBinaryOptions
1 parent 4127ca2 commit d3fcd0a

File tree

4 files changed

+182
-0
lines changed

4 files changed

+182
-0
lines changed

c_glib/arrow-glib/compute.cpp

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,8 @@ G_BEGIN_DECLS
269269
* #GArrowExtractRegexOptions is a class to customize the `extract_regex`
270270
* function.
271271
*
272+
* #GArrowRoundBinaryOptions is a class to customize the `round_binary` function.
273+
*
272274
* There are many functions to compute data on an array.
273275
*/
274276

@@ -7091,6 +7093,99 @@ garrow_extract_regex_options_new(void)
70917093
return GARROW_EXTRACT_REGEX_OPTIONS(options);
70927094
}
70937095

7096+
enum {
7097+
PROP_ROUND_BINARY_OPTIONS_MODE = 1,
7098+
};
7099+
7100+
G_DEFINE_TYPE(GArrowRoundBinaryOptions,
7101+
garrow_round_binary_options,
7102+
GARROW_TYPE_FUNCTION_OPTIONS)
7103+
7104+
static void
7105+
garrow_round_binary_options_set_property(GObject *object,
7106+
guint prop_id,
7107+
const GValue *value,
7108+
GParamSpec *pspec)
7109+
{
7110+
auto options = garrow_round_binary_options_get_raw(GARROW_ROUND_BINARY_OPTIONS(object));
7111+
7112+
switch (prop_id) {
7113+
case PROP_ROUND_BINARY_OPTIONS_MODE:
7114+
options->round_mode = static_cast<arrow::compute::RoundMode>(g_value_get_enum(value));
7115+
break;
7116+
default:
7117+
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
7118+
break;
7119+
}
7120+
}
7121+
7122+
static void
7123+
garrow_round_binary_options_get_property(GObject *object,
7124+
guint prop_id,
7125+
GValue *value,
7126+
GParamSpec *pspec)
7127+
{
7128+
auto options = garrow_round_binary_options_get_raw(GARROW_ROUND_BINARY_OPTIONS(object));
7129+
7130+
switch (prop_id) {
7131+
case PROP_ROUND_BINARY_OPTIONS_MODE:
7132+
g_value_set_enum(value, static_cast<GArrowRoundMode>(options->round_mode));
7133+
break;
7134+
default:
7135+
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
7136+
break;
7137+
}
7138+
}
7139+
7140+
static void
7141+
garrow_round_binary_options_init(GArrowRoundBinaryOptions *object)
7142+
{
7143+
auto arrow_priv = GARROW_FUNCTION_OPTIONS_GET_PRIVATE(object);
7144+
arrow_priv->options = static_cast<arrow::compute::FunctionOptions *>(
7145+
new arrow::compute::RoundBinaryOptions());
7146+
}
7147+
7148+
static void
7149+
garrow_round_binary_options_class_init(GArrowRoundBinaryOptionsClass *klass)
7150+
{
7151+
auto gobject_class = G_OBJECT_CLASS(klass);
7152+
7153+
gobject_class->set_property = garrow_round_binary_options_set_property;
7154+
gobject_class->get_property = garrow_round_binary_options_get_property;
7155+
7156+
arrow::compute::RoundBinaryOptions options;
7157+
7158+
GParamSpec *spec;
7159+
/**
7160+
* GArrowRoundBinaryOptions:mode:
7161+
*
7162+
* The rounding and tie-breaking mode.
7163+
*
7164+
* Since: 23.0.0
7165+
*/
7166+
spec = g_param_spec_enum("mode",
7167+
"Mode",
7168+
"The rounding and tie-breaking mode",
7169+
GARROW_TYPE_ROUND_MODE,
7170+
static_cast<GArrowRoundMode>(options.round_mode),
7171+
static_cast<GParamFlags>(G_PARAM_READWRITE));
7172+
g_object_class_install_property(gobject_class, PROP_ROUND_BINARY_OPTIONS_MODE, spec);
7173+
}
7174+
7175+
/**
7176+
* garrow_round_binary_options_new:
7177+
*
7178+
* Returns: A newly created #GArrowRoundBinaryOptions.
7179+
*
7180+
* Since: 23.0.0
7181+
*/
7182+
GArrowRoundBinaryOptions *
7183+
garrow_round_binary_options_new(void)
7184+
{
7185+
return GARROW_ROUND_BINARY_OPTIONS(
7186+
g_object_new(GARROW_TYPE_ROUND_BINARY_OPTIONS, NULL));
7187+
}
7188+
70947189
G_END_DECLS
70957190

70967191
arrow::Result<arrow::FieldRef>
@@ -7254,6 +7349,11 @@ garrow_function_options_new_raw(const arrow::compute::FunctionOptions *arrow_opt
72547349
static_cast<const arrow::compute::ExtractRegexOptions *>(arrow_options);
72557350
auto options = garrow_extract_regex_options_new_raw(arrow_extract_regex_options);
72567351
return GARROW_FUNCTION_OPTIONS(options);
7352+
} else if (arrow_type_name == "RoundBinaryOptions") {
7353+
const auto arrow_round_binary_options =
7354+
static_cast<const arrow::compute::RoundBinaryOptions *>(arrow_options);
7355+
auto options = garrow_round_binary_options_new_raw(arrow_round_binary_options);
7356+
return GARROW_FUNCTION_OPTIONS(options);
72577357
} else {
72587358
auto options = g_object_new(GARROW_TYPE_FUNCTION_OPTIONS, NULL);
72597359
return GARROW_FUNCTION_OPTIONS(options);
@@ -7893,3 +7993,21 @@ garrow_extract_regex_options_get_raw(GArrowExtractRegexOptions *options)
78937993
return static_cast<arrow::compute::ExtractRegexOptions *>(
78947994
garrow_function_options_get_raw(GARROW_FUNCTION_OPTIONS(options)));
78957995
}
7996+
7997+
GArrowRoundBinaryOptions *
7998+
garrow_round_binary_options_new_raw(
7999+
const arrow::compute::RoundBinaryOptions *arrow_options)
8000+
{
8001+
return GARROW_ROUND_BINARY_OPTIONS(
8002+
g_object_new(GARROW_TYPE_ROUND_BINARY_OPTIONS,
8003+
"mode",
8004+
static_cast<GArrowRoundMode>(arrow_options->round_mode),
8005+
NULL));
8006+
}
8007+
8008+
arrow::compute::RoundBinaryOptions *
8009+
garrow_round_binary_options_get_raw(GArrowRoundBinaryOptions *options)
8010+
{
8011+
return static_cast<arrow::compute::RoundBinaryOptions *>(
8012+
garrow_function_options_get_raw(GARROW_FUNCTION_OPTIONS(options)));
8013+
}

c_glib/arrow-glib/compute.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1272,4 +1272,20 @@ GARROW_AVAILABLE_IN_23_0
12721272
GArrowExtractRegexOptions *
12731273
garrow_extract_regex_options_new(void);
12741274

1275+
#define GARROW_TYPE_ROUND_BINARY_OPTIONS (garrow_round_binary_options_get_type())
1276+
GARROW_AVAILABLE_IN_23_0
1277+
G_DECLARE_DERIVABLE_TYPE(GArrowRoundBinaryOptions,
1278+
garrow_round_binary_options,
1279+
GARROW,
1280+
ROUND_BINARY_OPTIONS,
1281+
GArrowFunctionOptions)
1282+
struct _GArrowRoundBinaryOptionsClass
1283+
{
1284+
GArrowFunctionOptionsClass parent_class;
1285+
};
1286+
1287+
GARROW_AVAILABLE_IN_23_0
1288+
GArrowRoundBinaryOptions *
1289+
garrow_round_binary_options_new(void);
1290+
12751291
G_END_DECLS

c_glib/arrow-glib/compute.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,3 +209,9 @@ garrow_extract_regex_options_new_raw(
209209
const arrow::compute::ExtractRegexOptions *arrow_options);
210210
arrow::compute::ExtractRegexOptions *
211211
garrow_extract_regex_options_get_raw(GArrowExtractRegexOptions *options);
212+
213+
GArrowRoundBinaryOptions *
214+
garrow_round_binary_options_new_raw(
215+
const arrow::compute::RoundBinaryOptions *arrow_options);
216+
arrow::compute::RoundBinaryOptions *
217+
garrow_round_binary_options_get_raw(GArrowRoundBinaryOptions *options);
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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 TestRoundBinaryOptions < Test::Unit::TestCase
19+
include Helper::Buildable
20+
21+
def setup
22+
@options = Arrow::RoundBinaryOptions.new
23+
end
24+
25+
def test_mode
26+
assert_equal(Arrow::RoundMode::HALF_TO_EVEN, @options.mode)
27+
@options.mode = :down
28+
assert_equal(Arrow::RoundMode::DOWN, @options.mode)
29+
end
30+
31+
def test_round_binary_function
32+
args = [
33+
Arrow::ArrayDatum.new(build_double_array([5.0])),
34+
Arrow::ArrayDatum.new(build_int32_array([-1])),
35+
]
36+
@options.mode = :half_towards_zero
37+
round_binary_function = Arrow::Function.find("round_binary")
38+
result = round_binary_function.execute(args, @options).value
39+
expected = build_double_array([0.0])
40+
assert_equal(expected, result)
41+
end
42+
end

0 commit comments

Comments
 (0)