Skip to content

Commit 14097c6

Browse files
committed
PARQUET-387: Improve NPE message when avro arrays contain null.
Previously, the NPE had no error message but the Avro support accepts schemas that have nullable array elements. Author: Ryan Blue <blue@apache.org> Closes #291 from rdblue/PARQUET-387-fix-npe-message and squashes the following commits: 39d3c83 [Ryan Blue] PARQUET-387: Update test case to verify help message. d6b6bd8 [Ryan Blue] PARQUET-387: Improve NPE message when avro arrays contain null.
1 parent e32aa6f commit 14097c6

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

parquet-avro/src/main/java/org/apache/parquet/avro/AvroWriteSupport.java

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -452,8 +452,24 @@ public void writeCollection(GroupType schema, Schema avroSchema,
452452
Collection<?> array) {
453453
if (array.size() > 0) {
454454
recordConsumer.startField(OLD_LIST_REPEATED_NAME, 0);
455-
for (Object elt : array) {
456-
writeValue(schema.getType(0), avroSchema.getElementType(), elt);
455+
try {
456+
for (Object elt : array) {
457+
writeValue(schema.getType(0), avroSchema.getElementType(), elt);
458+
}
459+
} catch (NullPointerException e) {
460+
// find the null element and throw a better error message
461+
int i = 0;
462+
for (Object elt : array) {
463+
if (elt == null) {
464+
throw new NullPointerException(
465+
"Array contains a null element at " + i + "\n" +
466+
"Set parquet.avro.write-old-list-structure=false to turn " +
467+
"on support for arrays with null elements.");
468+
}
469+
i += 1;
470+
}
471+
// no element was null, throw the original exception
472+
throw e;
457473
}
458474
recordConsumer.endField(OLD_LIST_REPEATED_NAME, 0);
459475
}
@@ -464,8 +480,22 @@ protected void writeObjectArray(GroupType type, Schema schema,
464480
Object[] array) {
465481
if (array.length > 0) {
466482
recordConsumer.startField(OLD_LIST_REPEATED_NAME, 0);
467-
for (Object element : array) {
468-
writeValue(type.getType(0), schema.getElementType(), element);
483+
try {
484+
for (Object element : array) {
485+
writeValue(type.getType(0), schema.getElementType(), element);
486+
}
487+
} catch (NullPointerException e) {
488+
// find the null element and throw a better error message
489+
for (int i = 0; i < array.length; i += 1) {
490+
if (array[i] == null) {
491+
throw new NullPointerException(
492+
"Array contains a null element at " + i + "\n" +
493+
"Set parquet.avro.write-old-list-structure=false to turn " +
494+
"on support for arrays with null elements.");
495+
}
496+
}
497+
// no element was null, throw the original exception
498+
throw e;
469499
}
470500
recordConsumer.endField(OLD_LIST_REPEATED_NAME, 0);
471501
}

parquet-avro/src/test/java/org/apache/parquet/avro/TestReadWriteOldListBehavior.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import org.apache.parquet.io.api.Binary;
4444
import org.apache.parquet.io.api.RecordConsumer;
4545
import org.apache.parquet.schema.MessageTypeParser;
46+
import org.junit.Assert;
4647
import org.junit.Test;
4748
import org.junit.runner.RunWith;
4849
import org.junit.runners.Parameterized;
@@ -347,7 +348,8 @@ public void testArrayWithNullValues() throws Exception {
347348
writer.write(record);
348349
fail("Should not succeed writing an array with null values");
349350
} catch (Exception e) {
350-
// expected
351+
Assert.assertTrue("Error message should provide context and help",
352+
e.getMessage().contains("parquet.avro.write-old-list-structure"));
351353
} finally {
352354
writer.close();
353355
}

0 commit comments

Comments
 (0)