-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Core: Fix equality in StructLikeMap #9236
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,6 @@ | |
import java.util.AbstractMap; | ||
import java.util.Collection; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
import java.util.function.Function; | ||
import org.apache.iceberg.StructLike; | ||
|
@@ -128,9 +127,9 @@ public Set<Entry<StructLike, T>> entrySet() { | |
|
||
private static class StructLikeEntry<R> implements Entry<StructLike, R> { | ||
|
||
private Map.Entry<StructLikeWrapper, R> inner; | ||
private final Entry<StructLikeWrapper, R> inner; | ||
|
||
private StructLikeEntry(Map.Entry<StructLikeWrapper, R> inner) { | ||
private StructLikeEntry(Entry<StructLikeWrapper, R> inner) { | ||
this.inner = inner; | ||
} | ||
|
||
|
@@ -146,25 +145,19 @@ public R getValue() { | |
|
||
@Override | ||
public int hashCode() { | ||
int hashCode = getKey().hashCode(); | ||
if (getValue() != null) { | ||
hashCode ^= getValue().hashCode(); | ||
} | ||
return hashCode; | ||
return inner.hashCode(); | ||
} | ||
|
||
@Override | ||
@SuppressWarnings("unchecked") | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} else if (!(o instanceof StructLikeEntry)) { | ||
} else if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} else { | ||
StructLikeEntry that = (StructLikeEntry<R>) o; | ||
return Objects.equals(getKey(), that.getKey()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We used |
||
&& Objects.equals(getValue(), that.getValue()); | ||
} | ||
|
||
StructLikeEntry<?> that = (StructLikeEntry<?>) o; | ||
return inner.equals(that.inner); | ||
} | ||
|
||
@Override | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,8 @@ | |
import java.util.Map; | ||
import java.util.Set; | ||
import org.apache.iceberg.StructLike; | ||
import org.apache.iceberg.TestHelpers.CustomRow; | ||
import org.apache.iceberg.TestHelpers.Row; | ||
import org.apache.iceberg.data.GenericRecord; | ||
import org.apache.iceberg.data.Record; | ||
import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap; | ||
|
@@ -147,4 +149,32 @@ public void testKeysWithNulls() { | |
|
||
assertThat(map.remove(record3)).isEqualTo("aaa"); | ||
} | ||
|
||
@Test | ||
public void testEqualsAndHashCode() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we do a quick addition of a test for empty map equality? I am pretty sure that is fine but just to be complete There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, I'll add that. |
||
Map<StructLike, String> map1 = StructLikeMap.create(STRUCT_TYPE); | ||
map1.put(CustomRow.of(1, null), "aaa"); | ||
map1.put(CustomRow.of(2, null), "bbb"); | ||
|
||
Map<StructLike, String> map2 = StructLikeMap.create(STRUCT_TYPE); | ||
map2.put(Row.of(1, null), "aaa"); | ||
map2.put(Row.of(2, null), "bbb"); | ||
|
||
assertThat(map1).isEqualTo(map2); | ||
assertThat(map1.hashCode()).isEqualTo(map2.hashCode()); | ||
} | ||
|
||
@Test | ||
public void testKeyAndEntrySetEquality() { | ||
Map<StructLike, String> map1 = StructLikeMap.create(STRUCT_TYPE); | ||
map1.put(CustomRow.of(1, null), "aaa"); | ||
map1.put(CustomRow.of(2, null), "bbb"); | ||
|
||
Map<StructLike, String> map2 = StructLikeMap.create(STRUCT_TYPE); | ||
map2.put(Row.of(1, null), "aaa"); | ||
map2.put(Row.of(2, null), "bbb"); | ||
|
||
assertThat(map1.keySet()).isEqualTo(map2.keySet()); | ||
assertThat(map1.entrySet()).isEqualTo(map2.entrySet()); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was also throwing NPE cause key could be null. Not anymore.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also,
getKey().hashCode()
meant we used the hash of the actual element, not the wrapper.