Skip to content

Commit 9dff3ef

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

File tree

4 files changed

+259
-1
lines changed

4 files changed

+259
-1
lines changed

c_glib/arrow-glib/compute.cpp

Lines changed: 171 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,8 @@ G_BEGIN_DECLS
332332
* #GArrowTrimOptions is a class to customize the `utf8_trim`, `utf8_ltrim`,
333333
* `utf8_rtrim`, `ascii_trim`, `ascii_ltrim`, and `ascii_rtrim` functions.
334334
*
335+
* #GArrowWeekOptions is a class to customize the `week` function.
336+
*
335337
* There are many functions to compute data on an array.
336338
*/
337339

@@ -9310,7 +9312,7 @@ garrow_round_temporal_options_class_init(GArrowRoundTemporalOptionsClass *klass)
93109312
*/
93119313
spec =
93129314
g_param_spec_boolean("week-starts-monday",
9313-
"Week Starts Monday",
9315+
"Week starts Monday",
93149316
"What day does the week start with (Monday=true, Sunday=false)",
93159317
options.week_starts_monday,
93169318
static_cast<GParamFlags>(G_PARAM_READWRITE));
@@ -10088,6 +10090,148 @@ garrow_trim_options_new(void)
1008810090
return GARROW_TRIM_OPTIONS(g_object_new(GARROW_TYPE_TRIM_OPTIONS, nullptr));
1008910091
}
1009010092

10093+
enum {
10094+
PROP_WEEK_OPTIONS_WEEK_STARTS_MONDAY = 1,
10095+
PROP_WEEK_OPTIONS_COUNT_FROM_ZERO,
10096+
PROP_WEEK_OPTIONS_FIRST_WEEK_IS_FULLY_IN_YEAR,
10097+
};
10098+
10099+
G_DEFINE_TYPE(GArrowWeekOptions, garrow_week_options, GARROW_TYPE_FUNCTION_OPTIONS)
10100+
10101+
static void
10102+
garrow_week_options_set_property(GObject *object,
10103+
guint prop_id,
10104+
const GValue *value,
10105+
GParamSpec *pspec)
10106+
{
10107+
auto options = garrow_week_options_get_raw(GARROW_WEEK_OPTIONS(object));
10108+
10109+
switch (prop_id) {
10110+
case PROP_WEEK_OPTIONS_WEEK_STARTS_MONDAY:
10111+
options->week_starts_monday = g_value_get_boolean(value);
10112+
break;
10113+
case PROP_WEEK_OPTIONS_COUNT_FROM_ZERO:
10114+
options->count_from_zero = g_value_get_boolean(value);
10115+
break;
10116+
case PROP_WEEK_OPTIONS_FIRST_WEEK_IS_FULLY_IN_YEAR:
10117+
options->first_week_is_fully_in_year = g_value_get_boolean(value);
10118+
break;
10119+
default:
10120+
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
10121+
break;
10122+
}
10123+
}
10124+
10125+
static void
10126+
garrow_week_options_get_property(GObject *object,
10127+
guint prop_id,
10128+
GValue *value,
10129+
GParamSpec *pspec)
10130+
{
10131+
auto options = garrow_week_options_get_raw(GARROW_WEEK_OPTIONS(object));
10132+
10133+
switch (prop_id) {
10134+
case PROP_WEEK_OPTIONS_WEEK_STARTS_MONDAY:
10135+
g_value_set_boolean(value, options->week_starts_monday);
10136+
break;
10137+
case PROP_WEEK_OPTIONS_COUNT_FROM_ZERO:
10138+
g_value_set_boolean(value, options->count_from_zero);
10139+
break;
10140+
case PROP_WEEK_OPTIONS_FIRST_WEEK_IS_FULLY_IN_YEAR:
10141+
g_value_set_boolean(value, options->first_week_is_fully_in_year);
10142+
break;
10143+
default:
10144+
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
10145+
break;
10146+
}
10147+
}
10148+
10149+
static void
10150+
garrow_week_options_init(GArrowWeekOptions *object)
10151+
{
10152+
auto priv = GARROW_FUNCTION_OPTIONS_GET_PRIVATE(object);
10153+
priv->options =
10154+
static_cast<arrow::compute::FunctionOptions *>(new arrow::compute::WeekOptions());
10155+
}
10156+
10157+
static void
10158+
garrow_week_options_class_init(GArrowWeekOptionsClass *klass)
10159+
{
10160+
auto gobject_class = G_OBJECT_CLASS(klass);
10161+
10162+
gobject_class->set_property = garrow_week_options_set_property;
10163+
gobject_class->get_property = garrow_week_options_get_property;
10164+
10165+
auto options = arrow::compute::WeekOptions::Defaults();
10166+
10167+
GParamSpec *spec;
10168+
/**
10169+
* GArrowWeekOptions:week-starts-monday:
10170+
*
10171+
* What day does the week start with (Monday=true, Sunday=false).
10172+
*
10173+
* Since: 23.0.0
10174+
*/
10175+
spec =
10176+
g_param_spec_boolean("week-starts-monday",
10177+
"Week starts Monday",
10178+
"What day does the week start with (Monday=true, Sunday=false)",
10179+
options.week_starts_monday,
10180+
static_cast<GParamFlags>(G_PARAM_READWRITE));
10181+
g_object_class_install_property(gobject_class,
10182+
PROP_WEEK_OPTIONS_WEEK_STARTS_MONDAY,
10183+
spec);
10184+
10185+
/**
10186+
* GArrowWeekOptions:count-from-zero:
10187+
*
10188+
* Dates from current year that fall into last ISO week of the previous year
10189+
* return 0 if true and 52 or 53 if false.
10190+
*
10191+
* Since: 23.0.0
10192+
*/
10193+
spec = g_param_spec_boolean("count-from-zero",
10194+
"Count from zero",
10195+
"Dates from current year that fall into last ISO week of "
10196+
"the previous year return 0 if true and 52 or 53 if false",
10197+
options.count_from_zero,
10198+
static_cast<GParamFlags>(G_PARAM_READWRITE));
10199+
g_object_class_install_property(gobject_class, PROP_WEEK_OPTIONS_COUNT_FROM_ZERO, spec);
10200+
10201+
/**
10202+
* GArrowWeekOptions:first-week-is-fully-in-year:
10203+
*
10204+
* Must the first week be fully in January (true), or is a week that begins
10205+
* on December 29, 30, or 31 considered to be the first week of the new
10206+
* year (false)?
10207+
*
10208+
* Since: 23.0.0
10209+
*/
10210+
spec = g_param_spec_boolean(
10211+
"first-week-is-fully-in-year",
10212+
"First week is fully in year",
10213+
"Must the first week be fully in January (true), or is a week that begins on "
10214+
"December 29, 30, or 31 considered to be the first week of the new year (false)?",
10215+
options.first_week_is_fully_in_year,
10216+
static_cast<GParamFlags>(G_PARAM_READWRITE));
10217+
g_object_class_install_property(gobject_class,
10218+
PROP_WEEK_OPTIONS_FIRST_WEEK_IS_FULLY_IN_YEAR,
10219+
spec);
10220+
}
10221+
10222+
/**
10223+
* garrow_week_options_new:
10224+
*
10225+
* Returns: A newly created #GArrowWeekOptions.
10226+
*
10227+
* Since: 23.0.0
10228+
*/
10229+
GArrowWeekOptions *
10230+
garrow_week_options_new(void)
10231+
{
10232+
return GARROW_WEEK_OPTIONS(g_object_new(GARROW_TYPE_WEEK_OPTIONS, nullptr));
10233+
}
10234+
1009110235
G_END_DECLS
1009210236

1009310237
arrow::Result<arrow::FieldRef>
@@ -10353,6 +10497,11 @@ garrow_function_options_new_raw(const arrow::compute::FunctionOptions *arrow_opt
1035310497
static_cast<const arrow::compute::TrimOptions *>(arrow_options);
1035410498
auto options = garrow_trim_options_new_raw(arrow_trim_options);
1035510499
return GARROW_FUNCTION_OPTIONS(options);
10500+
} else if (arrow_type_name == "WeekOptions") {
10501+
const auto arrow_week_options =
10502+
static_cast<const arrow::compute::WeekOptions *>(arrow_options);
10503+
auto options = garrow_week_options_new_raw(arrow_week_options);
10504+
return GARROW_FUNCTION_OPTIONS(options);
1035610505
} else {
1035710506
auto options = g_object_new(GARROW_TYPE_FUNCTION_OPTIONS, NULL);
1035810507
return GARROW_FUNCTION_OPTIONS(options);
@@ -11430,3 +11579,24 @@ garrow_trim_options_get_raw(GArrowTrimOptions *options)
1143011579
return static_cast<arrow::compute::TrimOptions *>(
1143111580
garrow_function_options_get_raw(GARROW_FUNCTION_OPTIONS(options)));
1143211581
}
11582+
11583+
GArrowWeekOptions *
11584+
garrow_week_options_new_raw(const arrow::compute::WeekOptions *arrow_options)
11585+
{
11586+
auto options = g_object_new(GARROW_TYPE_WEEK_OPTIONS,
11587+
"week-starts-monday",
11588+
arrow_options->week_starts_monday,
11589+
"count-from-zero",
11590+
arrow_options->count_from_zero,
11591+
"first-week-is-fully-in-year",
11592+
arrow_options->first_week_is_fully_in_year,
11593+
nullptr);
11594+
return GARROW_WEEK_OPTIONS(options);
11595+
}
11596+
11597+
arrow::compute::WeekOptions *
11598+
garrow_week_options_get_raw(GArrowWeekOptions *options)
11599+
{
11600+
return static_cast<arrow::compute::WeekOptions *>(
11601+
garrow_function_options_get_raw(GARROW_FUNCTION_OPTIONS(options)));
11602+
}

c_glib/arrow-glib/compute.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1742,4 +1742,17 @@ GARROW_AVAILABLE_IN_23_0
17421742
GArrowTrimOptions *
17431743
garrow_trim_options_new(void);
17441744

1745+
#define GARROW_TYPE_WEEK_OPTIONS (garrow_week_options_get_type())
1746+
GARROW_AVAILABLE_IN_23_0
1747+
G_DECLARE_DERIVABLE_TYPE(
1748+
GArrowWeekOptions, garrow_week_options, GARROW, WEEK_OPTIONS, GArrowFunctionOptions)
1749+
struct _GArrowWeekOptionsClass
1750+
{
1751+
GArrowFunctionOptionsClass parent_class;
1752+
};
1753+
1754+
GARROW_AVAILABLE_IN_23_0
1755+
GArrowWeekOptions *
1756+
garrow_week_options_new(void);
1757+
17451758
G_END_DECLS

c_glib/arrow-glib/compute.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,3 +323,8 @@ GArrowTrimOptions *
323323
garrow_trim_options_new_raw(const arrow::compute::TrimOptions *arrow_options);
324324
arrow::compute::TrimOptions *
325325
garrow_trim_options_get_raw(GArrowTrimOptions *options);
326+
327+
GArrowWeekOptions *
328+
garrow_week_options_new_raw(const arrow::compute::WeekOptions *arrow_options);
329+
arrow::compute::WeekOptions *
330+
garrow_week_options_get_raw(GArrowWeekOptions *options);

c_glib/test/test-week-options.rb

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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 TestWeekOptions < Test::Unit::TestCase
19+
include Helper::Buildable
20+
21+
def setup
22+
@options = Arrow::WeekOptions.new
23+
end
24+
25+
def test_week_starts_monday_property
26+
assert do
27+
@options.week_starts_monday?
28+
end
29+
@options.week_starts_monday = false
30+
assert do
31+
!@options.week_starts_monday?
32+
end
33+
end
34+
35+
def test_count_from_zero_property
36+
assert do
37+
!@options.count_from_zero?
38+
end
39+
@options.count_from_zero = true
40+
assert do
41+
@options.count_from_zero?
42+
end
43+
end
44+
45+
def test_first_week_is_fully_in_year_property
46+
assert do
47+
!@options.first_week_is_fully_in_year?
48+
end
49+
@options.first_week_is_fully_in_year = true
50+
assert do
51+
@options.first_week_is_fully_in_year?
52+
end
53+
end
54+
55+
def test_week_function_with_week_starts_monday
56+
omit("Missing tzdata on Windows") if Gem.win_platform?
57+
# January 1, 2023 (Sunday)
58+
args = [
59+
Arrow::ArrayDatum.new(build_timestamp_array(:milli, [1672531200000])),
60+
]
61+
@options.week_starts_monday = true
62+
week_function = Arrow::Function.find("week")
63+
result = week_function.execute(args, @options).value
64+
assert_equal(build_int64_array([52]), result)
65+
66+
@options.week_starts_monday = false
67+
result = week_function.execute(args, @options).value
68+
assert_equal(build_int64_array([1]), result)
69+
end
70+
end

0 commit comments

Comments
 (0)