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

[BEAM-5646] Fix quality and hashcode for bytes in Row. #6765

Merged
merged 1 commit into from Oct 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -347,12 +347,12 @@ public boolean equals(Object o) {
}
Row other = (Row) o;
return Objects.equals(getSchema(), other.getSchema())
&& Objects.equals(getValues(), other.getValues());
&& Objects.deepEquals(getValues().toArray(), other.getValues().toArray());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually not enough. If you have a schema with f: LIST<BYTES> it will not have correct equality. We need our own schema-driven deep equality.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. This is a valid concern. If there is no other option, we need our deep equality check for our schema.

https://issues.apache.org/jira/browse/BEAM-5868

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem comes from Objects.deepEquals only have a deep equal implementation for primitive types and array. So Map and List will at least fail to check the correct equality.

}

@Override
public int hashCode() {
amaliujia marked this conversation as resolved.
Show resolved Hide resolved
return Objects.hash(getSchema(), getValues());
return Arrays.deepHashCode(new Object[] {getSchema(), getValues().toArray()});
}

@Override
Expand Down
Expand Up @@ -26,6 +26,7 @@
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import java.math.BigDecimal;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
Expand All @@ -34,6 +35,7 @@
import org.apache.beam.sdk.schemas.Schema.FieldType;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
Expand Down Expand Up @@ -297,4 +299,30 @@ public void testThrowsForIncorrectNumberOfFields() {
thrown.expect(IllegalArgumentException.class);
Row.withSchema(type).addValues(1, "2").build();
}

@Test
public void testByteArrayEquality() {
byte[] a0 = new byte[] {1, 2, 3, 4};
byte[] b0 = new byte[] {1, 2, 3, 4};

Schema schema = Schema.of(Schema.Field.of("bytes", Schema.FieldType.BYTES));

Row a = Row.withSchema(schema).addValue(a0).build();
Row b = Row.withSchema(schema).addValue(b0).build();

Assert.assertEquals(a, b);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will this fail without the change in hashCode?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope. Seems like hashCode is not on the path of quality.

}

@Test
public void testByteBufferEquality() {
byte[] a0 = new byte[] {1, 2, 3, 4};
byte[] b0 = new byte[] {1, 2, 3, 4};

Schema schema = Schema.of(Schema.Field.of("bytes", Schema.FieldType.BYTES));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need a test of many deeper structures.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


Row a = Row.withSchema(schema).addValue(ByteBuffer.wrap(a0)).build();
Row b = Row.withSchema(schema).addValue(ByteBuffer.wrap(b0)).build();

Assert.assertEquals(a, b);
}
}