Skip to content

Commit

Permalink
[BEAM-5646] Fix quality and hashcode for bytes in Row.
Browse files Browse the repository at this point in the history
  • Loading branch information
amaliujia committed Oct 22, 2018
1 parent e475c4c commit b66a249
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
Expand Up @@ -346,13 +346,13 @@ public boolean equals(Object o) {
return false;
}
Row other = (Row) o;
return Objects.equals(getSchema(), other.getSchema())
&& Objects.equals(getValues(), other.getValues());
return Objects.deepEquals(getSchema(), other.getSchema())
&& Objects.deepEquals(getValues().toArray(), other.getValues().toArray());
}

@Override
public int hashCode() {
return Objects.hash(getSchema(), getValues());
return Arrays.deepHashCode(new Object[] {getSchema(), getValues().toArray()});
}

@Override
Expand Down
Expand Up @@ -27,6 +27,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 @@ -35,6 +36,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 @@ -298,4 +300,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);
}

@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));

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

Assert.assertEquals(a, b);
}
}

0 comments on commit b66a249

Please sign in to comment.