[
https://issues.apache.org/jira/browse/BEAM-8364?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]
Kenneth Knowles updated BEAM-8364:
----------------------------------
This Jira ticket has a pull request attached to it, but is still open. Did the
pull request resolve the issue? If so, could you please mark it resolved? This
will help the project have a clear view of its open issues.
> SchemaCoder inconsistent equality behavior for POJO
> ---------------------------------------------------
>
> Key: BEAM-8364
> URL: https://issues.apache.org/jira/browse/BEAM-8364
> Project: Beam
> Issue Type: Bug
> Components: dsl-sql, sdk-java-core
> Affects Versions: 2.16.0
> Reporter: Neville Li
> Priority: P3
> Labels: Clarified
> Time Spent: 1h
> Remaining Estimate: 0h
>
> One can create a {{SchemaCoder}} for arbitrary type {{T}} with
> {{SchemaCoder.of(schema, toRowFunction, fromRowFunction)}}. However, in cases
> where {{T}} lacks proper equality behavior, i.e. POJO, the result coder still
> returns true for {{consistentWithEquals}} and {{structuralValue}}s that fail
> equality check.
> This test reproduces the issue.
> {code:java}
> import org.apache.beam.sdk.schemas.Schema;
> import org.apache.beam.sdk.schemas.SchemaCoder;
> import org.apache.beam.sdk.values.Row;
> import org.junit.Test;
> import org.junit.runner.RunWith;
> import org.junit.runners.JUnit4;
> import java.nio.charset.Charset;
> import static org.junit.Assert.*;
> @RunWith(JUnit4.class)
> public class SchemaCoderTest {
> public static class Pojo {
> private final byte[] bytes;
> private final String id;
> public Pojo(byte[] bytes, String id) {
> this.bytes = bytes;
> this.id = id;
> }
> public byte[] getBytes() {
> return bytes;
> }
> public String getId() {
> return id;
> }
> }
> @Test
> public void testCoder() {
> Schema schema =
> Schema.builder().addByteArrayField("bytes").addStringField("id").build();
> SchemaCoder<Pojo> coder = SchemaCoder.<Pojo>of(
> schema,
> t -> Row.withSchema(schema).addValues(t.getBytes(),
> t.getId()).build(),
> r -> new Pojo(r.getBytes("bytes"), r.getString("id")));
> Pojo p1 = new Pojo("hello".getBytes(Charset.forName("UTF-8")), "world");
> Pojo p2 = new Pojo("hello".getBytes(Charset.forName("UTF-8")), "world");
> assertNotEquals(p1, p2); // EXPECTED, p1.equals(p2) == false
> assertFalse(coder.consistentWithEquals()); // FAIL, returns true
> assertEquals(coder.structuralValue(p1), coder.structuralValue(p2)); //
> FAIL
> }
> }
> {code}
--
This message was sent by Atlassian Jira
(v8.20.1#820001)