Skip to content

Commit

Permalink
ARROW-17289: [C++] Add type category membership checks (apache#13783)
Browse files Browse the repository at this point in the history
See https://issues.apache.org/jira/browse/ARROW-17289

Lead-authored-by: Yaron Gvili <[email protected]>
Co-authored-by: rtpsw <[email protected]>
Co-authored-by: Antoine Pitrou <[email protected]>
Signed-off-by: Antoine Pitrou <[email protected]>
  • Loading branch information
2 people authored and zagto committed Oct 7, 2022
1 parent 9596839 commit 8d6b36b
Show file tree
Hide file tree
Showing 3 changed files with 438 additions and 45 deletions.
16 changes: 11 additions & 5 deletions cpp/src/arrow/type.h
Original file line number Diff line number Diff line change
Expand Up @@ -2161,31 +2161,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

0 comments on commit 8d6b36b

Please sign in to comment.