Skip to content

Commit 85e630c

Browse files
committed
Add GArrowMakeStructOptions
1 parent 4127ca2 commit 85e630c

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
@@ -269,6 +269,9 @@ G_BEGIN_DECLS
269269
* #GArrowExtractRegexOptions is a class to customize the `extract_regex`
270270
* function.
271271
*
272+
* #GArrowMakeStructOptions is a class to customize the `make_struct`
273+
* function.
274+
*
272275
* There are many functions to compute data on an array.
273276
*/
274277

@@ -7091,6 +7094,120 @@ garrow_extract_regex_options_new(void)
70917094
return GARROW_EXTRACT_REGEX_OPTIONS(options);
70927095
}
70937096

7097+
enum {
7098+
PROP_MAKE_STRUCT_OPTIONS_FIELD_NAMES = 1,
7099+
};
7100+
7101+
G_DEFINE_TYPE(GArrowMakeStructOptions,
7102+
garrow_make_struct_options,
7103+
GARROW_TYPE_FUNCTION_OPTIONS)
7104+
7105+
static void
7106+
garrow_make_struct_options_set_property(GObject *object,
7107+
guint prop_id,
7108+
const GValue *value,
7109+
GParamSpec *pspec)
7110+
{
7111+
auto options = static_cast<arrow::compute::MakeStructOptions *>(
7112+
garrow_function_options_get_raw(GARROW_FUNCTION_OPTIONS(object)));
7113+
7114+
switch (prop_id) {
7115+
case PROP_MAKE_STRUCT_OPTIONS_FIELD_NAMES:
7116+
{
7117+
auto strv = static_cast<gchar **>(g_value_get_boxed(value));
7118+
options->field_names.clear();
7119+
if (strv) {
7120+
for (gchar **p = strv; *p; ++p) {
7121+
options->field_names.emplace_back(*p);
7122+
}
7123+
}
7124+
// Keep nullability and metadata vectors in sync with names.
7125+
options->field_nullability.assign(options->field_names.size(), true);
7126+
options->field_metadata.assign(options->field_names.size(), NULLPTR);
7127+
}
7128+
break;
7129+
default:
7130+
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
7131+
break;
7132+
}
7133+
}
7134+
7135+
static void
7136+
garrow_make_struct_options_get_property(GObject *object,
7137+
guint prop_id,
7138+
GValue *value,
7139+
GParamSpec *pspec)
7140+
{
7141+
auto options = static_cast<arrow::compute::MakeStructOptions *>(
7142+
garrow_function_options_get_raw(GARROW_FUNCTION_OPTIONS(object)));
7143+
7144+
switch (prop_id) {
7145+
case PROP_MAKE_STRUCT_OPTIONS_FIELD_NAMES:
7146+
{
7147+
const auto &names = options->field_names;
7148+
auto strv = static_cast<gchar **>(g_new0(gchar *, names.size() + 1));
7149+
for (gsize i = 0; i < names.size(); ++i) {
7150+
strv[i] = g_strdup(names[i].c_str());
7151+
}
7152+
g_value_take_boxed(value, strv);
7153+
}
7154+
break;
7155+
default:
7156+
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
7157+
break;
7158+
}
7159+
}
7160+
7161+
static void
7162+
garrow_make_struct_options_init(GArrowMakeStructOptions *object)
7163+
{
7164+
auto priv = GARROW_FUNCTION_OPTIONS_GET_PRIVATE(object);
7165+
priv->options = static_cast<arrow::compute::FunctionOptions *>(
7166+
new arrow::compute::MakeStructOptions());
7167+
}
7168+
7169+
static void
7170+
garrow_make_struct_options_class_init(GArrowMakeStructOptionsClass *klass)
7171+
{
7172+
auto gobject_class = G_OBJECT_CLASS(klass);
7173+
7174+
gobject_class->set_property = garrow_make_struct_options_set_property;
7175+
gobject_class->get_property = garrow_make_struct_options_get_property;
7176+
7177+
arrow::compute::MakeStructOptions options;
7178+
7179+
GParamSpec *spec;
7180+
/**
7181+
* GArrowMakeStructOptions:field-names:
7182+
*
7183+
* Names for wrapped columns.
7184+
*
7185+
* Since: 23.0.0
7186+
*/
7187+
spec = g_param_spec_boxed("field-names",
7188+
"Field names",
7189+
"Names for wrapped columns",
7190+
G_TYPE_STRV,
7191+
static_cast<GParamFlags>(G_PARAM_READWRITE));
7192+
g_object_class_install_property(gobject_class,
7193+
PROP_MAKE_STRUCT_OPTIONS_FIELD_NAMES,
7194+
spec);
7195+
}
7196+
7197+
/**
7198+
* garrow_make_struct_options_new:
7199+
*
7200+
* Returns: A newly created #GArrowMakeStructOptions.
7201+
*
7202+
* Since: 23.0.0
7203+
*/
7204+
GArrowMakeStructOptions *
7205+
garrow_make_struct_options_new(void)
7206+
{
7207+
auto options = g_object_new(GARROW_TYPE_MAKE_STRUCT_OPTIONS, NULL);
7208+
return GARROW_MAKE_STRUCT_OPTIONS(options);
7209+
}
7210+
70947211
G_END_DECLS
70957212

70967213
arrow::Result<arrow::FieldRef>
@@ -7254,6 +7371,11 @@ garrow_function_options_new_raw(const arrow::compute::FunctionOptions *arrow_opt
72547371
static_cast<const arrow::compute::ExtractRegexOptions *>(arrow_options);
72557372
auto options = garrow_extract_regex_options_new_raw(arrow_extract_regex_options);
72567373
return GARROW_FUNCTION_OPTIONS(options);
7374+
} else if (arrow_type_name == "MakeStructOptions") {
7375+
const auto arrow_make_struct_options =
7376+
static_cast<const arrow::compute::MakeStructOptions *>(arrow_options);
7377+
auto options = garrow_make_struct_options_new_raw(arrow_make_struct_options);
7378+
return GARROW_FUNCTION_OPTIONS(options);
72577379
} else {
72587380
auto options = g_object_new(GARROW_TYPE_FUNCTION_OPTIONS, NULL);
72597381
return GARROW_FUNCTION_OPTIONS(options);
@@ -7893,3 +8015,21 @@ garrow_extract_regex_options_get_raw(GArrowExtractRegexOptions *options)
78938015
return static_cast<arrow::compute::ExtractRegexOptions *>(
78948016
garrow_function_options_get_raw(GARROW_FUNCTION_OPTIONS(options)));
78958017
}
8018+
8019+
GArrowMakeStructOptions *
8020+
garrow_make_struct_options_new_raw(const arrow::compute::MakeStructOptions *arrow_options)
8021+
{
8022+
auto options =
8023+
GARROW_MAKE_STRUCT_OPTIONS(g_object_new(GARROW_TYPE_MAKE_STRUCT_OPTIONS, NULL));
8024+
auto arrow_new_options = static_cast<arrow::compute::MakeStructOptions *>(
8025+
garrow_function_options_get_raw(GARROW_FUNCTION_OPTIONS(options)));
8026+
*arrow_new_options = *arrow_options;
8027+
return options;
8028+
}
8029+
8030+
arrow::compute::MakeStructOptions *
8031+
garrow_make_struct_options_get_raw(GArrowMakeStructOptions *options)
8032+
{
8033+
return static_cast<arrow::compute::MakeStructOptions *>(
8034+
garrow_function_options_get_raw(GARROW_FUNCTION_OPTIONS(options)));
8035+
}

c_glib/arrow-glib/compute.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1272,4 +1272,20 @@ GARROW_AVAILABLE_IN_23_0
12721272
GArrowExtractRegexOptions *
12731273
garrow_extract_regex_options_new(void);
12741274

1275+
#define GARROW_TYPE_MAKE_STRUCT_OPTIONS (garrow_make_struct_options_get_type())
1276+
GARROW_AVAILABLE_IN_23_0
1277+
G_DECLARE_DERIVABLE_TYPE(GArrowMakeStructOptions,
1278+
garrow_make_struct_options,
1279+
GARROW,
1280+
MAKE_STRUCT_OPTIONS,
1281+
GArrowFunctionOptions)
1282+
struct _GArrowMakeStructOptionsClass
1283+
{
1284+
GArrowFunctionOptionsClass parent_class;
1285+
};
1286+
1287+
GARROW_AVAILABLE_IN_23_0
1288+
GArrowMakeStructOptions *
1289+
garrow_make_struct_options_new(void);
1290+
12751291
G_END_DECLS

c_glib/arrow-glib/compute.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,3 +209,9 @@ garrow_extract_regex_options_new_raw(
209209
const arrow::compute::ExtractRegexOptions *arrow_options);
210210
arrow::compute::ExtractRegexOptions *
211211
garrow_extract_regex_options_get_raw(GArrowExtractRegexOptions *options);
212+
213+
GArrowMakeStructOptions *
214+
garrow_make_struct_options_new_raw(
215+
const arrow::compute::MakeStructOptions *arrow_options);
216+
arrow::compute::MakeStructOptions *
217+
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)