PARQUET-2051: Pass Configuration to AvroSchemaConverter as to not lose options (#912)
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 82a80d3..a407e1b 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
@@ -127,7 +127,7 @@
public WriteContext init(Configuration configuration) {
if (rootAvroSchema == null) {
this.rootAvroSchema = new Schema.Parser().parse(configuration.get(AVRO_SCHEMA));
- this.rootSchema = new AvroSchemaConverter().convert(rootAvroSchema);
+ this.rootSchema = new AvroSchemaConverter(configuration).convert(rootAvroSchema);
}
if (model == null) {
diff --git a/parquet-avro/src/test/java/org/apache/parquet/avro/TestAvroWriteSupport.java b/parquet-avro/src/test/java/org/apache/parquet/avro/TestAvroWriteSupport.java
new file mode 100644
index 0000000..106f859
--- /dev/null
+++ b/parquet-avro/src/test/java/org/apache/parquet/avro/TestAvroWriteSupport.java
@@ -0,0 +1,61 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.parquet.avro;
+
+import com.google.common.io.Resources;
+import org.apache.avro.Schema;
+import org.apache.avro.generic.GenericRecord;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.parquet.column.ColumnDescriptor;
+import org.apache.parquet.hadoop.api.WriteSupport;
+import org.apache.parquet.schema.MessageType;
+import org.apache.parquet.schema.PrimitiveType;
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.io.IOException;
+
+public class TestAvroWriteSupport {
+
+ @Test
+ public void testInitConfiguration() throws IOException {
+ final String listField = "NullableList";
+ Schema schema = new Schema.Parser().parse(Resources.getResource("list_with_nulls.avsc").openStream());
+ AvroWriteSupport<GenericRecord> awsWithConfigSpecificBehavior = new AvroWriteSupport<>();
+ Configuration configuration = new Configuration();
+ AvroWriteSupport.setSchema(configuration, schema);
+ configuration.set(AvroWriteSupport.WRITE_OLD_LIST_STRUCTURE, "false");
+ WriteSupport.WriteContext writeContext = awsWithConfigSpecificBehavior.init(configuration);
+ MessageType actual = writeContext.getSchema();
+ int idx = actual.getFieldIndex(listField);
+ ColumnDescriptor columnDescriptor = actual.getColumns().get(idx);
+ PrimitiveType primitiveType = columnDescriptor.getPrimitiveType();
+ Assert.assertEquals("Confirm that old list type was not used", AvroWriteSupport.LIST_ELEMENT_NAME, primitiveType.getName());
+ // Default configuration
+ configuration = new Configuration();
+ AvroWriteSupport.setSchema(configuration, schema);
+ AvroWriteSupport<GenericRecord> awsWithoutConfigBehavior = new AvroWriteSupport<>();
+ WriteSupport.WriteContext writeContextDefault = awsWithoutConfigBehavior.init(configuration);
+ actual = writeContextDefault.getSchema();
+ idx = actual.getFieldIndex(listField);
+ columnDescriptor = actual.getColumns().get(idx);
+ primitiveType = columnDescriptor.getPrimitiveType();
+ Assert.assertEquals("Confirm that old list type was used if not specified", "array", primitiveType.getName());
+ }
+}
diff --git a/parquet-avro/src/test/resources/list_with_nulls.avsc b/parquet-avro/src/test/resources/list_with_nulls.avsc
new file mode 100644
index 0000000..179787e
--- /dev/null
+++ b/parquet-avro/src/test/resources/list_with_nulls.avsc
@@ -0,0 +1,25 @@
+{
+ "type": "record",
+ "name": "NullLists",
+ "namespace": "com.test",
+ "fields": [
+ {
+ "name": "KeyID",
+ "type": "string"
+ },
+ {
+ "name": "NullableList",
+ "type": [
+ "null",
+ {
+ "type": "array",
+ "items": [
+ "null",
+ "string"
+ ]
+ }
+ ],
+ "default": null
+ }
+ ]
+}