Skip to content

Commit 3cf59d8

Browse files
authored
GH-48365: [GLib][Ruby] Add DayOfWeekOptions (#48372)
### Rationale for this change The `DayOfWeekOptions` class is not available in GLib/Ruby, and it is used together with the `day_of_week` compute function. ### What changes are included in this PR? This adds the `DayOfWeekOptions` class to GLib. ### Are these changes tested? Yes, with Ruby unit tests. ### Are there any user-facing changes? Yes, a new class. * GitHub Issue: #48365 Authored-by: Sten Larsson <sten@burtcorp.com> Signed-off-by: Sutou Kouhei <kou@clear-code.com>
1 parent d5171c6 commit 3cf59d8

File tree

4 files changed

+231
-0
lines changed

4 files changed

+231
-0
lines changed

c_glib/arrow-glib/compute.cpp

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,8 @@ G_BEGIN_DECLS
264264
* #GArrowElementWiseAggregateOptions is a class to customize element-wise
265265
* aggregate functions such as `min_element_wise` and `max_element_wise`.
266266
*
267+
* #GArrowDayOfWeekOptions is a class to customize the `day_of_week` function.
268+
*
267269
* There are many functions to compute data on an array.
268270
*/
269271

@@ -6869,6 +6871,127 @@ garrow_element_wise_aggregate_options_new(void)
68696871
return GARROW_ELEMENT_WISE_AGGREGATE_OPTIONS(options);
68706872
}
68716873

6874+
enum {
6875+
PROP_DAY_OF_WEEK_OPTIONS_COUNT_FROM_ZERO = 1,
6876+
PROP_DAY_OF_WEEK_OPTIONS_WEEK_START,
6877+
};
6878+
6879+
G_DEFINE_TYPE(GArrowDayOfWeekOptions,
6880+
garrow_day_of_week_options,
6881+
GARROW_TYPE_FUNCTION_OPTIONS)
6882+
6883+
static void
6884+
garrow_day_of_week_options_set_property(GObject *object,
6885+
guint prop_id,
6886+
const GValue *value,
6887+
GParamSpec *pspec)
6888+
{
6889+
auto options = garrow_day_of_week_options_get_raw(GARROW_DAY_OF_WEEK_OPTIONS(object));
6890+
6891+
switch (prop_id) {
6892+
case PROP_DAY_OF_WEEK_OPTIONS_COUNT_FROM_ZERO:
6893+
options->count_from_zero = g_value_get_boolean(value);
6894+
break;
6895+
case PROP_DAY_OF_WEEK_OPTIONS_WEEK_START:
6896+
options->week_start = g_value_get_uint(value);
6897+
break;
6898+
default:
6899+
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
6900+
break;
6901+
}
6902+
}
6903+
6904+
static void
6905+
garrow_day_of_week_options_get_property(GObject *object,
6906+
guint prop_id,
6907+
GValue *value,
6908+
GParamSpec *pspec)
6909+
{
6910+
auto options = garrow_day_of_week_options_get_raw(GARROW_DAY_OF_WEEK_OPTIONS(object));
6911+
6912+
switch (prop_id) {
6913+
case PROP_DAY_OF_WEEK_OPTIONS_COUNT_FROM_ZERO:
6914+
g_value_set_boolean(value, options->count_from_zero);
6915+
break;
6916+
case PROP_DAY_OF_WEEK_OPTIONS_WEEK_START:
6917+
g_value_set_uint(value, options->week_start);
6918+
break;
6919+
default:
6920+
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
6921+
break;
6922+
}
6923+
}
6924+
6925+
static void
6926+
garrow_day_of_week_options_init(GArrowDayOfWeekOptions *object)
6927+
{
6928+
auto priv = GARROW_FUNCTION_OPTIONS_GET_PRIVATE(object);
6929+
priv->options = static_cast<arrow::compute::FunctionOptions *>(
6930+
new arrow::compute::DayOfWeekOptions());
6931+
}
6932+
6933+
static void
6934+
garrow_day_of_week_options_class_init(GArrowDayOfWeekOptionsClass *klass)
6935+
{
6936+
auto gobject_class = G_OBJECT_CLASS(klass);
6937+
6938+
gobject_class->set_property = garrow_day_of_week_options_set_property;
6939+
gobject_class->get_property = garrow_day_of_week_options_get_property;
6940+
6941+
arrow::compute::DayOfWeekOptions options;
6942+
6943+
GParamSpec *spec;
6944+
/**
6945+
* GArrowDayOfWeekOptions:count-from-zero:
6946+
*
6947+
* Number days from 0 if true and from 1 if false.
6948+
*
6949+
* Since: 23.0.0
6950+
*/
6951+
spec = g_param_spec_boolean("count-from-zero",
6952+
"Count from zero",
6953+
"Number days from 0 if true and from 1 if false",
6954+
options.count_from_zero,
6955+
static_cast<GParamFlags>(G_PARAM_READWRITE));
6956+
g_object_class_install_property(gobject_class,
6957+
PROP_DAY_OF_WEEK_OPTIONS_COUNT_FROM_ZERO,
6958+
spec);
6959+
6960+
/**
6961+
* GArrowDayOfWeekOptions:week-start:
6962+
*
6963+
* What day does the week start with (Monday=1, Sunday=7).
6964+
* The numbering is unaffected by the count_from_zero parameter.
6965+
*
6966+
* Since: 23.0.0
6967+
*/
6968+
spec = g_param_spec_uint("week-start",
6969+
"Week start",
6970+
"What day does the week start with (Monday=1, Sunday=7). The "
6971+
"numbering is unaffected by the count_from_zero parameter",
6972+
1,
6973+
7,
6974+
options.week_start,
6975+
static_cast<GParamFlags>(G_PARAM_READWRITE));
6976+
g_object_class_install_property(gobject_class,
6977+
PROP_DAY_OF_WEEK_OPTIONS_WEEK_START,
6978+
spec);
6979+
}
6980+
6981+
/**
6982+
* garrow_day_of_week_options_new:
6983+
*
6984+
* Returns: A newly created #GArrowDayOfWeekOptions.
6985+
*
6986+
* Since: 23.0.0
6987+
*/
6988+
GArrowDayOfWeekOptions *
6989+
garrow_day_of_week_options_new(void)
6990+
{
6991+
auto options = g_object_new(GARROW_TYPE_DAY_OF_WEEK_OPTIONS, NULL);
6992+
return GARROW_DAY_OF_WEEK_OPTIONS(options);
6993+
}
6994+
68726995
G_END_DECLS
68736996

68746997
arrow::Result<arrow::FieldRef>
@@ -7022,6 +7145,11 @@ garrow_function_options_new_raw(const arrow::compute::FunctionOptions *arrow_opt
70227145
auto options =
70237146
garrow_element_wise_aggregate_options_new_raw(arrow_element_wise_aggregate_options);
70247147
return GARROW_FUNCTION_OPTIONS(options);
7148+
} else if (arrow_type_name == "DayOfWeekOptions") {
7149+
const auto arrow_day_of_week_options =
7150+
static_cast<const arrow::compute::DayOfWeekOptions *>(arrow_options);
7151+
auto options = garrow_day_of_week_options_new_raw(arrow_day_of_week_options);
7152+
return GARROW_FUNCTION_OPTIONS(options);
70257153
} else {
70267154
auto options = g_object_new(GARROW_TYPE_FUNCTION_OPTIONS, NULL);
70277155
return GARROW_FUNCTION_OPTIONS(options);
@@ -7626,3 +7754,21 @@ garrow_element_wise_aggregate_options_get_raw(GArrowElementWiseAggregateOptions
76267754
return static_cast<arrow::compute::ElementWiseAggregateOptions *>(
76277755
garrow_function_options_get_raw(GARROW_FUNCTION_OPTIONS(options)));
76287756
}
7757+
7758+
GArrowDayOfWeekOptions *
7759+
garrow_day_of_week_options_new_raw(const arrow::compute::DayOfWeekOptions *arrow_options)
7760+
{
7761+
return GARROW_DAY_OF_WEEK_OPTIONS(g_object_new(GARROW_TYPE_DAY_OF_WEEK_OPTIONS,
7762+
"count-from-zero",
7763+
arrow_options->count_from_zero,
7764+
"week-start",
7765+
arrow_options->week_start,
7766+
NULL));
7767+
}
7768+
7769+
arrow::compute::DayOfWeekOptions *
7770+
garrow_day_of_week_options_get_raw(GArrowDayOfWeekOptions *options)
7771+
{
7772+
return static_cast<arrow::compute::DayOfWeekOptions *>(
7773+
garrow_function_options_get_raw(GARROW_FUNCTION_OPTIONS(options)));
7774+
}

c_glib/arrow-glib/compute.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,4 +1240,20 @@ GARROW_AVAILABLE_IN_23_0
12401240
GArrowElementWiseAggregateOptions *
12411241
garrow_element_wise_aggregate_options_new(void);
12421242

1243+
#define GARROW_TYPE_DAY_OF_WEEK_OPTIONS (garrow_day_of_week_options_get_type())
1244+
GARROW_AVAILABLE_IN_23_0
1245+
G_DECLARE_DERIVABLE_TYPE(GArrowDayOfWeekOptions,
1246+
garrow_day_of_week_options,
1247+
GARROW,
1248+
DAY_OF_WEEK_OPTIONS,
1249+
GArrowFunctionOptions)
1250+
struct _GArrowDayOfWeekOptionsClass
1251+
{
1252+
GArrowFunctionOptionsClass parent_class;
1253+
};
1254+
1255+
GARROW_AVAILABLE_IN_23_0
1256+
GArrowDayOfWeekOptions *
1257+
garrow_day_of_week_options_new(void);
1258+
12431259
G_END_DECLS

c_glib/arrow-glib/compute.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,3 +198,8 @@ garrow_element_wise_aggregate_options_new_raw(
198198
const arrow::compute::ElementWiseAggregateOptions *arrow_options);
199199
arrow::compute::ElementWiseAggregateOptions *
200200
garrow_element_wise_aggregate_options_get_raw(GArrowElementWiseAggregateOptions *options);
201+
202+
GArrowDayOfWeekOptions *
203+
garrow_day_of_week_options_new_raw(const arrow::compute::DayOfWeekOptions *arrow_options);
204+
arrow::compute::DayOfWeekOptions *
205+
garrow_day_of_week_options_get_raw(GArrowDayOfWeekOptions *options);
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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 TestDayOfWeekOptions < Test::Unit::TestCase
19+
include Helper::Buildable
20+
21+
def setup
22+
@options = Arrow::DayOfWeekOptions.new
23+
end
24+
25+
def test_count_from_zero_property
26+
assert do
27+
@options.count_from_zero?
28+
end
29+
@options.count_from_zero = false
30+
assert do
31+
!@options.count_from_zero?
32+
end
33+
end
34+
35+
def test_week_start_property
36+
assert_equal(1, @options.week_start)
37+
@options.week_start = 7
38+
assert_equal(7, @options.week_start)
39+
end
40+
41+
def test_day_of_week_function_with_count_from_zero_false
42+
omit("Missing tzdata on Windows") if Gem.win_platform?
43+
args = [
44+
# 2017-09-09T10:33:10Z (Saturday)
45+
Arrow::ArrayDatum.new(build_timestamp_array(:milli, [1504953190000])),
46+
]
47+
@options.count_from_zero = false
48+
day_of_week_function = Arrow::Function.find("day_of_week")
49+
assert_equal(build_int64_array([6]),
50+
day_of_week_function.execute(args, @options).value)
51+
end
52+
53+
def test_day_of_week_function_with_week_start
54+
omit("Missing tzdata on Windows") if Gem.win_platform?
55+
args = [
56+
# 2017-09-09T10:33:10Z (Saturday)
57+
Arrow::ArrayDatum.new(build_timestamp_array(:milli, [1504953190000])),
58+
]
59+
@options.week_start = 2
60+
day_of_week_function = Arrow::Function.find("day_of_week")
61+
assert_equal(build_int64_array([4]),
62+
day_of_week_function.execute(args, @options).value)
63+
end
64+
end

0 commit comments

Comments
 (0)