Skip to content

Commit 83e908a

Browse files
committed
Implement getter and setters for nullability and metadata in Ruby extension
1 parent 008f20c commit 83e908a

File tree

6 files changed

+340
-93
lines changed

6 files changed

+340
-93
lines changed

c_glib/arrow-glib/compute.cpp

Lines changed: 89 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8355,6 +8355,8 @@ garrow_pairwise_options_new(void)
83558355

83568356
enum {
83578357
PROP_MAKE_STRUCT_OPTIONS_FIELD_NAMES = 1,
8358+
PROP_MAKE_STRUCT_OPTIONS_FIELD_NULLABILITY,
8359+
PROP_MAKE_STRUCT_OPTIONS_FIELD_METADATA,
83588360
};
83598361

83608362
G_DEFINE_TYPE(GArrowMakeStructOptions,
@@ -8385,6 +8387,35 @@ garrow_make_struct_options_set_property(GObject *object,
83858387
options->field_metadata.assign(options->field_names.size(), NULLPTR);
83868388
}
83878389
break;
8390+
case PROP_MAKE_STRUCT_OPTIONS_FIELD_NULLABILITY:
8391+
{
8392+
auto array = static_cast<GArray *>(g_value_get_boxed(value));
8393+
options->field_nullability.clear();
8394+
if (array) {
8395+
for (guint i = 0; i < array->len; ++i) {
8396+
auto nullability = g_array_index(array, gboolean, i);
8397+
options->field_nullability.push_back(nullability != FALSE);
8398+
}
8399+
}
8400+
}
8401+
break;
8402+
case PROP_MAKE_STRUCT_OPTIONS_FIELD_METADATA:
8403+
{
8404+
auto array = static_cast<GArray *>(g_value_get_boxed(value));
8405+
options->field_metadata.clear();
8406+
if (array) {
8407+
for (guint i = 0; i < array->len; ++i) {
8408+
auto metadata = g_array_index(array, GHashTable *, i);
8409+
if (metadata) {
8410+
options->field_metadata.push_back(
8411+
garrow_internal_hash_table_to_metadata(metadata));
8412+
} else {
8413+
options->field_metadata.push_back(nullptr);
8414+
}
8415+
}
8416+
}
8417+
}
8418+
break;
83888419
default:
83898420
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
83908421
break;
@@ -8411,6 +8442,31 @@ garrow_make_struct_options_get_property(GObject *object,
84118442
g_value_take_boxed(value, strv);
84128443
}
84138444
break;
8445+
case PROP_MAKE_STRUCT_OPTIONS_FIELD_NULLABILITY:
8446+
{
8447+
const auto &nullability = options->field_nullability;
8448+
auto array = g_array_sized_new(FALSE, FALSE, sizeof(gboolean), nullability.size());
8449+
for (gsize i = 0; i < nullability.size(); ++i) {
8450+
gboolean val = nullability[i] ? TRUE : FALSE;
8451+
g_array_append_val(array, val);
8452+
}
8453+
g_value_take_boxed(value, array);
8454+
}
8455+
break;
8456+
case PROP_MAKE_STRUCT_OPTIONS_FIELD_METADATA:
8457+
{
8458+
const auto &metadata = options->field_metadata;
8459+
auto array = g_array_sized_new(FALSE, FALSE, sizeof(GHashTable *), metadata.size());
8460+
for (gsize i = 0; i < metadata.size(); ++i) {
8461+
GHashTable *hash_table = nullptr;
8462+
if (metadata[i]) {
8463+
hash_table = garrow_internal_hash_table_from_metadata(metadata[i]);
8464+
}
8465+
g_array_append_val(array, hash_table);
8466+
}
8467+
g_value_take_boxed(value, array);
8468+
}
8469+
break;
84148470
default:
84158471
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
84168472
break;
@@ -8451,6 +8507,39 @@ garrow_make_struct_options_class_init(GArrowMakeStructOptionsClass *klass)
84518507
g_object_class_install_property(gobject_class,
84528508
PROP_MAKE_STRUCT_OPTIONS_FIELD_NAMES,
84538509
spec);
8510+
8511+
/**
8512+
* GArrowMakeStructOptions:field-nullability:
8513+
*
8514+
* Nullability for each field. This is a #GArray of #gboolean values.
8515+
*
8516+
* Since: 23.0.0
8517+
*/
8518+
spec = g_param_spec_boxed("field-nullability",
8519+
"Field nullability",
8520+
"Nullability for each field",
8521+
G_TYPE_ARRAY,
8522+
static_cast<GParamFlags>(G_PARAM_READWRITE));
8523+
g_object_class_install_property(gobject_class,
8524+
PROP_MAKE_STRUCT_OPTIONS_FIELD_NULLABILITY,
8525+
spec);
8526+
8527+
/**
8528+
* GArrowMakeStructOptions:field-metadata:
8529+
*
8530+
* Metadata for each field. This is a #GArray of #GHashTable pointers
8531+
* (element-type utf8 utf8), or %NULL for fields without metadata.
8532+
*
8533+
* Since: 23.0.0
8534+
*/
8535+
spec = g_param_spec_boxed("field-metadata",
8536+
"Field metadata",
8537+
"Metadata for each field",
8538+
G_TYPE_ARRAY,
8539+
static_cast<GParamFlags>(G_PARAM_READWRITE));
8540+
g_object_class_install_property(gobject_class,
8541+
PROP_MAKE_STRUCT_OPTIONS_FIELD_METADATA,
8542+
spec);
84548543
}
84558544

84568545
/**
@@ -8467,37 +8556,6 @@ garrow_make_struct_options_new(void)
84678556
return GARROW_MAKE_STRUCT_OPTIONS(options);
84688557
}
84698558

8470-
/**
8471-
* garrow_make_struct_options_add_field:
8472-
* @options: A #GArrowMakeStructOptions.
8473-
* @name: The name of the field to add.
8474-
* @nullability: Whether the field is nullable.
8475-
* @metadata: (nullable) (element-type utf8 utf8): A #GHashTable for the field's
8476-
* metadata, or %NULL.
8477-
*
8478-
* Adds a field to the struct options with the specified name, nullability,
8479-
* and optional metadata.
8480-
*
8481-
* Since: 23.0.0
8482-
*/
8483-
void
8484-
garrow_make_struct_options_add_field(GArrowMakeStructOptions *options,
8485-
const char *name,
8486-
gboolean nullability,
8487-
GHashTable *metadata)
8488-
{
8489-
auto arrow_options = static_cast<arrow::compute::MakeStructOptions *>(
8490-
garrow_function_options_get_raw(GARROW_FUNCTION_OPTIONS(options)));
8491-
arrow_options->field_names.emplace_back(name);
8492-
arrow_options->field_nullability.push_back(nullability != FALSE);
8493-
if (metadata) {
8494-
arrow_options->field_metadata.push_back(
8495-
garrow_internal_hash_table_to_metadata(metadata));
8496-
} else {
8497-
arrow_options->field_metadata.push_back(nullptr);
8498-
}
8499-
}
8500-
85018559
G_END_DECLS
85028560

85038561
arrow::Result<arrow::FieldRef>

c_glib/arrow-glib/compute.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1491,11 +1491,4 @@ GARROW_AVAILABLE_IN_23_0
14911491
GArrowMakeStructOptions *
14921492
garrow_make_struct_options_new(void);
14931493

1494-
GARROW_AVAILABLE_IN_23_0
1495-
void
1496-
garrow_make_struct_options_add_field(GArrowMakeStructOptions *options,
1497-
const char *name,
1498-
gboolean nullability,
1499-
GHashTable *metadata);
1500-
15011494
G_END_DECLS

c_glib/test/test-make-struct-options.rb

Lines changed: 1 addition & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,14 @@ def test_field_names_property
2828
assert_equal(["a", "b", "c"], @options.field_names)
2929
end
3030

31-
def test_add_field
32-
@options.add_field("a", true, nil)
33-
@options.add_field("b", false, nil)
34-
metadata = {"key1" => "value1", "key2" => "value2"}
35-
@options.add_field("c", true, metadata)
36-
assert_equal(["a", "b", "c"], @options.field_names)
37-
end
38-
3931
def test_make_struct_function
4032
a = build_int8_array([1, 2, 3])
4133
b = build_boolean_array([true, false, nil])
4234
args = [
4335
Arrow::ArrayDatum.new(a),
4436
Arrow::ArrayDatum.new(b),
4537
]
46-
@options.add_field("a", true, nil)
47-
@options.add_field("b", true, nil)
38+
@options.field_names = ["a", "b"]
4839
make_struct_function = Arrow::Function.find("make_struct")
4940
result = make_struct_function.execute(args, @options).value
5041

@@ -61,49 +52,4 @@ def test_make_struct_function
6152
)
6253
assert_equal(expected, result)
6354
end
64-
65-
def test_make_struct_function_with_nullability
66-
a = build_int8_array([1, 2, 3])
67-
b = build_boolean_array([true, false, nil])
68-
args = [
69-
Arrow::ArrayDatum.new(a),
70-
Arrow::ArrayDatum.new(b),
71-
]
72-
@options.add_field("a", false, nil)
73-
@options.add_field("b", true, nil)
74-
make_struct_function = Arrow::Function.find("make_struct")
75-
result = make_struct_function.execute(args, @options).value
76-
77-
expected = build_struct_array(
78-
[
79-
Arrow::Field.new("a", Arrow::Int8DataType.new, false),
80-
Arrow::Field.new("b", Arrow::BooleanDataType.new, true),
81-
],
82-
[
83-
{"a" => 1, "b" => true},
84-
{"a" => 2, "b" => false},
85-
{"a" => 3, "b" => nil},
86-
]
87-
)
88-
assert_equal(expected, result)
89-
end
90-
91-
def test_make_struct_function_with_metadata
92-
a = build_int8_array([1, 2, 3])
93-
b = build_boolean_array([true, false, nil])
94-
args = [
95-
Arrow::ArrayDatum.new(a),
96-
Arrow::ArrayDatum.new(b),
97-
]
98-
metadata1 = {"key1" => "value1"}
99-
metadata2 = {"key2" => "value2"}
100-
@options.add_field("a", true, metadata1)
101-
@options.add_field("b", true, metadata2)
102-
make_struct_function = Arrow::Function.find("make_struct")
103-
result = make_struct_function.execute(args, @options).value
104-
105-
fields = result.value_data_type.fields
106-
assert_equal(metadata1, fields[0].metadata)
107-
assert_equal(metadata2, fields[1].metadata)
108-
end
10955
end

0 commit comments

Comments
 (0)