Skip to content

Commit 9b60b26

Browse files
committed
Add GArrowDayOfWeekOptions
1 parent 5499afa commit 9b60b26

File tree

4 files changed

+230
-0
lines changed

4 files changed

+230
-0
lines changed

c_glib/arrow-glib/compute.cpp

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,8 @@ G_BEGIN_DECLS
251251
* #GArrowStructFieldOptions is a class to customize the `struct_field`
252252
* function.
253253
*
254+
* #GArrowDayOfWeekOptions is a class to customize the `day_of_week` function.
255+
*
254256
* There are many functions to compute data on an array.
255257
*/
256258

@@ -6338,6 +6340,127 @@ garrow_struct_field_options_new(void)
63386340
return GARROW_STRUCT_FIELD_OPTIONS(options);
63396341
}
63406342

6343+
enum {
6344+
PROP_DAY_OF_WEEK_OPTIONS_COUNT_FROM_ZERO = 1,
6345+
PROP_DAY_OF_WEEK_OPTIONS_WEEK_START,
6346+
};
6347+
6348+
G_DEFINE_TYPE(GArrowDayOfWeekOptions,
6349+
garrow_day_of_week_options,
6350+
GARROW_TYPE_FUNCTION_OPTIONS)
6351+
6352+
static void
6353+
garrow_day_of_week_options_set_property(GObject *object,
6354+
guint prop_id,
6355+
const GValue *value,
6356+
GParamSpec *pspec)
6357+
{
6358+
auto options = garrow_day_of_week_options_get_raw(GARROW_DAY_OF_WEEK_OPTIONS(object));
6359+
6360+
switch (prop_id) {
6361+
case PROP_DAY_OF_WEEK_OPTIONS_COUNT_FROM_ZERO:
6362+
options->count_from_zero = g_value_get_boolean(value);
6363+
break;
6364+
case PROP_DAY_OF_WEEK_OPTIONS_WEEK_START:
6365+
options->week_start = g_value_get_uint(value);
6366+
break;
6367+
default:
6368+
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
6369+
break;
6370+
}
6371+
}
6372+
6373+
static void
6374+
garrow_day_of_week_options_get_property(GObject *object,
6375+
guint prop_id,
6376+
GValue *value,
6377+
GParamSpec *pspec)
6378+
{
6379+
auto options = garrow_day_of_week_options_get_raw(GARROW_DAY_OF_WEEK_OPTIONS(object));
6380+
6381+
switch (prop_id) {
6382+
case PROP_DAY_OF_WEEK_OPTIONS_COUNT_FROM_ZERO:
6383+
g_value_set_boolean(value, options->count_from_zero);
6384+
break;
6385+
case PROP_DAY_OF_WEEK_OPTIONS_WEEK_START:
6386+
g_value_set_uint(value, options->week_start);
6387+
break;
6388+
default:
6389+
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
6390+
break;
6391+
}
6392+
}
6393+
6394+
static void
6395+
garrow_day_of_week_options_init(GArrowDayOfWeekOptions *object)
6396+
{
6397+
auto priv = GARROW_FUNCTION_OPTIONS_GET_PRIVATE(object);
6398+
priv->options = static_cast<arrow::compute::FunctionOptions *>(
6399+
new arrow::compute::DayOfWeekOptions());
6400+
}
6401+
6402+
static void
6403+
garrow_day_of_week_options_class_init(GArrowDayOfWeekOptionsClass *klass)
6404+
{
6405+
auto gobject_class = G_OBJECT_CLASS(klass);
6406+
6407+
gobject_class->set_property = garrow_day_of_week_options_set_property;
6408+
gobject_class->get_property = garrow_day_of_week_options_get_property;
6409+
6410+
arrow::compute::DayOfWeekOptions options;
6411+
6412+
GParamSpec *spec;
6413+
/**
6414+
* GArrowDayOfWeekOptions:count-from-zero:
6415+
*
6416+
* Number days from 0 if true and from 1 if false.
6417+
*
6418+
* Since: 23.0.0
6419+
*/
6420+
spec = g_param_spec_boolean("count-from-zero",
6421+
"Count from zero",
6422+
"Number days from 0 if true and from 1 if false",
6423+
options.count_from_zero,
6424+
static_cast<GParamFlags>(G_PARAM_READWRITE));
6425+
g_object_class_install_property(gobject_class,
6426+
PROP_DAY_OF_WEEK_OPTIONS_COUNT_FROM_ZERO,
6427+
spec);
6428+
6429+
/**
6430+
* GArrowDayOfWeekOptions:week-start:
6431+
*
6432+
* What day does the week start with (Monday=1, Sunday=7).
6433+
* The numbering is unaffected by the count_from_zero parameter.
6434+
*
6435+
* Since: 23.0.0
6436+
*/
6437+
spec = g_param_spec_uint("week-start",
6438+
"Week start",
6439+
"What day does the week start with (Monday=1, Sunday=7). The "
6440+
"numbering is unaffected by the count_from_zero parameter",
6441+
1,
6442+
7,
6443+
options.week_start,
6444+
static_cast<GParamFlags>(G_PARAM_READWRITE));
6445+
g_object_class_install_property(gobject_class,
6446+
PROP_DAY_OF_WEEK_OPTIONS_WEEK_START,
6447+
spec);
6448+
}
6449+
6450+
/**
6451+
* garrow_day_of_week_options_new:
6452+
*
6453+
* Returns: A newly created #GArrowDayOfWeekOptions.
6454+
*
6455+
* Since: 23.0.0
6456+
*/
6457+
GArrowDayOfWeekOptions *
6458+
garrow_day_of_week_options_new(void)
6459+
{
6460+
auto options = g_object_new(GARROW_TYPE_DAY_OF_WEEK_OPTIONS, NULL);
6461+
return GARROW_DAY_OF_WEEK_OPTIONS(options);
6462+
}
6463+
63416464
G_END_DECLS
63426465

63436466
arrow::Result<arrow::FieldRef>
@@ -6469,6 +6592,11 @@ garrow_function_options_new_raw(const arrow::compute::FunctionOptions *arrow_opt
64696592
static_cast<const arrow::compute::StructFieldOptions *>(arrow_options);
64706593
auto options = garrow_struct_field_options_new_raw(arrow_struct_field_options);
64716594
return GARROW_FUNCTION_OPTIONS(options);
6595+
} else if (arrow_type_name == "DayOfWeekOptions") {
6596+
const auto arrow_day_of_week_options =
6597+
static_cast<const arrow::compute::DayOfWeekOptions *>(arrow_options);
6598+
auto options = garrow_day_of_week_options_new_raw(arrow_day_of_week_options);
6599+
return GARROW_FUNCTION_OPTIONS(options);
64726600
} else {
64736601
auto options = g_object_new(GARROW_TYPE_FUNCTION_OPTIONS, NULL);
64746602
return GARROW_FUNCTION_OPTIONS(options);
@@ -6987,3 +7115,21 @@ garrow_struct_field_options_get_raw(GArrowStructFieldOptions *options)
69877115
return static_cast<arrow::compute::StructFieldOptions *>(
69887116
garrow_function_options_get_raw(GARROW_FUNCTION_OPTIONS(options)));
69897117
}
7118+
7119+
GArrowDayOfWeekOptions *
7120+
garrow_day_of_week_options_new_raw(const arrow::compute::DayOfWeekOptions *arrow_options)
7121+
{
7122+
return GARROW_DAY_OF_WEEK_OPTIONS(g_object_new(GARROW_TYPE_DAY_OF_WEEK_OPTIONS,
7123+
"count-from-zero",
7124+
arrow_options->count_from_zero,
7125+
"week-start",
7126+
arrow_options->week_start,
7127+
NULL));
7128+
}
7129+
7130+
arrow::compute::DayOfWeekOptions *
7131+
garrow_day_of_week_options_get_raw(GArrowDayOfWeekOptions *options)
7132+
{
7133+
return static_cast<arrow::compute::DayOfWeekOptions *>(
7134+
garrow_function_options_get_raw(GARROW_FUNCTION_OPTIONS(options)));
7135+
}

c_glib/arrow-glib/compute.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,4 +1122,20 @@ GARROW_AVAILABLE_IN_16_0
11221122
GArrowStructFieldOptions *
11231123
garrow_struct_field_options_new(void);
11241124

1125+
#define GARROW_TYPE_DAY_OF_WEEK_OPTIONS (garrow_day_of_week_options_get_type())
1126+
GARROW_AVAILABLE_IN_23_0
1127+
G_DECLARE_DERIVABLE_TYPE(GArrowDayOfWeekOptions,
1128+
garrow_day_of_week_options,
1129+
GARROW,
1130+
DAY_OF_WEEK_OPTIONS,
1131+
GArrowFunctionOptions)
1132+
struct _GArrowDayOfWeekOptionsClass
1133+
{
1134+
GArrowFunctionOptionsClass parent_class;
1135+
};
1136+
1137+
GARROW_AVAILABLE_IN_23_0
1138+
GArrowDayOfWeekOptions *
1139+
garrow_day_of_week_options_new(void);
1140+
11251141
G_END_DECLS

c_glib/arrow-glib/compute.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,3 +175,8 @@ garrow_struct_field_options_new_raw(
175175
const arrow::compute::StructFieldOptions *arrow_options);
176176
arrow::compute::StructFieldOptions *
177177
garrow_struct_field_options_get_raw(GArrowStructFieldOptions *options);
178+
179+
GArrowDayOfWeekOptions *
180+
garrow_day_of_week_options_new_raw(const arrow::compute::DayOfWeekOptions *arrow_options);
181+
arrow::compute::DayOfWeekOptions *
182+
garrow_day_of_week_options_get_raw(GArrowDayOfWeekOptions *options);
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
Arrow::ArrayDatum.new(build_timestamp_array(:milli, [1504953190000])),
45+
]
46+
@options.count_from_zero = false
47+
day_of_week_function = Arrow::Function.find("day_of_week")
48+
assert_equal(build_int64_array([6]),
49+
day_of_week_function.execute(args, @options).value)
50+
end
51+
52+
def test_day_of_week_function_with_week_start
53+
omit("Missing tzdata on Windows") if Gem.win_platform?
54+
args = [
55+
Arrow::ArrayDatum.new(build_timestamp_array(:milli, [1504953190000])),
56+
]
57+
@options.week_start = 2
58+
day_of_week_function = Arrow::Function.find("day_of_week")
59+
assert_equal(build_int64_array([4]),
60+
day_of_week_function.execute(args, @options).value)
61+
end
62+
end
63+

0 commit comments

Comments
 (0)