Skip to content

Commit 83f66d4

Browse files
hiroyuki-satokou
andauthored
GH-46433: [GLib] Add GArrowFixedShapeDataType#dim_names (#46434)
### Rationale for this change The C++ API implemented `FixedShapeTensor::dim_names()` instance method. GLib was not yet supported. ### What changes are included in this PR? Add `GArrowFixedShapeDataType#dim_names` instance method. ### Are these changes tested? Yes. ### Are there any user-facing changes? Yes. * GitHub Issue: #46433 Lead-authored-by: Hiroyuki Sato <hiroysato@gmail.com> Co-authored-by: Sutou Kouhei <kou@cozmixng.org> Signed-off-by: Sutou Kouhei <kou@clear-code.com>
1 parent dd94c90 commit 83f66d4

3 files changed

Lines changed: 36 additions & 2 deletions

File tree

c_glib/arrow-glib/basic-data-type.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2384,6 +2384,32 @@ garrow_fixed_shape_tensor_data_type_get_permutation(
23842384
*length = arrow_permutation.size();
23852385
return arrow_permutation.data();
23862386
}
2387+
2388+
/**
2389+
* garrow_fixed_shape_tensor_data_type_get_dim_names:
2390+
* @data_type: A #GArrowFixedShapeTensorDataType.
2391+
*
2392+
* Returns: (array zero-terminated=1) (element-type utf8) (transfer full):
2393+
* Dimention names of the tensor.
2394+
*
2395+
* It's a %NULL-terminated string array. It must be freed with
2396+
* g_strfreev() when no longer needed.
2397+
*/
2398+
gchar **
2399+
garrow_fixed_shape_tensor_data_type_get_dim_names(
2400+
GArrowFixedShapeTensorDataType *data_type)
2401+
{
2402+
auto arrow_data_type = std::static_pointer_cast<arrow::extension::FixedShapeTensorType>(
2403+
garrow_data_type_get_raw(GARROW_DATA_TYPE(data_type)));
2404+
const auto &arrow_dim_names = arrow_data_type->dim_names();
2405+
auto n = arrow_dim_names.size();
2406+
auto dim_names = g_new(gchar *, n + 1);
2407+
for (size_t i = 0; i < n; ++i) {
2408+
dim_names[i] = g_strndup(arrow_dim_names[i].data(), arrow_dim_names[i].size());
2409+
}
2410+
dim_names[n] = nullptr;
2411+
return dim_names;
2412+
}
23872413
G_END_DECLS
23882414

23892415
GArrowDataType *

c_glib/arrow-glib/basic-data-type.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -835,4 +835,9 @@ GARROW_AVAILABLE_IN_21_0
835835
const gint64 *
836836
garrow_fixed_shape_tensor_data_type_get_permutation(
837837
GArrowFixedShapeTensorDataType *data_type, gsize *length);
838+
839+
GARROW_AVAILABLE_IN_21_0
840+
gchar **
841+
garrow_fixed_shape_tensor_data_type_get_dim_names(
842+
GArrowFixedShapeTensorDataType *data_type);
838843
G_END_DECLS

c_glib/test/test-fixed-shape-tensor-data-type.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ def test_permutation
4040
assert_equal([1, 0], @data_type.permutation)
4141
end
4242

43+
def test_dim_names
44+
assert_equal(["x", "y"], @data_type.dim_names)
45+
end
46+
4347
def test_to_s
4448
assert do
4549
@data_type.to_s.start_with?("extension<arrow.fixed_shape_tensor")
@@ -59,8 +63,7 @@ def test_nil_dim_names
5963
[3, 4],
6064
[0, 1],
6165
nil)
62-
# TODO: Use Arrow::FixedShapeTensorDataType#dim_names
63-
assert_equal(Arrow::Type::EXTENSION, data_type.id)
66+
assert_equal([], data_type.dim_names)
6467
end
6568

6669
def test_mismatch_permutation_size

0 commit comments

Comments
 (0)