[ https://issues.apache.org/jira/browse/BEAM-9041?focusedWorklogId=367798&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-367798 ]
ASF GitHub Bot logged work on BEAM-9041: ---------------------------------------- Author: ASF GitHub Bot Created on: 07/Jan/20 21:55 Start Date: 07/Jan/20 21:55 Worklog Time Spent: 10m Work Description: iemejia commented on pull request #10492: [BEAM-9041, BEAM-9042] SchemaCoder equals should not rely on from/toRowFunction equality URL: https://github.com/apache/beam/pull/10492#discussion_r363970136 ########## File path: sdks/java/core/src/main/java/org/apache/beam/sdk/schemas/utils/AvroUtils.java ########## @@ -431,7 +465,47 @@ public static GenericRecord toGenericRecord( */ public static SerializableFunction<Row, GenericRecord> getRowToGenericRecordFunction( @Nullable org.apache.avro.Schema avroSchema) { - return g -> toGenericRecord(g, avroSchema); + return new RowToGenericRecordFn(avroSchema); + } + + private static class RowToGenericRecordFn implements SerializableFunction<Row, GenericRecord> { + private transient org.apache.avro.Schema avroSchema; + + RowToGenericRecordFn(@Nullable org.apache.avro.Schema avroSchema) { + this.avroSchema = avroSchema; + } + + @Override + public GenericRecord apply(Row input) { + return toGenericRecord(input, avroSchema); + } + + @Override + public boolean equals(Object other) { + if (this == other) { + return true; + } + if (other == null || getClass() != other.getClass()) { + return false; + } + RowToGenericRecordFn that = (RowToGenericRecordFn) other; + return avroSchema.equals(that.avroSchema); + } + + @Override + public int hashCode() { + return Objects.hash(avroSchema); + } + + private void writeObject(ObjectOutputStream out) throws IOException { + final String avroSchemaAsString = (avroSchema == null) ? null : avroSchema.toString(); + out.writeObject(avroSchemaAsString); + } + + private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { + final String avroSchemaAsString = (String) in.readObject(); + avroSchema = new org.apache.avro.Schema.Parser().parse(avroSchemaAsString); Review comment: Yes you are right I forgot to check the nullability of the string before the parse, I will fix that and add a method for this. Hopefully everything will be green at that moment. Thanks for the hint. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org Issue Time Tracking ------------------- Worklog Id: (was: 367798) Time Spent: 2h 10m (was: 2h) > SchemaCoder equals should not rely on from/toRowFunction equality > ----------------------------------------------------------------- > > Key: BEAM-9041 > URL: https://issues.apache.org/jira/browse/BEAM-9041 > Project: Beam > Issue Type: Bug > Components: sdk-java-core > Reporter: Ismaël Mejía > Assignee: Ismaël Mejía > Priority: Minor > Fix For: 2.18.0 > > Time Spent: 2h 10m > Remaining Estimate: 0h > > SchemaCoder equals implementation relies on SerializableFunction equals > method, this is error-prone because users rarely implement the equals method > for a SerializableFunction. One alternative would be to rely on bytes > equality for this. -- This message was sent by Atlassian Jira (v8.3.4#803005)