Skip to content

Commit

Permalink
ARROW-6772: [C++] Add operator== for interfaces with an Equals() meth…
Browse files Browse the repository at this point in the history
…od (apache#14038)

Addresses [ARROW-6772](https://issues.apache.org/jira/browse/ARROW-6772).

Adds util::EqualityComparable mixin to relevant types, replacing prior operator==() overloads if applicable.

Authored-by: benibus <bpharks@gmx.com>
Signed-off-by: Antoine Pitrou <antoine@python.org>
  • Loading branch information
benibus authored and zagto committed Oct 7, 2022
1 parent 16c819a commit d200a1c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
17 changes: 5 additions & 12 deletions cpp/src/arrow/type.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ struct ARROW_EXPORT DataTypeLayout {
/// Simple datatypes may be entirely described by their Type::type id, but
/// complex datatypes are usually parametric.
class ARROW_EXPORT DataType : public std::enable_shared_from_this<DataType>,
public detail::Fingerprintable {
public detail::Fingerprintable,
public util::EqualityComparable<DataType> {
public:
explicit DataType(Type::type id) : detail::Fingerprintable(), id_(id) {}
~DataType() override;
Expand Down Expand Up @@ -271,13 +272,6 @@ std::ostream& operator<<(std::ostream& os, const DataType& type);
ARROW_EXPORT
std::ostream& operator<<(std::ostream& os, const TypeHolder& type);

inline bool operator==(const DataType& lhs, const DataType& rhs) {
return lhs.Equals(rhs);
}
inline bool operator!=(const DataType& lhs, const DataType& rhs) {
return !lhs.Equals(rhs);
}

/// \brief Return the compatible physical data type
///
/// Some types may have distinct logical meanings but the exact same physical
Expand Down Expand Up @@ -338,7 +332,8 @@ class ARROW_EXPORT NestedType : public DataType, public ParametricType {
///
/// A field's metadata is represented by a KeyValueMetadata instance,
/// which holds arbitrary key-value pairs.
class ARROW_EXPORT Field : public detail::Fingerprintable {
class ARROW_EXPORT Field : public detail::Fingerprintable,
public util::EqualityComparable<Field> {
public:
Field(std::string name, std::shared_ptr<DataType> type, bool nullable = true,
std::shared_ptr<const KeyValueMetadata> metadata = NULLPTR)
Expand Down Expand Up @@ -1717,7 +1712,7 @@ class ARROW_EXPORT FieldPath {
/// matching children:
/// auto maybe_match = FieldRef("struct", "field_i32").FindOneOrNone(schema);
/// auto maybe_column = FieldRef("struct", "field_i32").GetOne(some_table);
class ARROW_EXPORT FieldRef {
class ARROW_EXPORT FieldRef : public util::EqualityComparable<FieldRef> {
public:
FieldRef() = default;

Expand Down Expand Up @@ -1771,8 +1766,6 @@ class ARROW_EXPORT FieldRef {
std::string ToDotPath() const;

bool Equals(const FieldRef& other) const { return impl_ == other.impl_; }
bool operator==(const FieldRef& other) const { return Equals(other); }
bool operator!=(const FieldRef& other) const { return !Equals(other); }

std::string ToString() const;

Expand Down
8 changes: 8 additions & 0 deletions cpp/src/arrow/type_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@ TEST(TestField, Equals) {
AssertFieldEqual(f0, f0_with_meta1);
AssertFieldEqual(f0, f0_with_meta2);
AssertFieldEqual(f0_with_meta1, f0_with_meta2);

// operator==(), where check_metadata == false
ASSERT_EQ(f0, f0_other);
ASSERT_NE(f0, f0_nn);
ASSERT_EQ(f0, f0_with_meta1);
ASSERT_EQ(f0_with_meta1, f0_with_meta2);
}

#define ASSERT_COMPATIBLE_IMPL(NAME, TYPE, PLURAL) \
Expand Down Expand Up @@ -472,6 +478,8 @@ TEST_F(TestSchema, Basics) {
auto schema3 = std::make_shared<Schema>(fields3);
AssertSchemaEqual(schema, schema2);
AssertSchemaNotEqual(schema, schema3);
ASSERT_EQ(*schema, *schema2);
ASSERT_NE(*schema, *schema3);

ASSERT_EQ(schema->fingerprint(), schema2->fingerprint());
ASSERT_NE(schema->fingerprint(), schema3->fingerprint());
Expand Down

0 comments on commit d200a1c

Please sign in to comment.