You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@avro.apache.org by GitBox <gi...@apache.org> on 2022/05/19 22:28:31 UTC

[GitHub] [avro] itstheceo opened a new pull request, #1692: AVRO-3520 expose read schema in custom encoding

itstheceo opened a new pull request, #1692:
URL: https://github.com/apache/avro/pull/1692

   Currently the read schema is not exposed when using `CustomEncoding<T>` which makes detecting schema changes impossible without rolling your own versioning inside the record itself. This is not an option for files that already exist, and it adds overhead by adding the version to every row in the file.
   
   To address this issue I added a `CustomEncoderFieldAccess` interface and set the read schema onto the encoder for the respective field accessor. This method can be overridden in the concrete implementation of `CustomEncoding<T>` enabling the detection of a schema change in a way that makes backwards compatible reads possible.
   
   ### Jira
   
   - [x] My PR addresses the following [Avro Jira](https://issues.apache.org/jira/browse/AVRO/) issues and references them in the PR title.
   - https://issues.apache.org/jira/browse/AVRO-3520
   
   ### Tests
   
   - [x] My PR adds the following unit tests.
   - TestReflectCustomEncoderFieldAccess
   
   ### Commits
   
   - [x] My commits all reference Jira issues in their subject lines. In addition, my commits follow the guidelines from "[How to write a good git commit message](https://chris.beams.io/posts/git-commit/)":
     1. Subject is separated from body by a blank line
     1. Subject is limited to 50 characters (not including Jira issue reference)
     1. Subject does not end with a period
     1. Subject uses the imperative mood ("add", not "adding")
     1. Body wraps at 72 characters
     1. Body explains "what" and "why", not "how"
   
   ### Documentation
   
   - [x] In case of new functionality, my PR adds documentation that describes how to use it.
     - All the public functions and the classes in the PR contain Javadoc that explain what it does


-- 
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.

To unsubscribe, e-mail: dev-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [avro] github-code-scanning[bot] commented on a diff in pull request #1692: AVRO-3520: Expose read schema in custom encoding

Posted by GitBox <gi...@apache.org>.
github-code-scanning[bot] commented on code in PR #1692:
URL: https://github.com/apache/avro/pull/1692#discussion_r913153611


##########
lang/java/avro/src/test/java/org/apache/avro/reflect/TestReflectCustomEncoderFieldAccess.java:
##########
@@ -0,0 +1,158 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.avro.reflect;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+
+import org.apache.avro.Schema;
+import org.apache.avro.file.CodecFactory;
+import org.apache.avro.file.DataFileStream;
+import org.apache.avro.file.DataFileWriter;
+import org.apache.avro.io.DatumReader;
+import org.apache.avro.io.Decoder;
+import org.apache.avro.io.Encoder;
+import org.junit.Test;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.util.Arrays;
+import java.util.Collections;
+
+public class TestReflectCustomEncoderFieldAccess {
+
+  @Test
+  public void testReadSchemaIsSet() throws IOException {
+    Custom in = new Custom("hello", "world");
+    byte[] encoded = write(in);
+
+    CustomEncoder.SCHEMA = CustomEncoder.v2Schema;
+    Custom decoded = read(encoded);
+    assertNotNull(decoded);
+    assertEquals("hello", decoded.v1Field);
+    assertNull(decoded.v2Field);
+
+    ReflectData.get().removeFromSchemaClassCache(Wrapper.class);
+    encoded = write(in);
+
+    decoded = read(encoded);
+    assertNotNull(decoded);
+    assertEquals("hello", decoded.v1Field);
+    assertEquals("world", decoded.v2Field);
+  }
+
+  private Custom read(byte[] toDecode) throws IOException {
+    DatumReader<Wrapper> datumReader = new ReflectDatumReader<>();
+    DataFileStream<Wrapper> dataFileReader = new DataFileStream<>(
+        new ByteArrayInputStream(toDecode, 0, toDecode.length), datumReader);
+    Wrapper wrapper = null;
+    if (dataFileReader.hasNext()) {
+      wrapper = dataFileReader.next();
+    }
+    return wrapper.custom;

Review Comment:
   ## Dereferenced variable may be null
   
   Variable [wrapper](1) may be null here because of [this](2) assignment.
   
   [Show more details](https://github.com/apache/avro/security/code-scanning/2725)



-- 
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.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [avro] itstheceo commented on a diff in pull request #1692: AVRO-3520: Expose read schema in custom encoding

Posted by GitBox <gi...@apache.org>.
itstheceo commented on code in PR #1692:
URL: https://github.com/apache/avro/pull/1692#discussion_r915221411


##########
lang/java/avro/src/main/java/org/apache/avro/reflect/CustomEncoderFieldAccess.java:
##########
@@ -0,0 +1,32 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.avro.reflect;
+
+import org.apache.avro.Schema;
+
+public interface CustomEncoderFieldAccess {

Review Comment:
   Added :)



-- 
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.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [avro] KalleOlaviNiemitalo commented on pull request #1692: AVRO-3520: Expose read schema in custom encoding

Posted by GitBox <gi...@apache.org>.
KalleOlaviNiemitalo commented on PR #1692:
URL: https://github.com/apache/avro/pull/1692#issuecomment-1174863781

   I wonder if the method be called `withSchema` rather than `setSchema` because it is apparently not intended to mutate the target instance.


-- 
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.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [avro] KalleOlaviNiemitalo commented on a diff in pull request #1692: AVRO-3520: Expose read schema in custom encoding

Posted by GitBox <gi...@apache.org>.
KalleOlaviNiemitalo commented on code in PR #1692:
URL: https://github.com/apache/avro/pull/1692#discussion_r913627123


##########
lang/java/avro/src/main/java/org/apache/avro/reflect/CustomEncoderFieldAccess.java:
##########
@@ -0,0 +1,32 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.avro.reflect;
+
+import org.apache.avro.Schema;
+
+public interface CustomEncoderFieldAccess {

Review Comment:
   Please add javadoc to both FieldAccessor and CustomEncoderFieldAccess, saying that classes that extend FieldAccessor can optionally implement CustomEncoderFieldAccess.



-- 
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.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org