This is an automated email from the ASF dual-hosted git repository.

fokko pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/parquet-mr.git


The following commit(s) were added to refs/heads/master by this push:
     new a2459b6  PARQUET-1728: Simplify NullPointerException Handling in 
AvroWriteSupport (#716)
a2459b6 is described below

commit a2459b68941cd1eefb3169e80004e1af434b2a80
Author: belugabehr <[email protected]>
AuthorDate: Thu May 7 11:57:17 2020 -0400

    PARQUET-1728: Simplify NullPointerException Handling in AvroWriteSupport 
(#716)
    
    * PARQUET-1728: Simplify NullPointerException Handling in AvroWriteSupport
    
    * Fixed issue whereby a Collection was being wrapped and not an Array
    
    Co-authored-by: David Mollitor <[email protected]>
---
 .../org/apache/parquet/avro/AvroWriteSupport.java  | 40 ++++++++++------------
 1 file changed, 19 insertions(+), 21 deletions(-)

diff --git 
a/parquet-avro/src/main/java/org/apache/parquet/avro/AvroWriteSupport.java 
b/parquet-avro/src/main/java/org/apache/parquet/avro/AvroWriteSupport.java
index 859a133..333a13d 100644
--- a/parquet-avro/src/main/java/org/apache/parquet/avro/AvroWriteSupport.java
+++ b/parquet-avro/src/main/java/org/apache/parquet/avro/AvroWriteSupport.java
@@ -19,6 +19,7 @@
 package org.apache.parquet.avro;
 
 import java.nio.ByteBuffer;
+import java.util.Arrays;
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.List;
@@ -542,7 +543,7 @@ public class AvroWriteSupport<T> extends WriteSupport<T> {
     @Override
     public void writeCollection(GroupType schema, Schema avroSchema,
                                 Collection<?> array) {
-      if (array.size() > 0) {
+      if (!array.isEmpty()) {
         recordConsumer.startField(OLD_LIST_REPEATED_NAME, 0);
         try {
           for (Object elt : array) {
@@ -550,18 +551,16 @@ public class AvroWriteSupport<T> extends WriteSupport<T> {
           }
         } catch (NullPointerException e) {
           // find the null element and throw a better error message
-          int i = 0;
-          for (Object elt : array) {
-            if (elt == null) {
-              throw new NullPointerException(
-                  "Array contains a null element at " + i + "\n" +
-                  "Set parquet.avro.write-old-list-structure=false to turn " +
-                  "on support for arrays with null elements.");
-            }
-            i += 1;
+          final int idx =
+              Arrays.asList(array.toArray(new Object[0])).indexOf(null);
+          if (idx < 0) {
+            // no element was null, throw the original exception
+            throw e;
           }
-          // no element was null, throw the original exception
-          throw e;
+          throw new NullPointerException(
+              "Array contains a null element at " + idx + ". "
+                  + "Set parquet.avro.write-old-list-structure=false to turn "
+                  + "on support for arrays with null elements.");
         }
         recordConsumer.endField(OLD_LIST_REPEATED_NAME, 0);
       }
@@ -578,16 +577,15 @@ public class AvroWriteSupport<T> extends WriteSupport<T> {
           }
         } catch (NullPointerException e) {
           // find the null element and throw a better error message
-          for (int i = 0; i < array.length; i += 1) {
-            if (array[i] == null) {
-              throw new NullPointerException(
-                  "Array contains a null element at " + i + "\n" +
-                  "Set parquet.avro.write-old-list-structure=false to turn " +
-                  "on support for arrays with null elements.");
-            }
+          final int idx = Arrays.asList(array).indexOf(null);
+          if (idx < 0) {
+            // no element was null, throw the original exception
+            throw e;
           }
-          // no element was null, throw the original exception
-          throw e;
+          throw new NullPointerException(
+              "Array contains a null element at " + idx + ". " +
+              "Set parquet.avro.write-old-list-structure=false to turn " +
+              "on support for arrays with null elements.");
         }
         recordConsumer.endField(OLD_LIST_REPEATED_NAME, 0);
       }

Reply via email to