SavicStefan commented on code in PR #59669:
URL: https://github.com/apache/spark/pull/59669#discussion_r4597794224


##########
common/variant/src/main/java/org/apache/spark/types/variant/VariantBuilder.java:
##########
@@ -465,8 +694,13 @@ public void finishWritingObject(int start, 
ArrayList<FieldEntry> fields) {
     } else {
       for (int i = 1; i < size; ++i) {
         maxId = Math.max(maxId, fields.get(i).id);
-        String key = fields.get(i).key;
-        if (key.equals(fields.get(i - 1).key)) {
+        FieldEntry field = fields.get(i);
+        FieldEntry previous = fields.get(i - 1);
+        boolean duplicate = field.key == null ? field.id == previous.id :
+            field.key.equals(previous.key);
+        if (duplicate) {
+          String key = field.key != null ? field.key :
+              new String(field.keyBytes, StandardCharsets.UTF_8);

Review Comment:
   The previous code was correct, I just chose to keep the keys as bytes during 
canonicalization to avoid decoding and allocating a String for each fields, 
because like identical keys map to the same canonical ID, with that  we can 
detect duplicates by comparing those IDs and only decode the name for the error 
message.
   This introduces the key == null case, so the shared duplicate check needs to 
handle it, this is just the optimization that I think its really worth to do.



##########
common/variant/src/main/java/org/apache/spark/types/variant/VariantBuilder.java:
##########
@@ -465,8 +694,13 @@ public void finishWritingObject(int start, 
ArrayList<FieldEntry> fields) {
     } else {
       for (int i = 1; i < size; ++i) {
         maxId = Math.max(maxId, fields.get(i).id);
-        String key = fields.get(i).key;
-        if (key.equals(fields.get(i - 1).key)) {
+        FieldEntry field = fields.get(i);
+        FieldEntry previous = fields.get(i - 1);
+        boolean duplicate = field.key == null ? field.id == previous.id :
+            field.key.equals(previous.key);
+        if (duplicate) {
+          String key = field.key != null ? field.key :
+              new String(field.keyBytes, StandardCharsets.UTF_8);

Review Comment:
   The previous code was correct, I just chose to keep the keys as bytes during 
canonicalization to avoid decoding and allocating a String for each fields, 
because like identical keys map to the same canonical ID, with that  we can 
detect duplicates by comparing those IDs and only decode the name for the error 
message.
   This introduces the `key == null` case, so the shared duplicate check needs 
to handle it, this is just the optimization that I think its really worth to do.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to