Skip to content

Commit c342ac5

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

File tree

4 files changed

+196
-0
lines changed

4 files changed

+196
-0
lines changed

c_glib/arrow-glib/compute.cpp

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,9 @@ G_BEGIN_DECLS
276276
* #GArrowJoinOptions is a class to customize the `binary_join_element_wise`
277277
* function.
278278
*
279+
* #GArrowListFlattenOptions is a class to customize the `list_flatten`
280+
* function.
281+
*
279282
* There are many functions to compute data on an array.
280283
*/
281284

@@ -7400,6 +7403,101 @@ garrow_join_options_new(void)
74007403
return GARROW_JOIN_OPTIONS(options);
74017404
}
74027405

7406+
enum {
7407+
PROP_LIST_FLATTEN_OPTIONS_RECURSIVE = 1,
7408+
};
7409+
7410+
G_DEFINE_TYPE(GArrowListFlattenOptions,
7411+
garrow_list_flatten_options,
7412+
GARROW_TYPE_FUNCTION_OPTIONS)
7413+
7414+
static void
7415+
garrow_list_flatten_options_set_property(GObject *object,
7416+
guint prop_id,
7417+
const GValue *value,
7418+
GParamSpec *pspec)
7419+
{
7420+
auto options = garrow_list_flatten_options_get_raw(GARROW_LIST_FLATTEN_OPTIONS(object));
7421+
7422+
switch (prop_id) {
7423+
case PROP_LIST_FLATTEN_OPTIONS_RECURSIVE:
7424+
options->recursive = g_value_get_boolean(value);
7425+
break;
7426+
default:
7427+
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
7428+
break;
7429+
}
7430+
}
7431+
7432+
static void
7433+
garrow_list_flatten_options_get_property(GObject *object,
7434+
guint prop_id,
7435+
GValue *value,
7436+
GParamSpec *pspec)
7437+
{
7438+
auto options = garrow_list_flatten_options_get_raw(GARROW_LIST_FLATTEN_OPTIONS(object));
7439+
7440+
switch (prop_id) {
7441+
case PROP_LIST_FLATTEN_OPTIONS_RECURSIVE:
7442+
g_value_set_boolean(value, options->recursive);
7443+
break;
7444+
default:
7445+
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
7446+
break;
7447+
}
7448+
}
7449+
7450+
static void
7451+
garrow_list_flatten_options_init(GArrowListFlattenOptions *object)
7452+
{
7453+
auto priv = GARROW_FUNCTION_OPTIONS_GET_PRIVATE(object);
7454+
priv->options = static_cast<arrow::compute::FunctionOptions *>(
7455+
new arrow::compute::ListFlattenOptions());
7456+
}
7457+
7458+
static void
7459+
garrow_list_flatten_options_class_init(GArrowListFlattenOptionsClass *klass)
7460+
{
7461+
auto gobject_class = G_OBJECT_CLASS(klass);
7462+
7463+
gobject_class->set_property = garrow_list_flatten_options_set_property;
7464+
gobject_class->get_property = garrow_list_flatten_options_get_property;
7465+
7466+
arrow::compute::ListFlattenOptions options;
7467+
7468+
GParamSpec *spec;
7469+
/**
7470+
* GArrowListFlattenOptions:recursive:
7471+
*
7472+
* If true, the list is flattened recursively until a non-list array is formed.
7473+
*
7474+
* Since: 23.0.0
7475+
*/
7476+
spec = g_param_spec_boolean(
7477+
"recursive",
7478+
"Recursive",
7479+
"If true, the list is flattened recursively until a non-list array is formed",
7480+
options.recursive,
7481+
static_cast<GParamFlags>(G_PARAM_READWRITE));
7482+
g_object_class_install_property(gobject_class,
7483+
PROP_LIST_FLATTEN_OPTIONS_RECURSIVE,
7484+
spec);
7485+
}
7486+
7487+
/**
7488+
* garrow_list_flatten_options_new:
7489+
*
7490+
* Returns: A newly created #GArrowListFlattenOptions.
7491+
*
7492+
* Since: 23.0.0
7493+
*/
7494+
GArrowListFlattenOptions *
7495+
garrow_list_flatten_options_new(void)
7496+
{
7497+
auto options = g_object_new(GARROW_TYPE_LIST_FLATTEN_OPTIONS, NULL);
7498+
return GARROW_LIST_FLATTEN_OPTIONS(options);
7499+
}
7500+
74037501
G_END_DECLS
74047502

74057503
arrow::Result<arrow::FieldRef>
@@ -7574,6 +7672,11 @@ garrow_function_options_new_raw(const arrow::compute::FunctionOptions *arrow_opt
75747672
static_cast<const arrow::compute::JoinOptions *>(arrow_options);
75757673
auto options = garrow_join_options_new_raw(arrow_join_options);
75767674
return GARROW_FUNCTION_OPTIONS(options);
7675+
} else if (arrow_type_name == "ListFlattenOptions") {
7676+
const auto arrow_list_flatten_options =
7677+
static_cast<const arrow::compute::ListFlattenOptions *>(arrow_options);
7678+
auto options = garrow_list_flatten_options_new_raw(arrow_list_flatten_options);
7679+
return GARROW_FUNCTION_OPTIONS(options);
75777680
} else {
75787681
auto options = g_object_new(GARROW_TYPE_FUNCTION_OPTIONS, NULL);
75797682
return GARROW_FUNCTION_OPTIONS(options);
@@ -8250,3 +8353,20 @@ garrow_join_options_get_raw(GArrowJoinOptions *options)
82508353
return static_cast<arrow::compute::JoinOptions *>(
82518354
garrow_function_options_get_raw(GARROW_FUNCTION_OPTIONS(options)));
82528355
}
8356+
8357+
GArrowListFlattenOptions *
8358+
garrow_list_flatten_options_new_raw(
8359+
const arrow::compute::ListFlattenOptions *arrow_options)
8360+
{
8361+
return GARROW_LIST_FLATTEN_OPTIONS(g_object_new(GARROW_TYPE_LIST_FLATTEN_OPTIONS,
8362+
"recursive",
8363+
arrow_options->recursive,
8364+
NULL));
8365+
}
8366+
8367+
arrow::compute::ListFlattenOptions *
8368+
garrow_list_flatten_options_get_raw(GArrowListFlattenOptions *options)
8369+
{
8370+
return static_cast<arrow::compute::ListFlattenOptions *>(
8371+
garrow_function_options_get_raw(GARROW_FUNCTION_OPTIONS(options)));
8372+
}

c_glib/arrow-glib/compute.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1322,4 +1322,20 @@ GARROW_AVAILABLE_IN_23_0
13221322
GArrowJoinOptions *
13231323
garrow_join_options_new(void);
13241324

1325+
#define GARROW_TYPE_LIST_FLATTEN_OPTIONS (garrow_list_flatten_options_get_type())
1326+
GARROW_AVAILABLE_IN_23_0
1327+
G_DECLARE_DERIVABLE_TYPE(GArrowListFlattenOptions,
1328+
garrow_list_flatten_options,
1329+
GARROW,
1330+
LIST_FLATTEN_OPTIONS,
1331+
GArrowFunctionOptions)
1332+
struct _GArrowListFlattenOptionsClass
1333+
{
1334+
GArrowFunctionOptionsClass parent_class;
1335+
};
1336+
1337+
GARROW_AVAILABLE_IN_23_0
1338+
GArrowListFlattenOptions *
1339+
garrow_list_flatten_options_new(void);
1340+
13251341
G_END_DECLS

c_glib/arrow-glib/compute.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,3 +220,9 @@ GArrowJoinOptions *
220220
garrow_join_options_new_raw(const arrow::compute::JoinOptions *arrow_options);
221221
arrow::compute::JoinOptions *
222222
garrow_join_options_get_raw(GArrowJoinOptions *options);
223+
224+
GArrowListFlattenOptions *
225+
garrow_list_flatten_options_new_raw(
226+
const arrow::compute::ListFlattenOptions *arrow_options);
227+
arrow::compute::ListFlattenOptions *
228+
garrow_list_flatten_options_get_raw(GArrowListFlattenOptions *options);
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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 TestListFlattenOptions < Test::Unit::TestCase
19+
include Helper::Buildable
20+
21+
def setup
22+
@options = Arrow::ListFlattenOptions.new
23+
end
24+
25+
def test_recursive_property
26+
assert do
27+
!@options.recursive?
28+
end
29+
@options.recursive = true
30+
assert do
31+
@options.recursive?
32+
end
33+
end
34+
35+
def test_list_flatten_function_recursive
36+
list_data_type = Arrow::ListDataType.new(Arrow::Field.new("value", Arrow::Int8DataType.new))
37+
nested_list = build_list_array(list_data_type, [[[1, 2], [3]], [[4, 5]]])
38+
39+
args = [
40+
Arrow::ArrayDatum.new(nested_list),
41+
]
42+
list_flatten_function = Arrow::Function.find("list_flatten")
43+
44+
@options.recursive = false
45+
result = list_flatten_function.execute(args, @options).value
46+
assert_equal(build_list_array(Arrow::Int8DataType.new, [[1, 2], [3], [4, 5]]),
47+
result)
48+
49+
@options.recursive = true
50+
result = list_flatten_function.execute(args, @options).value
51+
assert_equal(build_int8_array([1, 2, 3, 4, 5]),
52+
result)
53+
end
54+
end

0 commit comments

Comments
 (0)