This is an automated email from the ASF dual-hosted git repository.
kou pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow.git
The following commit(s) were added to refs/heads/main by this push:
new 3cf59d87b8 GH-48365: [GLib][Ruby] Add DayOfWeekOptions (#48372)
3cf59d87b8 is described below
commit 3cf59d87b8ae03790037994ae61b28ee41568e85
Author: Sten Larsson <[email protected]>
AuthorDate: Wed Dec 10 22:44:51 2025 +0100
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 <[email protected]>
Signed-off-by: Sutou Kouhei <[email protected]>
---
c_glib/arrow-glib/compute.cpp | 146 ++++++++++++++++++++++++++++++++
c_glib/arrow-glib/compute.h | 16 ++++
c_glib/arrow-glib/compute.hpp | 5 ++
c_glib/test/test-day-of-week-options.rb | 64 ++++++++++++++
4 files changed, 231 insertions(+)
diff --git a/c_glib/arrow-glib/compute.cpp b/c_glib/arrow-glib/compute.cpp
index a38ef66923..b021379bee 100644
--- a/c_glib/arrow-glib/compute.cpp
+++ b/c_glib/arrow-glib/compute.cpp
@@ -264,6 +264,8 @@ G_BEGIN_DECLS
* #GArrowElementWiseAggregateOptions is a class to customize element-wise
* aggregate functions such as `min_element_wise` and `max_element_wise`.
*
+ * #GArrowDayOfWeekOptions is a class to customize the `day_of_week` function.
+ *
* There are many functions to compute data on an array.
*/
@@ -6869,6 +6871,127 @@ garrow_element_wise_aggregate_options_new(void)
return GARROW_ELEMENT_WISE_AGGREGATE_OPTIONS(options);
}
+enum {
+ PROP_DAY_OF_WEEK_OPTIONS_COUNT_FROM_ZERO = 1,
+ PROP_DAY_OF_WEEK_OPTIONS_WEEK_START,
+};
+
+G_DEFINE_TYPE(GArrowDayOfWeekOptions,
+ garrow_day_of_week_options,
+ GARROW_TYPE_FUNCTION_OPTIONS)
+
+static void
+garrow_day_of_week_options_set_property(GObject *object,
+ guint prop_id,
+ const GValue *value,
+ GParamSpec *pspec)
+{
+ auto options =
garrow_day_of_week_options_get_raw(GARROW_DAY_OF_WEEK_OPTIONS(object));
+
+ switch (prop_id) {
+ case PROP_DAY_OF_WEEK_OPTIONS_COUNT_FROM_ZERO:
+ options->count_from_zero = g_value_get_boolean(value);
+ break;
+ case PROP_DAY_OF_WEEK_OPTIONS_WEEK_START:
+ options->week_start = g_value_get_uint(value);
+ break;
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
+ break;
+ }
+}
+
+static void
+garrow_day_of_week_options_get_property(GObject *object,
+ guint prop_id,
+ GValue *value,
+ GParamSpec *pspec)
+{
+ auto options =
garrow_day_of_week_options_get_raw(GARROW_DAY_OF_WEEK_OPTIONS(object));
+
+ switch (prop_id) {
+ case PROP_DAY_OF_WEEK_OPTIONS_COUNT_FROM_ZERO:
+ g_value_set_boolean(value, options->count_from_zero);
+ break;
+ case PROP_DAY_OF_WEEK_OPTIONS_WEEK_START:
+ g_value_set_uint(value, options->week_start);
+ break;
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
+ break;
+ }
+}
+
+static void
+garrow_day_of_week_options_init(GArrowDayOfWeekOptions *object)
+{
+ auto priv = GARROW_FUNCTION_OPTIONS_GET_PRIVATE(object);
+ priv->options = static_cast<arrow::compute::FunctionOptions *>(
+ new arrow::compute::DayOfWeekOptions());
+}
+
+static void
+garrow_day_of_week_options_class_init(GArrowDayOfWeekOptionsClass *klass)
+{
+ auto gobject_class = G_OBJECT_CLASS(klass);
+
+ gobject_class->set_property = garrow_day_of_week_options_set_property;
+ gobject_class->get_property = garrow_day_of_week_options_get_property;
+
+ arrow::compute::DayOfWeekOptions options;
+
+ GParamSpec *spec;
+ /**
+ * GArrowDayOfWeekOptions:count-from-zero:
+ *
+ * Number days from 0 if true and from 1 if false.
+ *
+ * Since: 23.0.0
+ */
+ spec = g_param_spec_boolean("count-from-zero",
+ "Count from zero",
+ "Number days from 0 if true and from 1 if false",
+ options.count_from_zero,
+ static_cast<GParamFlags>(G_PARAM_READWRITE));
+ g_object_class_install_property(gobject_class,
+ PROP_DAY_OF_WEEK_OPTIONS_COUNT_FROM_ZERO,
+ spec);
+
+ /**
+ * GArrowDayOfWeekOptions:week-start:
+ *
+ * What day does the week start with (Monday=1, Sunday=7).
+ * The numbering is unaffected by the count_from_zero parameter.
+ *
+ * Since: 23.0.0
+ */
+ spec = g_param_spec_uint("week-start",
+ "Week start",
+ "What day does the week start with (Monday=1,
Sunday=7). The "
+ "numbering is unaffected by the count_from_zero
parameter",
+ 1,
+ 7,
+ options.week_start,
+ static_cast<GParamFlags>(G_PARAM_READWRITE));
+ g_object_class_install_property(gobject_class,
+ PROP_DAY_OF_WEEK_OPTIONS_WEEK_START,
+ spec);
+}
+
+/**
+ * garrow_day_of_week_options_new:
+ *
+ * Returns: A newly created #GArrowDayOfWeekOptions.
+ *
+ * Since: 23.0.0
+ */
+GArrowDayOfWeekOptions *
+garrow_day_of_week_options_new(void)
+{
+ auto options = g_object_new(GARROW_TYPE_DAY_OF_WEEK_OPTIONS, NULL);
+ return GARROW_DAY_OF_WEEK_OPTIONS(options);
+}
+
G_END_DECLS
arrow::Result<arrow::FieldRef>
@@ -7022,6 +7145,11 @@ garrow_function_options_new_raw(const
arrow::compute::FunctionOptions *arrow_opt
auto options =
garrow_element_wise_aggregate_options_new_raw(arrow_element_wise_aggregate_options);
return GARROW_FUNCTION_OPTIONS(options);
+ } else if (arrow_type_name == "DayOfWeekOptions") {
+ const auto arrow_day_of_week_options =
+ static_cast<const arrow::compute::DayOfWeekOptions *>(arrow_options);
+ auto options =
garrow_day_of_week_options_new_raw(arrow_day_of_week_options);
+ return GARROW_FUNCTION_OPTIONS(options);
} else {
auto options = g_object_new(GARROW_TYPE_FUNCTION_OPTIONS, NULL);
return GARROW_FUNCTION_OPTIONS(options);
@@ -7626,3 +7754,21 @@
garrow_element_wise_aggregate_options_get_raw(GArrowElementWiseAggregateOptions
return static_cast<arrow::compute::ElementWiseAggregateOptions *>(
garrow_function_options_get_raw(GARROW_FUNCTION_OPTIONS(options)));
}
+
+GArrowDayOfWeekOptions *
+garrow_day_of_week_options_new_raw(const arrow::compute::DayOfWeekOptions
*arrow_options)
+{
+ return
GARROW_DAY_OF_WEEK_OPTIONS(g_object_new(GARROW_TYPE_DAY_OF_WEEK_OPTIONS,
+ "count-from-zero",
+
arrow_options->count_from_zero,
+ "week-start",
+ arrow_options->week_start,
+ NULL));
+}
+
+arrow::compute::DayOfWeekOptions *
+garrow_day_of_week_options_get_raw(GArrowDayOfWeekOptions *options)
+{
+ return static_cast<arrow::compute::DayOfWeekOptions *>(
+ garrow_function_options_get_raw(GARROW_FUNCTION_OPTIONS(options)));
+}
diff --git a/c_glib/arrow-glib/compute.h b/c_glib/arrow-glib/compute.h
index aed2ab1513..a8bdd33fbb 100644
--- a/c_glib/arrow-glib/compute.h
+++ b/c_glib/arrow-glib/compute.h
@@ -1240,4 +1240,20 @@ GARROW_AVAILABLE_IN_23_0
GArrowElementWiseAggregateOptions *
garrow_element_wise_aggregate_options_new(void);
+#define GARROW_TYPE_DAY_OF_WEEK_OPTIONS (garrow_day_of_week_options_get_type())
+GARROW_AVAILABLE_IN_23_0
+G_DECLARE_DERIVABLE_TYPE(GArrowDayOfWeekOptions,
+ garrow_day_of_week_options,
+ GARROW,
+ DAY_OF_WEEK_OPTIONS,
+ GArrowFunctionOptions)
+struct _GArrowDayOfWeekOptionsClass
+{
+ GArrowFunctionOptionsClass parent_class;
+};
+
+GARROW_AVAILABLE_IN_23_0
+GArrowDayOfWeekOptions *
+garrow_day_of_week_options_new(void);
+
G_END_DECLS
diff --git a/c_glib/arrow-glib/compute.hpp b/c_glib/arrow-glib/compute.hpp
index 139082c635..61d1aad832 100644
--- a/c_glib/arrow-glib/compute.hpp
+++ b/c_glib/arrow-glib/compute.hpp
@@ -198,3 +198,8 @@ garrow_element_wise_aggregate_options_new_raw(
const arrow::compute::ElementWiseAggregateOptions *arrow_options);
arrow::compute::ElementWiseAggregateOptions *
garrow_element_wise_aggregate_options_get_raw(GArrowElementWiseAggregateOptions
*options);
+
+GArrowDayOfWeekOptions *
+garrow_day_of_week_options_new_raw(const arrow::compute::DayOfWeekOptions
*arrow_options);
+arrow::compute::DayOfWeekOptions *
+garrow_day_of_week_options_get_raw(GArrowDayOfWeekOptions *options);
diff --git a/c_glib/test/test-day-of-week-options.rb
b/c_glib/test/test-day-of-week-options.rb
new file mode 100644
index 0000000000..8f76956fb4
--- /dev/null
+++ b/c_glib/test/test-day-of-week-options.rb
@@ -0,0 +1,64 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+class TestDayOfWeekOptions < Test::Unit::TestCase
+ include Helper::Buildable
+
+ def setup
+ @options = Arrow::DayOfWeekOptions.new
+ end
+
+ def test_count_from_zero_property
+ assert do
+ @options.count_from_zero?
+ end
+ @options.count_from_zero = false
+ assert do
+ [email protected]_from_zero?
+ end
+ end
+
+ def test_week_start_property
+ assert_equal(1, @options.week_start)
+ @options.week_start = 7
+ assert_equal(7, @options.week_start)
+ end
+
+ def test_day_of_week_function_with_count_from_zero_false
+ omit("Missing tzdata on Windows") if Gem.win_platform?
+ args = [
+ # 2017-09-09T10:33:10Z (Saturday)
+ Arrow::ArrayDatum.new(build_timestamp_array(:milli, [1504953190000])),
+ ]
+ @options.count_from_zero = false
+ day_of_week_function = Arrow::Function.find("day_of_week")
+ assert_equal(build_int64_array([6]),
+ day_of_week_function.execute(args, @options).value)
+ end
+
+ def test_day_of_week_function_with_week_start
+ omit("Missing tzdata on Windows") if Gem.win_platform?
+ args = [
+ # 2017-09-09T10:33:10Z (Saturday)
+ Arrow::ArrayDatum.new(build_timestamp_array(:milli, [1504953190000])),
+ ]
+ @options.week_start = 2
+ day_of_week_function = Arrow::Function.find("day_of_week")
+ assert_equal(build_int64_array([4]),
+ day_of_week_function.execute(args, @options).value)
+ end
+end