Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ARROW-17289: [C++] Add type category membership checks #13783

Merged
merged 12 commits into from
Aug 17, 2022
16 changes: 11 additions & 5 deletions cpp/src/arrow/type.h
Original file line number Diff line number Diff line change
Expand Up @@ -2123,31 +2123,37 @@ std::string ToString(TimeUnit::type unit);

// Helpers to get instances of data types based on general categories

/// \brief Signed integer types
ARROW_EXPORT
const std::vector<std::shared_ptr<DataType>>& SignedIntTypes();
/// \brief Unsigned integer types
ARROW_EXPORT
const std::vector<std::shared_ptr<DataType>>& UnsignedIntTypes();
/// \brief Signed and unsigned integer types
ARROW_EXPORT
const std::vector<std::shared_ptr<DataType>>& IntTypes();
/// \brief Floating point types
ARROW_EXPORT
const std::vector<std::shared_ptr<DataType>>& FloatingPointTypes();
// Number types without boolean
/// \brief Number types without boolean - integer and floating point types
ARROW_EXPORT
const std::vector<std::shared_ptr<DataType>>& NumericTypes();
// Binary and string-like types (except fixed-size binary)
/// \brief Binary and string-like types (except fixed-size binary)
ARROW_EXPORT
const std::vector<std::shared_ptr<DataType>>& BaseBinaryTypes();
/// \brief Binary and large-binary types
ARROW_EXPORT
const std::vector<std::shared_ptr<DataType>>& BinaryTypes();
/// \brief String and large-string types
ARROW_EXPORT
const std::vector<std::shared_ptr<DataType>>& StringTypes();
// Temporal types including time and timestamps for each unit
/// \brief Temporal types including date, time and timestamps for each unit
ARROW_EXPORT
const std::vector<std::shared_ptr<DataType>>& TemporalTypes();
// Interval types
/// \brief Interval types
ARROW_EXPORT
const std::vector<std::shared_ptr<DataType>>& IntervalTypes();
// Integer, floating point, base binary, and temporal
/// \brief Numeric, base binary, date, boolean and null types
ARROW_EXPORT
const std::vector<std::shared_ptr<DataType>>& PrimitiveTypes();

Expand Down
43 changes: 43 additions & 0 deletions cpp/src/arrow/type_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1821,4 +1821,47 @@ TEST(TypesTest, TestDecimalEquals) {
AssertTypeNotEqual(t5, t10);
}

#define TEST_PREDICATE(all_types, type_predicate) \
for (auto type : all_types) { \
ASSERT_EQ(type_predicate(type->id()), type_predicate(*type)); \
}

TEST(TypesTest, TestMembership) {
std::vector<std::shared_ptr<DataType>> all_types;
for (auto type : NumericTypes()) {
all_types.push_back(type);
}
for (auto type : TemporalTypes()) {
all_types.push_back(type);
}
for (auto type : IntervalTypes()) {
all_types.push_back(type);
}
for (auto type : PrimitiveTypes()) {
all_types.push_back(type);
}
TEST_PREDICATE(all_types, is_integer);
TEST_PREDICATE(all_types, is_signed_integer);
TEST_PREDICATE(all_types, is_unsigned_integer);
TEST_PREDICATE(all_types, is_floating);
TEST_PREDICATE(all_types, is_numeric);
TEST_PREDICATE(all_types, is_decimal);
TEST_PREDICATE(all_types, is_primitive);
TEST_PREDICATE(all_types, is_base_binary_like);
TEST_PREDICATE(all_types, is_binary_like);
TEST_PREDICATE(all_types, is_large_binary_like);
TEST_PREDICATE(all_types, is_binary);
TEST_PREDICATE(all_types, is_string);
TEST_PREDICATE(all_types, is_temporal);
TEST_PREDICATE(all_types, is_interval);
TEST_PREDICATE(all_types, is_dictionary);
TEST_PREDICATE(all_types, is_fixed_size_binary);
TEST_PREDICATE(all_types, is_fixed_width);
TEST_PREDICATE(all_types, is_list_like);
TEST_PREDICATE(all_types, is_nested);
TEST_PREDICATE(all_types, is_union);
}

#undef TEST_PREDICATE

} // namespace arrow
Loading