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/17 12:37:32 UTC

[GitHub] [avro] clesaec opened a new pull request, #1688: AVRO-3374: special cases for qualified name

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

   Make sure you have checked _all_ steps below.
   
   ### Jira
   
   - [x] My PR addresses the following [Avro Jira](https://issues.apache.org/jira/browse/AVRO/) issues and references them in the PR title. For example, "AVRO-1234: My Avro PR"
     - https://issues.apache.org/jira/browse/AVRO-XXX
     - In case you are adding a dependency, check if the license complies with the [ASF 3rd Party License Policy](https://www.apache.org/legal/resolved.html#category-x).
   
   ### Tests
   
   - [x] My PR adds the following unit tests TestSchema.testParseRecordWithNameAsType and TestSchema.testQualifiedName
   
   ### 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
   
   - [ ] 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 (no adding functionality nor modified public classes)
     
   


-- 
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] clesaec commented on a diff in pull request #1688: AVRO-3374: special cases for qualified name

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


##########
lang/java/avro/src/main/java/org/apache/avro/Schema.java:
##########
@@ -751,8 +751,32 @@ public void writeName(Names names, JsonGenerator gen) throws IOException {
     }
 
     public String getQualified(String defaultSpace) {
-      return (space == null || space.equals(defaultSpace)) ? name : full;
+      return this.shouldWriteFull(defaultSpace) ? full : name;
     }
+
+    /**
+     * Determine if full name must be written. There are 2 cases for true :
+     * defaultSpace != from this.space or name is already a Schema.Type (int, array
+     * ...)
+     *
+     * @param defaultSpace : default name space.
+     * @return true if full name must be written.
+     */
+    private boolean shouldWriteFull(String defaultSpace) {
+      if (space != null && space.equals(defaultSpace)) {
+        try {
+          // name is a 'Type', so namespace must be written (int should return true, Int
+          // should return false).
+          return Type.valueOf(this.name.toUpperCase(Locale.ENGLISH)).name.equals(this.name);
+        } catch (IllegalArgumentException ex) {
+          // namespace can be omitted, (default & name is not a type)
+          return false;
+        }

Review Comment:
   Well, Type.valueOf use an internal hashmap (see Class.enumConstantDirectory method), that will have a direct access to Type values ...
   And, as reminded [here](https://medium.com/javarevisited/micro-optimizations-in-java-good-nice-and-slow-enum-261e6f77bd2e), Type.values() method will build a clone of all values.
   So, the code i proposed will be faster when exist (true return without exception), but i don't know for exception creation vs Type.values() with clone.
   Tell me what you think, i can change code.



-- 
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] clesaec commented on a diff in pull request #1688: AVRO-3374: special cases for qualified name

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


##########
lang/java/avro/src/main/java/org/apache/avro/Schema.java:
##########
@@ -751,8 +751,32 @@ public void writeName(Names names, JsonGenerator gen) throws IOException {
     }
 
     public String getQualified(String defaultSpace) {
-      return (space == null || space.equals(defaultSpace)) ? name : full;
+      return this.shouldWriteFull(defaultSpace) ? full : name;
     }
+
+    /**
+     * Determine if full name must be written. There are 2 cases for true :
+     * defaultSpace != from this.space or name is already a Schema.Type (int, array
+     * ...)
+     *
+     * @param defaultSpace : default name space.
+     * @return true if full name must be written.
+     */
+    private boolean shouldWriteFull(String defaultSpace) {
+      if (space != null && space.equals(defaultSpace)) {
+        try {
+          // name is a 'Type', so namespace must be written (int should return true, Int
+          // should return false).
+          return Type.valueOf(this.name.toUpperCase(Locale.ENGLISH)).name.equals(this.name);
+        } catch (IllegalArgumentException ex) {
+          // namespace can be omitted, (default & name is not a type)
+          return false;
+        }

Review Comment:
   But anyway, for 14 values, i think you're right, i updated the code.



-- 
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] clesaec commented on pull request #1688: AVRO-3374: special cases for qualified name

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

   @RyanSkraba : friendly reminder, if you have time to see this PR.


-- 
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] RyanSkraba merged pull request #1688: AVRO-3374: special cases for qualified name

Posted by GitBox <gi...@apache.org>.
RyanSkraba merged PR #1688:
URL: https://github.com/apache/avro/pull/1688


-- 
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] opwvhk commented on a diff in pull request #1688: AVRO-3374: special cases for qualified name

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


##########
lang/java/avro/src/main/java/org/apache/avro/Schema.java:
##########
@@ -751,8 +751,32 @@ public void writeName(Names names, JsonGenerator gen) throws IOException {
     }
 
     public String getQualified(String defaultSpace) {
-      return (space == null || space.equals(defaultSpace)) ? name : full;
+      return this.shouldWriteFull(defaultSpace) ? full : name;
     }
+
+    /**
+     * Determine if full name must be written. There are 2 cases for true :
+     * defaultSpace != from this.space or name is already a Schema.Type (int, array
+     * ...)
+     *
+     * @param defaultSpace : default name space.
+     * @return true if full name must be written.
+     */
+    private boolean shouldWriteFull(String defaultSpace) {
+      if (space != null && space.equals(defaultSpace)) {
+        try {
+          // name is a 'Type', so namespace must be written (int should return true, Int
+          // should return false).
+          return Type.valueOf(this.name.toUpperCase(Locale.ENGLISH)).name.equals(this.name);
+        } catch (IllegalArgumentException ex) {
+          // namespace can be omitted, (default & name is not a type)
+          return false;
+        }

Review Comment:
   This code catches the exception as flow control, which is quite expensive. I think we should prefer a comparison loop instead:
   ```
         for (Type type : Type.values()) {
           if (type.name.equals(name)) {
             return true;
           }
         }
         return false;
   ```



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