Skip to content

Commit d0a3efc

Browse files
committed
Add GArrowMakeStructOptions
1 parent d89c14b commit d0a3efc

File tree

4 files changed

+217
-0
lines changed

4 files changed

+217
-0
lines changed

c_glib/arrow-glib/compute.cpp

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,9 @@ G_BEGIN_DECLS
279279
* #GArrowListFlattenOptions is a class to customize the `list_flatten`
280280
* function.
281281
*
282+
* #GArrowMakeStructOptions is a class to customize the `make_struct`
283+
* function.
284+
*
282285
* There are many functions to compute data on an array.
283286
*/
284287

@@ -7498,6 +7501,120 @@ garrow_list_flatten_options_new(void)
74987501
return GARROW_LIST_FLATTEN_OPTIONS(options);
74997502
}
75007503

7504+
enum {
7505+
PROP_MAKE_STRUCT_OPTIONS_FIELD_NAMES = 1,
7506+
};
7507+
7508+
G_DEFINE_TYPE(GArrowMakeStructOptions,
7509+
garrow_make_struct_options,
7510+
GARROW_TYPE_FUNCTION_OPTIONS)
7511+
7512+
static void
7513+
garrow_make_struct_options_set_property(GObject *object,
7514+
guint prop_id,
7515+
const GValue *value,
7516+
GParamSpec *pspec)
7517+
{
7518+
auto options = static_cast<arrow::compute::MakeStructOptions *>(
7519+
garrow_function_options_get_raw(GARROW_FUNCTION_OPTIONS(object)));
7520+
7521+
switch (prop_id) {
7522+
case PROP_MAKE_STRUCT_OPTIONS_FIELD_NAMES:
7523+
{
7524+
auto strv = static_cast<gchar **>(g_value_get_boxed(value));
7525+
options->field_names.clear();
7526+
if (strv) {
7527+
for (gchar **p = strv; *p; ++p) {
7528+
options->field_names.emplace_back(*p);
7529+
}
7530+
}
7531+
// Keep nullability and metadata vectors in sync with names.
7532+
options->field_nullability.assign(options->field_names.size(), true);
7533+
options->field_metadata.assign(options->field_names.size(), NULLPTR);
7534+
}
7535+
break;
7536+
default:
7537+
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
7538+
break;
7539+
}
7540+
}
7541+
7542+
static void
7543+
garrow_make_struct_options_get_property(GObject *object,
7544+
guint prop_id,
7545+
GValue *value,
7546+
GParamSpec *pspec)
7547+
{
7548+
auto options = static_cast<arrow::compute::MakeStructOptions *>(
7549+
garrow_function_options_get_raw(GARROW_FUNCTION_OPTIONS(object)));
7550+
7551+
switch (prop_id) {
7552+
case PROP_MAKE_STRUCT_OPTIONS_FIELD_NAMES:
7553+
{
7554+
const auto &names = options->field_names;
7555+
auto strv = static_cast<gchar **>(g_new0(gchar *, names.size() + 1));
7556+
for (gsize i = 0; i < names.size(); ++i) {
7557+
strv[i] = g_strdup(names[i].c_str());
7558+
}
7559+
g_value_take_boxed(value, strv);
7560+
}
7561+
break;
7562+
default:
7563+
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
7564+
break;
7565+
}
7566+
}
7567+
7568+
static void
7569+
garrow_make_struct_options_init(GArrowMakeStructOptions *object)
7570+
{
7571+
auto priv = GARROW_FUNCTION_OPTIONS_GET_PRIVATE(object);
7572+
priv->options = static_cast<arrow::compute::FunctionOptions *>(
7573+
new arrow::compute::MakeStructOptions());
7574+
}
7575+
7576+
static void
7577+
garrow_make_struct_options_class_init(GArrowMakeStructOptionsClass *klass)
7578+
{
7579+
auto gobject_class = G_OBJECT_CLASS(klass);
7580+
7581+
gobject_class->set_property = garrow_make_struct_options_set_property;
7582+
gobject_class->get_property = garrow_make_struct_options_get_property;
7583+
7584+
arrow::compute::MakeStructOptions options;
7585+
7586+
GParamSpec *spec;
7587+
/**
7588+
* GArrowMakeStructOptions:field-names:
7589+
*
7590+
* Names for wrapped columns.
7591+
*
7592+
* Since: 23.0.0
7593+
*/
7594+
spec = g_param_spec_boxed("field-names",
7595+
"Field names",
7596+
"Names for wrapped columns",
7597+
G_TYPE_STRV,
7598+
static_cast<GParamFlags>(G_PARAM_READWRITE));
7599+
g_object_class_install_property(gobject_class,
7600+
PROP_MAKE_STRUCT_OPTIONS_FIELD_NAMES,
7601+
spec);
7602+
}
7603+
7604+
/**
7605+
* garrow_make_struct_options_new:
7606+
*
7607+
* Returns: A newly created #GArrowMakeStructOptions.
7608+
*
7609+
* Since: 23.0.0
7610+
*/
7611+
GArrowMakeStructOptions *
7612+
garrow_make_struct_options_new(void)
7613+
{
7614+
auto options = g_object_new(GARROW_TYPE_MAKE_STRUCT_OPTIONS, NULL);
7615+
return GARROW_MAKE_STRUCT_OPTIONS(options);
7616+
}
7617+
75017618
G_END_DECLS
75027619

75037620
arrow::Result<arrow::FieldRef>
@@ -7677,6 +7794,11 @@ garrow_function_options_new_raw(const arrow::compute::FunctionOptions *arrow_opt
76777794
static_cast<const arrow::compute::ListFlattenOptions *>(arrow_options);
76787795
auto options = garrow_list_flatten_options_new_raw(arrow_list_flatten_options);
76797796
return GARROW_FUNCTION_OPTIONS(options);
7797+
} else if (arrow_type_name == "MakeStructOptions") {
7798+
const auto arrow_make_struct_options =
7799+
static_cast<const arrow::compute::MakeStructOptions *>(arrow_options);
7800+
auto options = garrow_make_struct_options_new_raw(arrow_make_struct_options);
7801+
return GARROW_FUNCTION_OPTIONS(options);
76807802
} else {
76817803
auto options = g_object_new(GARROW_TYPE_FUNCTION_OPTIONS, NULL);
76827804
return GARROW_FUNCTION_OPTIONS(options);
@@ -8370,3 +8492,21 @@ garrow_list_flatten_options_get_raw(GArrowListFlattenOptions *options)
83708492
return static_cast<arrow::compute::ListFlattenOptions *>(
83718493
garrow_function_options_get_raw(GARROW_FUNCTION_OPTIONS(options)));
83728494
}
8495+
8496+
GArrowMakeStructOptions *
8497+
garrow_make_struct_options_new_raw(const arrow::compute::MakeStructOptions *arrow_options)
8498+
{
8499+
auto options =
8500+
GARROW_MAKE_STRUCT_OPTIONS(g_object_new(GARROW_TYPE_MAKE_STRUCT_OPTIONS, NULL));
8501+
auto arrow_new_options = static_cast<arrow::compute::MakeStructOptions *>(
8502+
garrow_function_options_get_raw(GARROW_FUNCTION_OPTIONS(options)));
8503+
*arrow_new_options = *arrow_options;
8504+
return options;
8505+
}
8506+
8507+
arrow::compute::MakeStructOptions *
8508+
garrow_make_struct_options_get_raw(GArrowMakeStructOptions *options)
8509+
{
8510+
return static_cast<arrow::compute::MakeStructOptions *>(
8511+
garrow_function_options_get_raw(GARROW_FUNCTION_OPTIONS(options)));
8512+
}

c_glib/arrow-glib/compute.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1338,4 +1338,20 @@ GARROW_AVAILABLE_IN_23_0
13381338
GArrowListFlattenOptions *
13391339
garrow_list_flatten_options_new(void);
13401340

1341+
#define GARROW_TYPE_MAKE_STRUCT_OPTIONS (garrow_make_struct_options_get_type())
1342+
GARROW_AVAILABLE_IN_23_0
1343+
G_DECLARE_DERIVABLE_TYPE(GArrowMakeStructOptions,
1344+
garrow_make_struct_options,
1345+
GARROW,
1346+
MAKE_STRUCT_OPTIONS,
1347+
GArrowFunctionOptions)
1348+
struct _GArrowMakeStructOptionsClass
1349+
{
1350+
GArrowFunctionOptionsClass parent_class;
1351+
};
1352+
1353+
GARROW_AVAILABLE_IN_23_0
1354+
GArrowMakeStructOptions *
1355+
garrow_make_struct_options_new(void);
1356+
13411357
G_END_DECLS

c_glib/arrow-glib/compute.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,3 +226,9 @@ garrow_list_flatten_options_new_raw(
226226
const arrow::compute::ListFlattenOptions *arrow_options);
227227
arrow::compute::ListFlattenOptions *
228228
garrow_list_flatten_options_get_raw(GArrowListFlattenOptions *options);
229+
230+
GArrowMakeStructOptions *
231+
garrow_make_struct_options_new_raw(
232+
const arrow::compute::MakeStructOptions *arrow_options);
233+
arrow::compute::MakeStructOptions *
234+
garrow_make_struct_options_get_raw(GArrowMakeStructOptions *options);
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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 TestMakeStructOptions < Test::Unit::TestCase
19+
include Helper::Buildable
20+
21+
def setup
22+
@options = Arrow::MakeStructOptions.new
23+
end
24+
25+
def test_field_names_property
26+
assert_equal([], @options.field_names)
27+
@options.field_names = ["a", "b", "c"]
28+
assert_equal(["a", "b", "c"], @options.field_names)
29+
end
30+
31+
def test_make_struct_function
32+
a = build_int8_array([1, 2, 3])
33+
b = build_boolean_array([true, false, nil])
34+
args = [
35+
Arrow::ArrayDatum.new(a),
36+
Arrow::ArrayDatum.new(b),
37+
]
38+
@options.field_names = ["a", "b"]
39+
make_struct_function = Arrow::Function.find("make_struct")
40+
result = make_struct_function.execute(args, @options).value
41+
42+
expected = build_struct_array(
43+
[
44+
Arrow::Field.new("a", Arrow::Int8DataType.new),
45+
Arrow::Field.new("b", Arrow::BooleanDataType.new),
46+
],
47+
[
48+
{"a" => 1, "b" => true},
49+
{"a" => 2, "b" => false},
50+
{"a" => 3, "b" => nil},
51+
]
52+
)
53+
assert_equal(expected, result)
54+
end
55+
end

0 commit comments

Comments
 (0)