Skip to content

Commit fae65cd

Browse files
hiroyuki-satokou
andauthored
GH-46380: [GLib] Add GArrowFixedShapeDataType#shape (#46381)
### Rationale for this change The C++ API implemented `FixedShapeTensor::shape()` instance method. GLib was not yet supported. ### What changes are included in this PR? Add `GArrowFixedShapeTensorDataType#shape` instance method. ### Are these changes tested? Yes. ### Are there any user-facing changes? Yes. * GitHub Issue: #46380 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 5989387 commit fae65cd

File tree

3 files changed

+37
-15
lines changed

3 files changed

+37
-15
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2347,6 +2347,25 @@ garrow_fixed_shape_tensor_data_type_new(GArrowDataType *value_type,
23472347
return data_type;
23482348
}
23492349

2350+
/**
2351+
* garrow_fixed_shape_tensor_data_type_get_shape:
2352+
* @data_type: A #GArrowFixedShapeTensorDataType.
2353+
* @length: (out): Return location for the number of dimensions of the tensor.
2354+
*
2355+
* Returns: (array length=length): Shape of the tensor.
2356+
*/
2357+
const gint64 *
2358+
garrow_fixed_shape_tensor_data_type_get_shape(GArrowFixedShapeTensorDataType *data_type,
2359+
gsize *length)
2360+
{
2361+
auto arrow_data_type = std::static_pointer_cast<arrow::extension::FixedShapeTensorType>(
2362+
garrow_data_type_get_raw(GARROW_DATA_TYPE(data_type)));
2363+
2364+
const auto &arrow_shape = arrow_data_type->shape();
2365+
*length = arrow_shape.size();
2366+
return arrow_shape.data();
2367+
}
2368+
23502369
G_END_DECLS
23512370

23522371
GArrowDataType *

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -826,4 +826,8 @@ garrow_fixed_shape_tensor_data_type_new(GArrowDataType *value_type,
826826
gsize n_dim_names,
827827
GError **error);
828828

829+
GARROW_AVAILABLE_IN_21_0
830+
const gint64 *
831+
garrow_fixed_shape_tensor_data_type_get_shape(GArrowFixedShapeTensorDataType *data_type,
832+
gsize *length);
829833
G_END_DECLS

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

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,29 @@
1616
# under the License.
1717

1818
class TestFixedShapeTensorDataType < Test::Unit::TestCase
19+
def setup
20+
@data_type = Arrow::FixedShapeTensorDataType.new(Arrow::UInt64DataType.new,
21+
[3, 4],
22+
[1, 0],
23+
["x", "y"])
24+
end
25+
1926
def test_id
20-
data_type = Arrow::FixedShapeTensorDataType.new(Arrow::UInt64DataType.new,
21-
[3, 4],
22-
[1, 0],
23-
["x", "y"])
24-
assert_equal(Arrow::Type::EXTENSION, data_type.id)
27+
assert_equal(Arrow::Type::EXTENSION, @data_type.id)
2528
end
2629

2730
def test_name
28-
data_type = Arrow::FixedShapeTensorDataType.new(Arrow::UInt64DataType.new,
29-
[3, 4],
30-
[1, 0],
31-
["x", "y"])
3231
assert_equal(["extension", "arrow.fixed_shape_tensor"],
33-
[data_type.name, data_type.extension_name])
32+
[@data_type.name, @data_type.extension_name])
33+
end
34+
35+
def test_shape
36+
assert_equal([3, 4], @data_type.shape)
3437
end
3538

3639
def test_to_s
37-
data_type = Arrow::FixedShapeTensorDataType.new(Arrow::UInt64DataType.new,
38-
[3, 4],
39-
[1, 0],
40-
["x", "y"])
4140
assert do
42-
data_type.to_s.start_with?("extension<arrow.fixed_shape_tensor")
41+
@data_type.to_s.start_with?("extension<arrow.fixed_shape_tensor")
4342
end
4443
end
4544

0 commit comments

Comments
 (0)