Skip to content

Commit 1ddb440

Browse files
committed
Add GArrowWeekOptions
1 parent 1fe2a32 commit 1ddb440

File tree

4 files changed

+251
-0
lines changed

4 files changed

+251
-0
lines changed

c_glib/arrow-glib/compute.cpp

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,8 @@ G_BEGIN_DECLS
333333
* #GArrowTrimOptions is a class to customize the `utf8_trim`, `utf8_ltrim`,
334334
* `utf8_rtrim`, `ascii_trim`, `ascii_ltrim`, and `ascii_rtrim` functions.
335335
*
336+
* #GArrowWeekOptions is a class to customize the `week` function.
337+
*
336338
* There are many functions to compute data on an array.
337339
*/
338340

@@ -10315,6 +10317,140 @@ garrow_trim_options_new(void)
1031510317
return GARROW_TRIM_OPTIONS(g_object_new(GARROW_TYPE_TRIM_OPTIONS, NULL));
1031610318
}
1031710319

10320+
enum {
10321+
PROP_WEEK_OPTIONS_WEEK_STARTS_MONDAY = 1,
10322+
PROP_WEEK_OPTIONS_COUNT_FROM_ZERO,
10323+
PROP_WEEK_OPTIONS_FIRST_WEEK_IS_FULLY_IN_YEAR,
10324+
};
10325+
10326+
G_DEFINE_TYPE(GArrowWeekOptions, garrow_week_options, GARROW_TYPE_FUNCTION_OPTIONS)
10327+
10328+
static void
10329+
garrow_week_options_set_property(GObject *object,
10330+
guint prop_id,
10331+
const GValue *value,
10332+
GParamSpec *pspec)
10333+
{
10334+
auto options = garrow_week_options_get_raw(GARROW_WEEK_OPTIONS(object));
10335+
10336+
switch (prop_id) {
10337+
case PROP_WEEK_OPTIONS_WEEK_STARTS_MONDAY:
10338+
options->week_starts_monday = g_value_get_boolean(value);
10339+
break;
10340+
case PROP_WEEK_OPTIONS_COUNT_FROM_ZERO:
10341+
options->count_from_zero = g_value_get_boolean(value);
10342+
break;
10343+
case PROP_WEEK_OPTIONS_FIRST_WEEK_IS_FULLY_IN_YEAR:
10344+
options->first_week_is_fully_in_year = g_value_get_boolean(value);
10345+
break;
10346+
default:
10347+
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
10348+
break;
10349+
}
10350+
}
10351+
10352+
static void
10353+
garrow_week_options_get_property(GObject *object,
10354+
guint prop_id,
10355+
GValue *value,
10356+
GParamSpec *pspec)
10357+
{
10358+
auto options = garrow_week_options_get_raw(GARROW_WEEK_OPTIONS(object));
10359+
10360+
switch (prop_id) {
10361+
case PROP_WEEK_OPTIONS_WEEK_STARTS_MONDAY:
10362+
g_value_set_boolean(value, options->week_starts_monday);
10363+
break;
10364+
case PROP_WEEK_OPTIONS_COUNT_FROM_ZERO:
10365+
g_value_set_boolean(value, options->count_from_zero);
10366+
break;
10367+
case PROP_WEEK_OPTIONS_FIRST_WEEK_IS_FULLY_IN_YEAR:
10368+
g_value_set_boolean(value, options->first_week_is_fully_in_year);
10369+
break;
10370+
default:
10371+
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
10372+
break;
10373+
}
10374+
}
10375+
10376+
static void
10377+
garrow_week_options_init(GArrowWeekOptions *object)
10378+
{
10379+
auto priv = GARROW_FUNCTION_OPTIONS_GET_PRIVATE(object);
10380+
priv->options =
10381+
static_cast<arrow::compute::FunctionOptions *>(new arrow::compute::WeekOptions());
10382+
}
10383+
10384+
static void
10385+
garrow_week_options_class_init(GArrowWeekOptionsClass *klass)
10386+
{
10387+
auto gobject_class = G_OBJECT_CLASS(klass);
10388+
10389+
gobject_class->set_property = garrow_week_options_set_property;
10390+
gobject_class->get_property = garrow_week_options_get_property;
10391+
10392+
auto options = arrow::compute::WeekOptions::Defaults();
10393+
10394+
GParamSpec *spec;
10395+
/**
10396+
* GArrowWeekOptions:week-starts-monday:
10397+
*
10398+
* What day does the week start with (Monday=true, Sunday=false).
10399+
*
10400+
* Since: 23.0.0
10401+
*/
10402+
spec = g_param_spec_boolean("week-starts-monday",
10403+
"Week starts Monday",
10404+
"What day does the week start with (Monday=true, Sunday=false)",
10405+
options.week_starts_monday,
10406+
static_cast<GParamFlags>(G_PARAM_READWRITE));
10407+
g_object_class_install_property(gobject_class, PROP_WEEK_OPTIONS_WEEK_STARTS_MONDAY, spec);
10408+
10409+
/**
10410+
* GArrowWeekOptions:count-from-zero:
10411+
*
10412+
* Dates from current year that fall into last ISO week of the previous year
10413+
* return 0 if true and 52 or 53 if false.
10414+
*
10415+
* Since: 23.0.0
10416+
*/
10417+
spec = g_param_spec_boolean("count-from-zero",
10418+
"Count from zero",
10419+
"Dates from current year that fall into last ISO week of the previous year return 0 if true and 52 or 53 if false",
10420+
options.count_from_zero,
10421+
static_cast<GParamFlags>(G_PARAM_READWRITE));
10422+
g_object_class_install_property(gobject_class, PROP_WEEK_OPTIONS_COUNT_FROM_ZERO, spec);
10423+
10424+
/**
10425+
* GArrowWeekOptions:first-week-is-fully-in-year:
10426+
*
10427+
* Must the first week be fully in January (true), or is a week that begins
10428+
* on December 29, 30, or 31 considered to be the first week of the new
10429+
* year (false)?
10430+
*
10431+
* Since: 23.0.0
10432+
*/
10433+
spec = g_param_spec_boolean("first-week-is-fully-in-year",
10434+
"First week is fully in year",
10435+
"Must the first week be fully in January (true), or is a week that begins on December 29, 30, or 31 considered to be the first week of the new year (false)?",
10436+
options.first_week_is_fully_in_year,
10437+
static_cast<GParamFlags>(G_PARAM_READWRITE));
10438+
g_object_class_install_property(gobject_class, PROP_WEEK_OPTIONS_FIRST_WEEK_IS_FULLY_IN_YEAR, spec);
10439+
}
10440+
10441+
/**
10442+
* garrow_week_options_new:
10443+
*
10444+
* Returns: A newly created #GArrowWeekOptions.
10445+
*
10446+
* Since: 23.0.0
10447+
*/
10448+
GArrowWeekOptions *
10449+
garrow_week_options_new(void)
10450+
{
10451+
return GARROW_WEEK_OPTIONS(g_object_new(GARROW_TYPE_WEEK_OPTIONS, NULL));
10452+
}
10453+
1031810454
G_END_DECLS
1031910455

1032010456
arrow::Result<arrow::FieldRef>
@@ -10586,6 +10722,11 @@ garrow_function_options_new_raw(const arrow::compute::FunctionOptions *arrow_opt
1058610722
static_cast<const arrow::compute::TrimOptions *>(arrow_options);
1058710723
auto options = garrow_trim_options_new_raw(arrow_trim_options);
1058810724
return GARROW_FUNCTION_OPTIONS(options);
10725+
} else if (arrow_type_name == "WeekOptions") {
10726+
const auto arrow_week_options =
10727+
static_cast<const arrow::compute::WeekOptions *>(arrow_options);
10728+
auto options = garrow_week_options_new_raw(arrow_week_options);
10729+
return GARROW_FUNCTION_OPTIONS(options);
1058910730
} else {
1059010731
auto options = g_object_new(GARROW_TYPE_FUNCTION_OPTIONS, NULL);
1059110732
return GARROW_FUNCTION_OPTIONS(options);
@@ -11691,3 +11832,24 @@ garrow_trim_options_get_raw(GArrowTrimOptions *options)
1169111832
return static_cast<arrow::compute::TrimOptions *>(
1169211833
garrow_function_options_get_raw(GARROW_FUNCTION_OPTIONS(options)));
1169311834
}
11835+
11836+
GArrowWeekOptions *
11837+
garrow_week_options_new_raw(const arrow::compute::WeekOptions *arrow_options)
11838+
{
11839+
auto options = g_object_new(GARROW_TYPE_WEEK_OPTIONS,
11840+
"week-starts-monday",
11841+
arrow_options->week_starts_monday,
11842+
"count-from-zero",
11843+
arrow_options->count_from_zero,
11844+
"first-week-is-fully-in-year",
11845+
arrow_options->first_week_is_fully_in_year,
11846+
NULL);
11847+
return GARROW_WEEK_OPTIONS(options);
11848+
}
11849+
11850+
arrow::compute::WeekOptions *
11851+
garrow_week_options_get_raw(GArrowWeekOptions *options)
11852+
{
11853+
return static_cast<arrow::compute::WeekOptions *>(
11854+
garrow_function_options_get_raw(GARROW_FUNCTION_OPTIONS(options)));
11855+
}

c_glib/arrow-glib/compute.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1746,4 +1746,17 @@ GARROW_AVAILABLE_IN_23_0
17461746
GArrowTrimOptions *
17471747
garrow_trim_options_new(void);
17481748

1749+
#define GARROW_TYPE_WEEK_OPTIONS (garrow_week_options_get_type())
1750+
GARROW_AVAILABLE_IN_23_0
1751+
G_DECLARE_DERIVABLE_TYPE(
1752+
GArrowWeekOptions, garrow_week_options, GARROW, WEEK_OPTIONS, GArrowFunctionOptions)
1753+
struct _GArrowWeekOptionsClass
1754+
{
1755+
GArrowFunctionOptionsClass parent_class;
1756+
};
1757+
1758+
GARROW_AVAILABLE_IN_23_0
1759+
GArrowWeekOptions *
1760+
garrow_week_options_new(void);
1761+
17491762
G_END_DECLS

c_glib/arrow-glib/compute.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,3 +327,8 @@ GArrowTrimOptions *
327327
garrow_trim_options_new_raw(const arrow::compute::TrimOptions *arrow_options);
328328
arrow::compute::TrimOptions *
329329
garrow_trim_options_get_raw(GArrowTrimOptions *options);
330+
331+
GArrowWeekOptions *
332+
garrow_week_options_new_raw(const arrow::compute::WeekOptions *arrow_options);
333+
arrow::compute::WeekOptions *
334+
garrow_week_options_get_raw(GArrowWeekOptions *options);

c_glib/test/test-week-options.rb

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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
71+

0 commit comments

Comments
 (0)