Skip to content

Commit afacd6d

Browse files
committed
GH-49956: [GLib] Add fallback data type for unknown extension data type
Users can define any extension data type. So we may encounter an unknown extension type. We can't use `GArrowExtensionDataType` for an unknown extension data type because `GArrowExtensionDataType` is an abstract class. This adds `GArrowUnknownExtensionDataType` and uses it for all unknown extension data types.
1 parent a3f3d8b commit afacd6d

4 files changed

Lines changed: 56 additions & 7 deletions

File tree

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

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ G_BEGIN_DECLS
131131
* #GArrowExtensionDataType is a base class for user-defined extension
132132
* data types.
133133
*
134+
* #GArrowUnknownExtensionDataType is a class for unknown extension
135+
* data types.
136+
*
134137
* #GArrowExtensionDataTypeRegistry is a class to manage extension
135138
* data types.
136139
*
@@ -2080,6 +2083,20 @@ namespace garrow {
20802083

20812084
G_BEGIN_DECLS
20822085

2086+
G_DEFINE_TYPE(GArrowUnknownExtensionDataType,
2087+
garrow_unknown_extension_data_type,
2088+
GARROW_TYPE_EXTENSION_DATA_TYPE)
2089+
2090+
static void
2091+
garrow_unknown_extension_data_type_init(GArrowUnknownExtensionDataType *object)
2092+
{
2093+
}
2094+
2095+
static void
2096+
garrow_unknown_extension_data_type_class_init(GArrowUnknownExtensionDataTypeClass *klass)
2097+
{
2098+
}
2099+
20832100
typedef struct GArrowExtensionDataTypeRegistryPrivate_
20842101
{
20852102
std::shared_ptr<arrow::ExtensionTypeRegistry> registry;
@@ -2720,16 +2737,25 @@ garrow_data_type_new_raw(std::shared_ptr<arrow::DataType> *arrow_data_type)
27202737
type = GARROW_TYPE_DURATION_DATA_TYPE;
27212738
break;
27222739
case arrow::Type::type::EXTENSION:
2740+
type = GARROW_TYPE_UNKNOWN_EXTENSION_DATA_TYPE;
27232741
{
2724-
auto g_extension_data_type =
2725-
std::static_pointer_cast<garrow::GExtensionType>(*arrow_data_type);
2726-
if (g_extension_data_type) {
2727-
auto garrow_data_type = g_extension_data_type->garrow_data_type();
2728-
g_object_ref(garrow_data_type);
2729-
return GARROW_DATA_TYPE(garrow_data_type);
2742+
auto arrow_extension_data_type =
2743+
std::static_pointer_cast<arrow::ExtensionType>(*arrow_data_type);
2744+
auto name = arrow_extension_data_type->extension_name();
2745+
if (name == "arrow.fixed_shape_tensor") {
2746+
type = GARROW_TYPE_FIXED_SHAPE_TENSOR_DATA_TYPE;
2747+
} else if (name == "arrow.uuid") {
2748+
type = GARROW_TYPE_UUID_DATA_TYPE;
2749+
} else {
2750+
auto g_extension_data_type =
2751+
std::dynamic_pointer_cast<garrow::GExtensionType>(*arrow_data_type);
2752+
if (g_extension_data_type) {
2753+
auto garrow_data_type = g_extension_data_type->garrow_data_type();
2754+
g_object_ref(garrow_data_type);
2755+
return GARROW_DATA_TYPE(garrow_data_type);
2756+
}
27302757
}
27312758
}
2732-
type = GARROW_TYPE_EXTENSION_DATA_TYPE;
27332759
break;
27342760
case arrow::Type::type::FIXED_SIZE_LIST:
27352761
type = GARROW_TYPE_FIXED_SIZE_LIST_DATA_TYPE;

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,19 @@ GArrowChunkedArray *
758758
garrow_extension_data_type_wrap_chunked_array(GArrowExtensionDataType *data_type,
759759
GArrowChunkedArray *storage);
760760

761+
#define GARROW_TYPE_UNKNOWN_EXTENSION_DATA_TYPE \
762+
(garrow_unknown_extension_data_type_get_type())
763+
GARROW_AVAILABLE_IN_25_0
764+
G_DECLARE_DERIVABLE_TYPE(GArrowUnknownExtensionDataType,
765+
garrow_unknown_extension_data_type,
766+
GARROW,
767+
UNKNOWN_EXTENSION_DATA_TYPE,
768+
GArrowExtensionDataType)
769+
struct _GArrowUnknownExtensionDataTypeClass
770+
{
771+
GArrowExtensionDataTypeClass parent_class;
772+
};
773+
761774
#define GARROW_TYPE_EXTENSION_DATA_TYPE_REGISTRY \
762775
(garrow_extension_data_type_registry_get_type())
763776
GARROW_AVAILABLE_IN_3_0

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,9 @@ def test_mismatch_dim_names_size
104104
assert_equal(message,
105105
error.message.lines.first.chomp)
106106
end
107+
108+
def test_converted_from_cpp
109+
schema = Arrow::Schema.new([Arrow::Field.new("tensor", @data_type)])
110+
assert_equal(@data_type, schema.fields[0].data_type)
111+
end
107112
end

c_glib/test/test-uuid-data-type.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,9 @@ def test_name
3232
def test_to_s
3333
assert_equal("extension<arrow.uuid>", @data_type.to_s)
3434
end
35+
36+
def test_converted_from_cpp
37+
schema = Arrow::Schema.new([Arrow::Field.new("uuid", @data_type)])
38+
assert_equal(@data_type, schema.fields[0].data_type)
39+
end
3540
end

0 commit comments

Comments
 (0)