You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@avro.apache.org by "clesaec (via GitHub)" <gi...@apache.org> on 2023/09/29 10:01:46 UTC

[GitHub] [avro] clesaec opened a new pull request, #2529: Avro 3876 jackson util

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

   <!--
   
   *Thank you very much for contributing to Apache Avro - we are happy that you want to help us improve Avro. To help the community review your contribution in the best possible way, please go through the checklist below, which will get the contribution into a shape in which it can be best reviewed.*
   
   *Please understand that we do not do this to make contributions to Avro a hassle. In order to uphold a high standard of quality for code contributions, while at the same time managing a large number of contributions, we need contributors to prepare the contributions well, and give reviewers enough contextual information for the review. Please also understand that contributions that do not follow this guide will take longer to review and thus typically be picked up with lower priority by the community.*
   
   ## Contribution Checklist
   
     - Make sure that the pull request corresponds to a [JIRA issue](https://issues.apache.org/jira/projects/AVRO/issues). Exceptions are made for typos in JavaDoc or documentation files, which need no JIRA issue.
     
     - Name the pull request in the form "AVRO-XXXX: [component] Title of the pull request", where *AVRO-XXXX* should be replaced by the actual issue number. 
       The *component* is optional, but can help identify the correct reviewers faster: either the language ("java", "python") or subsystem such as "build" or "doc" are good candidates.  
   
     - Fill out the template below to describe the changes contributed by the pull request. That will give reviewers the context they need to do the review.
     
     - Make sure that the change passes the automated tests. You can [build the entire project](https://github.com/apache/avro/blob/master/BUILD.md) or just the [language-specific SDK](https://avro.apache.org/project/how-to-contribute/#unit-tests).
   
     - Each pull request should address only one issue, not mix up code from multiple issues.
     
     - Each commit in the pull request has a meaningful commit message (including the JIRA id)
   
     - Every commit message references Jira issues in their subject lines. In addition, 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"
   
   -->
   
   ## What is the purpose of the change
   
   [AVRO-3876](https://issues.apache.org/jira/browse/AVRO-3876): Current JacksonUtils is not symmetric, means that
   ```java
   Object object = JacksonUtils.toObject(node);
   JsonNode node1 = JacksonUtils.toJsonNode(object);
   Assertions.assertEquals(node, node1);
   ```
   is not true for float with high precision and array of byte.
   
   Precision must also be fixed for Json encoder.
   
   
   ## Verifying this change
   
   - Test added on JacksonUtils class and on Encoder test.
   
   
   ## Documentation
   
   - Does this pull request introduce a new feature? (no)
   - If yes, how is the feature documented? (not applicable)
   


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


Re: [PR] Avro 3876 jackson util [avro]

Posted by "opwvhk (via GitHub)" <gi...@apache.org>.
opwvhk commented on code in PR #2529:
URL: https://github.com/apache/avro/pull/2529#discussion_r1342544826


##########
lang/java/avro/src/test/java/org/apache/avro/TestSchemaBuilder.java:
##########
@@ -216,10 +217,10 @@ void fieldArrayObjectProp() {
     assertEquals(Integer.MAX_VALUE, iter.next());
     assertEquals(Long.MAX_VALUE, iter.next());
     // float converts to double

Review Comment:
   Remove?



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


Re: [PR] Avro 3876 jackson util [avro]

Posted by "clesaec (via GitHub)" <gi...@apache.org>.
clesaec commented on code in PR #2529:
URL: https://github.com/apache/avro/pull/2529#discussion_r1342570161


##########
lang/java/avro/src/test/java/org/apache/avro/TestSchemaBuilder.java:
##########
@@ -141,14 +142,14 @@ void fieldObjectProps() {
     assertTrue(f.getObjectProp("intProp") instanceof Integer);
     assertTrue(f.getObjectProp("longProp") instanceof Long);
     assertEquals(Long.MAX_VALUE, f.getObjectProp("longProp"));
-    assertTrue(f.getObjectProp("floatProp") instanceof Double);
+    assertTrue(f.getObjectProp("floatProp") instanceof Float);
     // float converts to double

Review Comment:
   ??



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


Re: [PR] Avro 3876 jackson util [avro]

Posted by "opwvhk (via GitHub)" <gi...@apache.org>.
opwvhk commented on code in PR #2529:
URL: https://github.com/apache/avro/pull/2529#discussion_r1342547408


##########
lang/java/avro/src/test/java/org/apache/avro/util/internal/TestJacksonUtils.java:
##########
@@ -102,4 +114,22 @@ void testToObject() {
     assertEquals("a", toObject(TextNode.valueOf("a"), SchemaBuilder.unionOf().stringType().and().intType().endUnion()));
   }
 
+  @ParameterizedTest
+  @MethodSource("nodes")
+  void cycle(JsonNode input) {
+    Object object = JacksonUtils.toObject(input);
+    JsonNode node = JacksonUtils.toJsonNode(object);
+    Assertions.assertEquals(input, node);
+  }
+
+  public static Stream<Arguments> nodes() {
+    ObjectNode o1 = JsonNodeFactory.instance.objectNode();
+    o1.put("intField", 123);

Review Comment:
   How about small integers in a LongNode?
   
   Note: parsing JSON text must actually create an IntNode in these cases, as otherwise parsing a logical type `decimal` will fail!



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


Re: [PR] Avro 3876 jackson util [avro]

Posted by "clesaec (via GitHub)" <gi...@apache.org>.
clesaec merged PR #2529:
URL: https://github.com/apache/avro/pull/2529


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


Re: [PR] Avro 3876 jackson util [avro]

Posted by "clesaec (via GitHub)" <gi...@apache.org>.
clesaec commented on code in PR #2529:
URL: https://github.com/apache/avro/pull/2529#discussion_r1342587937


##########
lang/java/avro/src/test/java/org/apache/avro/util/internal/TestJacksonUtils.java:
##########
@@ -102,4 +114,22 @@ void testToObject() {
     assertEquals("a", toObject(TextNode.valueOf("a"), SchemaBuilder.unionOf().stringType().and().intType().endUnion()));
   }
 
+  @ParameterizedTest
+  @MethodSource("nodes")
+  void cycle(JsonNode input) {
+    Object object = JacksonUtils.toObject(input);
+    JsonNode node = JacksonUtils.toJsonNode(object);
+    Assertions.assertEquals(input, node);
+  }
+
+  public static Stream<Arguments> nodes() {
+    ObjectNode o1 = JsonNodeFactory.instance.objectNode();
+    o1.put("intField", 123);

Review Comment:
   Any suggestion for another test case here ?



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


Re: [PR] Avro 3876 jackson util [avro]

Posted by "opwvhk (via GitHub)" <gi...@apache.org>.
opwvhk commented on code in PR #2529:
URL: https://github.com/apache/avro/pull/2529#discussion_r1347045264


##########
lang/java/avro/src/test/java/org/apache/avro/util/internal/TestJacksonUtils.java:
##########
@@ -102,4 +114,22 @@ void testToObject() {
     assertEquals("a", toObject(TextNode.valueOf("a"), SchemaBuilder.unionOf().stringType().and().intType().endUnion()));
   }
 
+  @ParameterizedTest
+  @MethodSource("nodes")
+  void cycle(JsonNode input) {
+    Object object = JacksonUtils.toObject(input);
+    JsonNode node = JacksonUtils.toJsonNode(object);
+    Assertions.assertEquals(input, node);
+  }
+
+  public static Stream<Arguments> nodes() {
+    ObjectNode o1 = JsonNodeFactory.instance.objectNode();
+    o1.put("intField", 123);

Review Comment:
   This is tricky. We can see in the tests that `int` and `long` are correctly preserved by the tests here, so I think/hope we can assume the test is complete.



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


Re: [PR] Avro 3876 jackson util [avro]

Posted by "clesaec (via GitHub)" <gi...@apache.org>.
clesaec commented on code in PR #2529:
URL: https://github.com/apache/avro/pull/2529#discussion_r1342590160


##########
lang/java/avro/src/test/java/org/apache/avro/TestSchemaBuilder.java:
##########
@@ -141,14 +142,14 @@ void fieldObjectProps() {
     assertTrue(f.getObjectProp("intProp") instanceof Integer);
     assertTrue(f.getObjectProp("longProp") instanceof Long);
     assertEquals(Long.MAX_VALUE, f.getObjectProp("longProp"));
-    assertTrue(f.getObjectProp("floatProp") instanceof Double);
+    assertTrue(f.getObjectProp("floatProp") instanceof Float);
     // float converts to double

Review Comment:
   Comment removed :) 



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


Re: [PR] Avro 3876 jackson util [avro]

Posted by "opwvhk (via GitHub)" <gi...@apache.org>.
opwvhk commented on code in PR #2529:
URL: https://github.com/apache/avro/pull/2529#discussion_r1342549444


##########
lang/java/avro/src/main/java/org/apache/avro/util/internal/JacksonUtils.java:
##########
@@ -136,10 +137,23 @@ public static Object toObject(JsonNode jsonNode, Schema schema) {
         return jsonNode.asDouble();
       }
     } else if (jsonNode.isDouble() || jsonNode.isFloat()) {
-      if (schema == null || schema.getType().equals(Schema.Type.DOUBLE)) {
-        return jsonNode.asDouble();
-      } else if (schema.getType().equals(Schema.Type.FLOAT)) {
-        return (float) jsonNode.asDouble();
+      if (schema != null) {
+        if (schema.getType().equals(Schema.Type.DOUBLE)) {
+          return jsonNode.doubleValue();
+        } else if (schema.getType().equals(Schema.Type.FLOAT)) {
+          return jsonNode.floatValue();
+        }
+      } else if (jsonNode.isDouble()) {
+        return jsonNode.doubleValue();
+      } else if (jsonNode.isFloat()) {

Review Comment:
   We can drop this, given the if-statement in line 139.



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


Re: [PR] Avro 3876 jackson util [avro]

Posted by "clesaec (via GitHub)" <gi...@apache.org>.
clesaec commented on code in PR #2529:
URL: https://github.com/apache/avro/pull/2529#discussion_r1342568772


##########
lang/java/avro/src/main/java/org/apache/avro/io/JsonEncoder.java:
##########
@@ -208,7 +208,7 @@ public void writeLong(long n) throws IOException {
   @Override
   public void writeFloat(float f) throws IOException {
     parser.advance(Symbol.FLOAT);
-    out.writeNumber(f);
+    out.writeNumber(f + 0d);

Review Comment:
   We want to prevent the fact that in Java, `Float.toString(33.33000183105469f)` return "33.33" instead of the whole number.
   So, the cast to Double trigger Double.toString method instead which keep precision, and write 33.33000183105469.
   The "parser.advance(Symbol.FLOAT)" ensure we won't cast it to double at reading.
   



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


Re: [PR] Avro 3876 jackson util [avro]

Posted by "opwvhk (via GitHub)" <gi...@apache.org>.
opwvhk commented on code in PR #2529:
URL: https://github.com/apache/avro/pull/2529#discussion_r1347032473


##########
lang/java/avro/src/test/java/org/apache/avro/TestSchemaBuilder.java:
##########
@@ -111,14 +112,14 @@ void objectProps() {
     assertTrue(s.getObjectProp("intProp") instanceof Integer);
     assertTrue(s.getObjectProp("longProp") instanceof Long);
     assertEquals(Long.MAX_VALUE, s.getObjectProp("longProp"));
-    assertTrue(s.getObjectProp("floatProp") instanceof Double);
+    assertTrue(s.getObjectProp("floatProp") instanceof Float);
     // float converts to double

Review Comment:
   I intended the comment: it's no longer converted, so the comment is outdated.



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


Re: [PR] Avro 3876 jackson util [avro]

Posted by "opwvhk (via GitHub)" <gi...@apache.org>.
opwvhk commented on code in PR #2529:
URL: https://github.com/apache/avro/pull/2529#discussion_r1342550039


##########
lang/java/avro/src/main/java/org/apache/avro/util/internal/JacksonUtils.java:
##########
@@ -136,10 +137,23 @@ public static Object toObject(JsonNode jsonNode, Schema schema) {
         return jsonNode.asDouble();
       }
     } else if (jsonNode.isDouble() || jsonNode.isFloat()) {
-      if (schema == null || schema.getType().equals(Schema.Type.DOUBLE)) {
-        return jsonNode.asDouble();
-      } else if (schema.getType().equals(Schema.Type.FLOAT)) {
-        return (float) jsonNode.asDouble();
+      if (schema != null) {
+        if (schema.getType().equals(Schema.Type.DOUBLE)) {
+          return jsonNode.doubleValue();
+        } else if (schema.getType().equals(Schema.Type.FLOAT)) {
+          return jsonNode.floatValue();
+        }

Review Comment:
   Is it possible to have a JSON node with a float/double, but a schema that is neither?



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


Re: [PR] Avro 3876 jackson util [avro]

Posted by "opwvhk (via GitHub)" <gi...@apache.org>.
opwvhk commented on code in PR #2529:
URL: https://github.com/apache/avro/pull/2529#discussion_r1342548688


##########
lang/java/avro/src/main/java/org/apache/avro/io/JsonEncoder.java:
##########
@@ -208,7 +208,7 @@ public void writeLong(long n) throws IOException {
   @Override
   public void writeFloat(float f) throws IOException {
     parser.advance(Symbol.FLOAT);
-    out.writeNumber(f);
+    out.writeNumber(f + 0d);

Review Comment:
   Won't this add more digits / convert a float to a double?
   
   I though that's what we're trying to prevent.



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


Re: [PR] Avro 3876 jackson util [avro]

Posted by "opwvhk (via GitHub)" <gi...@apache.org>.
opwvhk commented on code in PR #2529:
URL: https://github.com/apache/avro/pull/2529#discussion_r1342544603


##########
lang/java/avro/src/test/java/org/apache/avro/TestSchemaBuilder.java:
##########
@@ -181,10 +182,10 @@ void arrayObjectProp() {
     assertEquals(Integer.MAX_VALUE, iter.next());
     assertEquals(Long.MAX_VALUE, iter.next());
     // float converts to double

Review Comment:
   Remove?



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


Re: [PR] Avro 3876 jackson util [avro]

Posted by "opwvhk (via GitHub)" <gi...@apache.org>.
opwvhk commented on code in PR #2529:
URL: https://github.com/apache/avro/pull/2529#discussion_r1342544321


##########
lang/java/avro/src/test/java/org/apache/avro/TestSchemaBuilder.java:
##########
@@ -111,14 +112,14 @@ void objectProps() {
     assertTrue(s.getObjectProp("intProp") instanceof Integer);
     assertTrue(s.getObjectProp("longProp") instanceof Long);
     assertEquals(Long.MAX_VALUE, s.getObjectProp("longProp"));
-    assertTrue(s.getObjectProp("floatProp") instanceof Double);
+    assertTrue(s.getObjectProp("floatProp") instanceof Float);
     // float converts to double

Review Comment:
   Remove?



##########
lang/java/avro/src/test/java/org/apache/avro/TestSchemaBuilder.java:
##########
@@ -141,14 +142,14 @@ void fieldObjectProps() {
     assertTrue(f.getObjectProp("intProp") instanceof Integer);
     assertTrue(f.getObjectProp("longProp") instanceof Long);
     assertEquals(Long.MAX_VALUE, f.getObjectProp("longProp"));
-    assertTrue(f.getObjectProp("floatProp") instanceof Double);
+    assertTrue(f.getObjectProp("floatProp") instanceof Float);
     // float converts to double

Review Comment:
   Remove?



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


Re: [PR] Avro 3876 jackson util [avro]

Posted by "clesaec (via GitHub)" <gi...@apache.org>.
clesaec commented on code in PR #2529:
URL: https://github.com/apache/avro/pull/2529#discussion_r1342548625


##########
lang/java/avro/src/test/java/org/apache/avro/TestSchemaBuilder.java:
##########
@@ -111,14 +112,14 @@ void objectProps() {
     assertTrue(s.getObjectProp("intProp") instanceof Integer);
     assertTrue(s.getObjectProp("longProp") instanceof Long);
     assertEquals(Long.MAX_VALUE, s.getObjectProp("longProp"));
-    assertTrue(s.getObjectProp("floatProp") instanceof Double);
+    assertTrue(s.getObjectProp("floatProp") instanceof Float);
     // float converts to double

Review Comment:
   ? Double is replaced by Float for "floatProp"; i don't understand the point of removing this line ?



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


Re: [PR] Avro 3876 jackson util [avro]

Posted by "clesaec (via GitHub)" <gi...@apache.org>.
clesaec commented on code in PR #2529:
URL: https://github.com/apache/avro/pull/2529#discussion_r1342569622


##########
lang/java/avro/src/main/java/org/apache/avro/util/internal/JacksonUtils.java:
##########
@@ -136,10 +137,23 @@ public static Object toObject(JsonNode jsonNode, Schema schema) {
         return jsonNode.asDouble();
       }
     } else if (jsonNode.isDouble() || jsonNode.isFloat()) {
-      if (schema == null || schema.getType().equals(Schema.Type.DOUBLE)) {
-        return jsonNode.asDouble();
-      } else if (schema.getType().equals(Schema.Type.FLOAT)) {
-        return (float) jsonNode.asDouble();
+      if (schema != null) {
+        if (schema.getType().equals(Schema.Type.DOUBLE)) {
+          return jsonNode.doubleValue();
+        } else if (schema.getType().equals(Schema.Type.FLOAT)) {
+          return jsonNode.floatValue();
+        }

Review Comment:
   if there is an error in the data, i guess it's possible.



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


Re: [PR] Avro 3876 jackson util [avro]

Posted by "clesaec (via GitHub)" <gi...@apache.org>.
clesaec commented on code in PR #2529:
URL: https://github.com/apache/avro/pull/2529#discussion_r1342570161


##########
lang/java/avro/src/test/java/org/apache/avro/TestSchemaBuilder.java:
##########
@@ -141,14 +142,14 @@ void fieldObjectProps() {
     assertTrue(f.getObjectProp("intProp") instanceof Integer);
     assertTrue(f.getObjectProp("longProp") instanceof Long);
     assertEquals(Long.MAX_VALUE, f.getObjectProp("longProp"));
-    assertTrue(f.getObjectProp("floatProp") instanceof Double);
+    assertTrue(f.getObjectProp("floatProp") instanceof Float);
     // float converts to double

Review Comment:
   ??



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


Re: [PR] Avro 3876 jackson util [avro]

Posted by "opwvhk (via GitHub)" <gi...@apache.org>.
opwvhk commented on code in PR #2529:
URL: https://github.com/apache/avro/pull/2529#discussion_r1347032473


##########
lang/java/avro/src/test/java/org/apache/avro/TestSchemaBuilder.java:
##########
@@ -111,14 +112,14 @@ void objectProps() {
     assertTrue(s.getObjectProp("intProp") instanceof Integer);
     assertTrue(s.getObjectProp("longProp") instanceof Long);
     assertEquals(Long.MAX_VALUE, s.getObjectProp("longProp"));
-    assertTrue(s.getObjectProp("floatProp") instanceof Double);
+    assertTrue(s.getObjectProp("floatProp") instanceof Float);
     // float converts to double

Review Comment:
   I intended the comment: it's no longer converted, so the comment is outdated.
   This occurred several times in the tests.



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