Skip to content

Commit 98795b2

Browse files
ARROW-46531: Refactor type_singleton using TypeTraits and VisitTypeIdInline.
1 parent 8e8abdc commit 98795b2

6 files changed

Lines changed: 48 additions & 88 deletions

File tree

cpp/src/arrow/scalar_test.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,8 @@ TEST(TestScalar, IdentityCast) {
180180
test_identity_cast_for_type(type);
181181
}
182182
for (auto& type : {
183-
arrow::fixed_size_list(arrow::int32(), 20), arrow::list(arrow::int32()),
183+
arrow::fixed_size_list(arrow::int32(), 20),
184+
arrow::list(arrow::int32()),
184185
arrow::large_list(arrow::int32()),
185186
// TODO(GH-45430): CastTo for ListView is not implemented yet.
186187
// arrow::list_view(arrow::int32()), arrow::large_list_view(arrow::int32())

cpp/src/arrow/type.cc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "arrow/result.h"
3939
#include "arrow/status.h"
4040
#include "arrow/table.h"
41+
#include "arrow/type_traits.h"
4142
#include "arrow/util/checked_cast.h"
4243
#include "arrow/util/decimal.h"
4344
#include "arrow/util/hash_util.h"
@@ -3552,4 +3553,26 @@ const std::vector<Type::type>& DecimalTypeIds() {
35523553
return type_ids;
35533554
}
35543555

3556+
Result<std::shared_ptr<DataType>> type_singleton(Type::type id) {
3557+
struct Visitor {
3558+
Result<std::shared_ptr<DataType>> result;
3559+
3560+
template <typename T>
3561+
Status Visit(const T* type) {
3562+
if constexpr (TypeTraits<T>::is_parameter_free) {
3563+
result = TypeTraits<T>::type_singleton();
3564+
return Status::OK();
3565+
}
3566+
return Status::Invalid("Type ", T::type_id, " is not a parameter-free singleton type.");
3567+
}
3568+
};
3569+
3570+
Visitor visitor;
3571+
auto status = VisitTypeIdInline(id, &visitor);
3572+
if (!status.ok()) {
3573+
return Status::Invalid("Type ", id, " requires parameters or is not supported");
3574+
}
3575+
return visitor.result;
3576+
}
3577+
35553578
} // namespace arrow

cpp/src/arrow/type.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2645,4 +2645,17 @@ const std::vector<std::shared_ptr<DataType>>& PrimitiveTypes();
26452645
ARROW_EXPORT
26462646
const std::vector<Type::type>& DecimalTypeIds();
26472647

2648+
/// \brief Create a data type instance from a type ID for parameter-free types
2649+
///
2650+
/// This function creates a data type instance for types that don't require
2651+
/// additional parameters (where TypeTraits<T>::is_parameter_free is true).
2652+
/// For types that require parameters (like TimestampType or ListType),
2653+
/// this function will return an error.
2654+
///
2655+
/// \param[in] id The type ID to create a type instance for
2656+
/// \return Result with a shared pointer to the created DataType instance,
2657+
/// or an error if the type requires parameters
2658+
ARROW_EXPORT
2659+
Result<std::shared_ptr<DataType>> type_singleton(Type::type id);
2660+
26482661
} // namespace arrow

cpp/src/arrow/type_test.cc

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@
3838
#include "arrow/testing/util.h"
3939
#include "arrow/type.h"
4040
#include "arrow/type_traits.h"
41-
#include "arrow/util/logging.h"
4241
#include "arrow/util/checked_cast.h"
4342
#include "arrow/util/key_value_metadata.h"
43+
#include "arrow/util/logging.h"
4444

4545
namespace arrow {
4646

@@ -53,25 +53,12 @@ TEST(TestTypeId, AllTypeIds) {
5353
}
5454

5555
TEST(TestTypeSingleton, ParameterFreeTypes) {
56-
// Test successful cases - parameter-free types
56+
// Test successful cases - parameter-free types (sample a few)
5757
std::vector<std::pair<Type::type, std::shared_ptr<DataType>>> cases = {
5858
{Type::NA, null()},
5959
{Type::BOOL, boolean()},
60-
{Type::INT8, int8()},
61-
{Type::INT16, int16()},
6260
{Type::INT32, int32()},
63-
{Type::INT64, int64()},
64-
{Type::UINT8, uint8()},
65-
{Type::UINT16, uint16()},
66-
{Type::UINT32, uint32()},
67-
{Type::UINT64, uint64()},
68-
{Type::HALF_FLOAT, float16()},
69-
{Type::FLOAT, float32()},
70-
{Type::DOUBLE, float64()},
7161
{Type::STRING, utf8()},
72-
{Type::BINARY, binary()},
73-
{Type::LARGE_STRING, large_utf8()},
74-
{Type::LARGE_BINARY, large_binary()},
7562
{Type::DATE32, date32()},
7663
};
7764

@@ -88,19 +75,18 @@ TEST(TestTypeSingleton, ParameterFreeTypes) {
8875
TEST(TestTypeSingleton, ParameterizedTypes) {
8976
// Test error cases - parameterized types
9077
std::vector<Type::type> parameterized_types = {
91-
Type::TIMESTAMP, Type::TIME32, Type::TIME64,
92-
Type::DURATION, Type::FIXED_SIZE_BINARY, Type::DECIMAL128,
93-
Type::LIST, Type::LARGE_LIST, Type::FIXED_SIZE_LIST,
94-
Type::STRUCT, Type::DICTIONARY, Type::MAP,
95-
Type::EXTENSION
96-
};
78+
Type::TIMESTAMP, Type::TIME32, Type::TIME64, Type::DURATION,
79+
Type::FIXED_SIZE_BINARY, Type::DECIMAL128, Type::LIST, Type::LARGE_LIST,
80+
Type::FIXED_SIZE_LIST, Type::STRUCT, Type::DICTIONARY, Type::MAP,
81+
Type::EXTENSION};
9782

9883
for (const auto type_id : parameterized_types) {
9984
SCOPED_TRACE("Testing type: " + std::to_string(static_cast<int>(type_id)));
10085
auto result = type_singleton(type_id);
10186
ASSERT_FALSE(result.ok());
10287
const auto& status = result.status();
103-
EXPECT_THAT(status.message(), testing::HasSubstr("is not a parameter-free singleton type"));
88+
EXPECT_THAT(status.message(),
89+
testing::HasSubstr("is not a parameter-free singleton type"));
10490
}
10591
}
10692

@@ -109,7 +95,8 @@ TEST(TestTypeSingleton, InvalidType) {
10995
auto result = type_singleton(static_cast<Type::type>(9999));
11096
ASSERT_FALSE(result.ok());
11197
const auto& status = result.status();
112-
EXPECT_THAT(status.message(), testing::HasSubstr("requires parameters or is not supported"));
98+
EXPECT_THAT(status.message(),
99+
testing::HasSubstr("requires parameters or is not supported"));
113100
}
114101

115102
template <typename ReprFunc>

cpp/src/arrow/type_traits.cc

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -22,68 +22,6 @@
2222
#include "arrow/type.h"
2323
#include "arrow/util/logging_internal.h"
2424

25-
namespace arrow {
26-
27-
Result<std::shared_ptr<DataType>> type_singleton(Type::type id) {
28-
switch (id) {
29-
case Type::NA:
30-
return null();
31-
case Type::BOOL:
32-
return boolean();
33-
case Type::INT8:
34-
return int8();
35-
case Type::INT16:
36-
return int16();
37-
case Type::INT32:
38-
return int32();
39-
case Type::INT64:
40-
return int64();
41-
case Type::UINT8:
42-
return uint8();
43-
case Type::UINT16:
44-
return uint16();
45-
case Type::UINT32:
46-
return uint32();
47-
case Type::UINT64:
48-
return uint64();
49-
case Type::HALF_FLOAT:
50-
return float16();
51-
case Type::FLOAT:
52-
return float32();
53-
case Type::DOUBLE:
54-
return float64();
55-
case Type::STRING:
56-
return utf8();
57-
case Type::BINARY:
58-
return binary();
59-
case Type::LARGE_STRING:
60-
return large_utf8();
61-
case Type::LARGE_BINARY:
62-
return large_binary();
63-
case Type::DATE32:
64-
return date32();
65-
66-
// Explicitly handle known parameterized types
67-
case Type::TIMESTAMP:
68-
case Type::TIME32:
69-
case Type::TIME64:
70-
case Type::DURATION:
71-
case Type::FIXED_SIZE_BINARY:
72-
case Type::DECIMAL128:
73-
case Type::LIST:
74-
case Type::LARGE_LIST:
75-
case Type::FIXED_SIZE_LIST:
76-
case Type::STRUCT:
77-
case Type::DICTIONARY:
78-
case Type::MAP:
79-
case Type::EXTENSION:
80-
return Status::Invalid("Type ", id, " is not a parameter-free singleton type.");
81-
82-
default:
83-
return Status::Invalid("Type ", id, " requires parameters or is not supported");
84-
}
85-
}
86-
8725
int RequiredValueAlignmentForBuffer(Type::type type_id, int buffer_index) {
8826
if (buffer_index == 2 && type_id == Type::DENSE_UNION) {
8927
// A dense union array is the only array (so far) that requires alignment

cpp/src/arrow/type_traits.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -608,8 +608,6 @@ struct TypeTraits<ExtensionType> {
608608
/// \param[in] id The type ID to create a type instance for
609609
/// \return Result with a shared pointer to the created DataType instance,
610610
/// or an error if the type requires parameters
611-
ARROW_EXPORT
612-
Result<std::shared_ptr<DataType>> type_singleton(Type::type id);
613611

614612
namespace internal {
615613

0 commit comments

Comments
 (0)