Skip to content

Commit 469976f

Browse files
authored
GH-48494: [GLib][Ruby] Add NullOptions (#48515)
### Rationale for this change The `NullOptions` class is not available in GLib/Ruby, and it is used together with the `is_null` compute function. ### What changes are included in this PR? This adds the `NullOptions` class to GLib. ### Are these changes tested? Yes, with Ruby unit tests. ### Are there any user-facing changes? Yes, a new class. * GitHub Issue: #48494 Authored-by: Sten Larsson <[email protected]> Signed-off-by: Sutou Kouhei <[email protected]>
1 parent 1e057e5 commit 469976f

File tree

4 files changed

+174
-0
lines changed

4 files changed

+174
-0
lines changed

c_glib/arrow-glib/compute.cpp

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,8 @@ G_BEGIN_DECLS
287287
*
288288
* #GArrowModeOptions is a class to customize the `mode` function.
289289
*
290+
* #GArrowNullOptions is a class to customize the `is_null` function.
291+
*
290292
* There are many functions to compute data on an array.
291293
*/
292294

@@ -8016,6 +8018,95 @@ garrow_mode_options_new(void)
80168018
return GARROW_MODE_OPTIONS(g_object_new(GARROW_TYPE_MODE_OPTIONS, NULL));
80178019
}
80188020

8021+
enum {
8022+
PROP_NULL_OPTIONS_NAN_IS_NULL = 1,
8023+
};
8024+
8025+
G_DEFINE_TYPE(GArrowNullOptions, garrow_null_options, GARROW_TYPE_FUNCTION_OPTIONS)
8026+
8027+
static void
8028+
garrow_null_options_set_property(GObject *object,
8029+
guint prop_id,
8030+
const GValue *value,
8031+
GParamSpec *pspec)
8032+
{
8033+
auto options = garrow_null_options_get_raw(GARROW_NULL_OPTIONS(object));
8034+
8035+
switch (prop_id) {
8036+
case PROP_NULL_OPTIONS_NAN_IS_NULL:
8037+
options->nan_is_null = g_value_get_boolean(value);
8038+
break;
8039+
default:
8040+
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
8041+
break;
8042+
}
8043+
}
8044+
8045+
static void
8046+
garrow_null_options_get_property(GObject *object,
8047+
guint prop_id,
8048+
GValue *value,
8049+
GParamSpec *pspec)
8050+
{
8051+
auto options = garrow_null_options_get_raw(GARROW_NULL_OPTIONS(object));
8052+
8053+
switch (prop_id) {
8054+
case PROP_NULL_OPTIONS_NAN_IS_NULL:
8055+
g_value_set_boolean(value, options->nan_is_null);
8056+
break;
8057+
default:
8058+
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
8059+
break;
8060+
}
8061+
}
8062+
8063+
static void
8064+
garrow_null_options_init(GArrowNullOptions *object)
8065+
{
8066+
auto priv = GARROW_FUNCTION_OPTIONS_GET_PRIVATE(object);
8067+
priv->options =
8068+
static_cast<arrow::compute::FunctionOptions *>(new arrow::compute::NullOptions());
8069+
}
8070+
8071+
static void
8072+
garrow_null_options_class_init(GArrowNullOptionsClass *klass)
8073+
{
8074+
auto gobject_class = G_OBJECT_CLASS(klass);
8075+
8076+
gobject_class->set_property = garrow_null_options_set_property;
8077+
gobject_class->get_property = garrow_null_options_get_property;
8078+
8079+
arrow::compute::NullOptions options;
8080+
8081+
GParamSpec *spec;
8082+
/**
8083+
* GArrowNullOptions:nan-is-null:
8084+
*
8085+
* Whether floating-point NaN values are considered null.
8086+
*
8087+
* Since: 23.0.0
8088+
*/
8089+
spec = g_param_spec_boolean("nan-is-null",
8090+
"NaN is null",
8091+
"Whether floating-point NaN values are considered null",
8092+
options.nan_is_null,
8093+
static_cast<GParamFlags>(G_PARAM_READWRITE));
8094+
g_object_class_install_property(gobject_class, PROP_NULL_OPTIONS_NAN_IS_NULL, spec);
8095+
}
8096+
8097+
/**
8098+
* garrow_null_options_new:
8099+
*
8100+
* Returns: A newly created #GArrowNullOptions.
8101+
*
8102+
* Since: 23.0.0
8103+
*/
8104+
GArrowNullOptions *
8105+
garrow_null_options_new(void)
8106+
{
8107+
return GARROW_NULL_OPTIONS(g_object_new(GARROW_TYPE_NULL_OPTIONS, NULL));
8108+
}
8109+
80198110
G_END_DECLS
80208111

80218112
arrow::Result<arrow::FieldRef>
@@ -8205,6 +8296,11 @@ garrow_function_options_new_raw(const arrow::compute::FunctionOptions *arrow_opt
82058296
static_cast<const arrow::compute::ModeOptions *>(arrow_options);
82068297
auto options = garrow_mode_options_new_raw(arrow_mode_options);
82078298
return GARROW_FUNCTION_OPTIONS(options);
8299+
} else if (arrow_type_name == "NullOptions") {
8300+
const auto arrow_null_options =
8301+
static_cast<const arrow::compute::NullOptions *>(arrow_options);
8302+
auto options = garrow_null_options_new_raw(arrow_null_options);
8303+
return GARROW_FUNCTION_OPTIONS(options);
82088304
} else {
82098305
auto options = g_object_new(GARROW_TYPE_FUNCTION_OPTIONS, NULL);
82108306
return GARROW_FUNCTION_OPTIONS(options);
@@ -8979,3 +9075,19 @@ garrow_mode_options_get_raw(GArrowModeOptions *options)
89799075
return static_cast<arrow::compute::ModeOptions *>(
89809076
garrow_function_options_get_raw(GARROW_FUNCTION_OPTIONS(options)));
89819077
}
9078+
9079+
GArrowNullOptions *
9080+
garrow_null_options_new_raw(const arrow::compute::NullOptions *arrow_options)
9081+
{
9082+
return GARROW_NULL_OPTIONS(g_object_new(GARROW_TYPE_NULL_OPTIONS,
9083+
"nan-is-null",
9084+
arrow_options->nan_is_null,
9085+
NULL));
9086+
}
9087+
9088+
arrow::compute::NullOptions *
9089+
garrow_null_options_get_raw(GArrowNullOptions *options)
9090+
{
9091+
return static_cast<arrow::compute::NullOptions *>(
9092+
garrow_function_options_get_raw(GARROW_FUNCTION_OPTIONS(options)));
9093+
}

c_glib/arrow-glib/compute.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1433,4 +1433,17 @@ GARROW_AVAILABLE_IN_23_0
14331433
GArrowModeOptions *
14341434
garrow_mode_options_new(void);
14351435

1436+
#define GARROW_TYPE_NULL_OPTIONS (garrow_null_options_get_type())
1437+
GARROW_AVAILABLE_IN_23_0
1438+
G_DECLARE_DERIVABLE_TYPE(
1439+
GArrowNullOptions, garrow_null_options, GARROW, NULL_OPTIONS, GArrowFunctionOptions)
1440+
struct _GArrowNullOptionsClass
1441+
{
1442+
GArrowFunctionOptionsClass parent_class;
1443+
};
1444+
1445+
GARROW_AVAILABLE_IN_23_0
1446+
GArrowNullOptions *
1447+
garrow_null_options_new(void);
1448+
14361449
G_END_DECLS

c_glib/arrow-glib/compute.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,3 +241,8 @@ GArrowModeOptions *
241241
garrow_mode_options_new_raw(const arrow::compute::ModeOptions *arrow_options);
242242
arrow::compute::ModeOptions *
243243
garrow_mode_options_get_raw(GArrowModeOptions *options);
244+
245+
GArrowNullOptions *
246+
garrow_null_options_new_raw(const arrow::compute::NullOptions *arrow_options);
247+
arrow::compute::NullOptions *
248+
garrow_null_options_get_raw(GArrowNullOptions *options);

c_glib/test/test-null-options.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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 TestNullOptions < Test::Unit::TestCase
19+
include Helper::Buildable
20+
21+
def setup
22+
@options = Arrow::NullOptions.new
23+
end
24+
25+
def test_nan_is_null_property
26+
assert do
27+
!@options.nan_is_null?
28+
end
29+
@options.nan_is_null = true
30+
assert do
31+
@options.nan_is_null?
32+
end
33+
end
34+
35+
def test_is_null_function
36+
args = [
37+
Arrow::ArrayDatum.new(build_float_array([1.0, Float::NAN, 2.0, nil, 4.0])),
38+
]
39+
is_null_function = Arrow::Function.find("is_null")
40+
@options.nan_is_null = true
41+
result = is_null_function.execute(args, @options).value
42+
assert_equal(build_boolean_array([false, true, false, true, false]), result)
43+
end
44+
end

0 commit comments

Comments
 (0)