From b8c7c1563a431eda720fa47cc71027a1f856f3df Mon Sep 17 00:00:00 2001 From: Timothy Miller Date: Thu, 14 Apr 2022 11:44:34 -0400 Subject: [PATCH 1/5] Fix PARQUET-2069: Allow list and array record types to be compatible. --- .../apache/parquet/avro/AvroRecordConverter.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/parquet-avro/src/main/java/org/apache/parquet/avro/AvroRecordConverter.java b/parquet-avro/src/main/java/org/apache/parquet/avro/AvroRecordConverter.java index ea5b907157..4a50b8018f 100644 --- a/parquet-avro/src/main/java/org/apache/parquet/avro/AvroRecordConverter.java +++ b/parquet-avro/src/main/java/org/apache/parquet/avro/AvroRecordConverter.java @@ -866,6 +866,20 @@ static boolean isElementType(Type repeatedType, Schema elementSchema) { } else if (elementSchema != null && elementSchema.getType() == Schema.Type.RECORD) { Schema schemaFromRepeated = CONVERTER.convert(repeatedType.asGroupType()); + + // Fix for PARQUET-2069 + // ParquetMR breaks compatibility with itself by including a JSON + // representation of a schema that names a record "list", when + // it should be named "array" to match with the rest of the metadata. + // Inserting this code allows Avro to detect that the "array" and "list" + // types are compatible. Since this alias is being added to something + // that is the result of parsing JSON, we can't add the alias at the + // time of construction. Therefore we have to do it here where the the data + // structures have been unwrapped to the point where we have the + // incompatible structure and can add the necessary alias. + if (elementSchema.getName().equals("list")) elementSchema.addAlias("array", ""); + if (elementSchema.getName().equals("array")) elementSchema.addAlias("list", ""); + if (checkReaderWriterCompatibility(elementSchema, schemaFromRepeated) .getType() == COMPATIBLE) { return true; From 6c9f2d4f825dd0e76bfe0254d8cf2b29270da8ff Mon Sep 17 00:00:00 2001 From: "Timothy N. Miller" <94654785+theosib-amazon@users.noreply.github.com> Date: Wed, 11 May 2022 11:48:31 -0400 Subject: [PATCH 2/5] Update AvroRecordConverter.java Changed formatting of IF statements. --- .../java/org/apache/parquet/avro/AvroRecordConverter.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/parquet-avro/src/main/java/org/apache/parquet/avro/AvroRecordConverter.java b/parquet-avro/src/main/java/org/apache/parquet/avro/AvroRecordConverter.java index 4a50b8018f..93c902db12 100644 --- a/parquet-avro/src/main/java/org/apache/parquet/avro/AvroRecordConverter.java +++ b/parquet-avro/src/main/java/org/apache/parquet/avro/AvroRecordConverter.java @@ -877,8 +877,12 @@ static boolean isElementType(Type repeatedType, Schema elementSchema) { // time of construction. Therefore we have to do it here where the the data // structures have been unwrapped to the point where we have the // incompatible structure and can add the necessary alias. - if (elementSchema.getName().equals("list")) elementSchema.addAlias("array", ""); - if (elementSchema.getName().equals("array")) elementSchema.addAlias("list", ""); + if (elementSchema.getName().equals("list")) { + elementSchema.addAlias("array", ""); + } + if (elementSchema.getName().equals("array")) { + elementSchema.addAlias("list", ""); + } if (checkReaderWriterCompatibility(elementSchema, schemaFromRepeated) .getType() == COMPATIBLE) { From 325fa1c9a3c74a728c807d28e958d4b640d85eee Mon Sep 17 00:00:00 2001 From: Tim Miller Date: Mon, 16 May 2022 15:25:08 +0000 Subject: [PATCH 3/5] Fix PARQUET-2069 and related bugs Sometimes the avro schema is bad. This fix will attempt to use the avro schema, but if that fails, it'll fall back to converting the parquet schema. --- .../apache/parquet/avro/AvroReadSupport.java | 19 +++++-- .../parquet/avro/AvroRecordConverter.java | 18 ------- .../avro/TestArrayListCompatibility.java | 51 ++++++++++++++++++ .../test/resources/list-array-compat.parquet | Bin 0 -> 4411 bytes 4 files changed, 67 insertions(+), 21 deletions(-) create mode 100644 parquet-avro/src/test/java/org/apache/parquet/avro/TestArrayListCompatibility.java create mode 100644 parquet-avro/src/test/resources/list-array-compat.parquet diff --git a/parquet-avro/src/main/java/org/apache/parquet/avro/AvroReadSupport.java b/parquet-avro/src/main/java/org/apache/parquet/avro/AvroReadSupport.java index eca14413a6..577edaa8a1 100644 --- a/parquet-avro/src/main/java/org/apache/parquet/avro/AvroReadSupport.java +++ b/parquet-avro/src/main/java/org/apache/parquet/avro/AvroReadSupport.java @@ -25,6 +25,7 @@ import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.util.ReflectionUtils; import org.apache.parquet.hadoop.api.ReadSupport; +import org.apache.parquet.io.InvalidRecordException; import org.apache.parquet.io.api.RecordMaterializer; import org.apache.parquet.schema.MessageType; @@ -136,10 +137,22 @@ public RecordMaterializer prepareForRead( GenericData model = getDataModel(configuration); String compatEnabled = metadata.get(AvroReadSupport.AVRO_COMPATIBILITY); - if (compatEnabled != null && Boolean.valueOf(compatEnabled)) { - return newCompatMaterializer(parquetSchema, avroSchema, model); + + try { + if (compatEnabled != null && Boolean.valueOf(compatEnabled)) { + return newCompatMaterializer(parquetSchema, avroSchema, model); + } + return new AvroRecordMaterializer(parquetSchema, avroSchema, model); + } catch (InvalidRecordException | ClassCastException e) { + System.err.println("Warning, Avro schema doesn't match Parquet schema, falling back to conversion: " + e.toString()); + // If the Avro schema is bad, fall back to reconstructing it from the Parquet schema + avroSchema = new AvroSchemaConverter(configuration).convert(parquetSchema); + + if (compatEnabled != null && Boolean.valueOf(compatEnabled)) { + return newCompatMaterializer(parquetSchema, avroSchema, model); + } + return new AvroRecordMaterializer(parquetSchema, avroSchema, model); } - return new AvroRecordMaterializer(parquetSchema, avroSchema, model); } @SuppressWarnings("unchecked") diff --git a/parquet-avro/src/main/java/org/apache/parquet/avro/AvroRecordConverter.java b/parquet-avro/src/main/java/org/apache/parquet/avro/AvroRecordConverter.java index 93c902db12..ea5b907157 100644 --- a/parquet-avro/src/main/java/org/apache/parquet/avro/AvroRecordConverter.java +++ b/parquet-avro/src/main/java/org/apache/parquet/avro/AvroRecordConverter.java @@ -866,24 +866,6 @@ static boolean isElementType(Type repeatedType, Schema elementSchema) { } else if (elementSchema != null && elementSchema.getType() == Schema.Type.RECORD) { Schema schemaFromRepeated = CONVERTER.convert(repeatedType.asGroupType()); - - // Fix for PARQUET-2069 - // ParquetMR breaks compatibility with itself by including a JSON - // representation of a schema that names a record "list", when - // it should be named "array" to match with the rest of the metadata. - // Inserting this code allows Avro to detect that the "array" and "list" - // types are compatible. Since this alias is being added to something - // that is the result of parsing JSON, we can't add the alias at the - // time of construction. Therefore we have to do it here where the the data - // structures have been unwrapped to the point where we have the - // incompatible structure and can add the necessary alias. - if (elementSchema.getName().equals("list")) { - elementSchema.addAlias("array", ""); - } - if (elementSchema.getName().equals("array")) { - elementSchema.addAlias("list", ""); - } - if (checkReaderWriterCompatibility(elementSchema, schemaFromRepeated) .getType() == COMPATIBLE) { return true; diff --git a/parquet-avro/src/test/java/org/apache/parquet/avro/TestArrayListCompatibility.java b/parquet-avro/src/test/java/org/apache/parquet/avro/TestArrayListCompatibility.java new file mode 100644 index 0000000000..1eff1015d5 --- /dev/null +++ b/parquet-avro/src/test/java/org/apache/parquet/avro/TestArrayListCompatibility.java @@ -0,0 +1,51 @@ +/** + * 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.generic.GenericData; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.fs.Path; +import org.apache.parquet.hadoop.ParquetReader; +import org.junit.Test; +import java.io.IOException; + +public class TestArrayListCompatibility { + + @Test + public void testListArrayCompatibility() throws IOException { + Path testPath = new Path(Resources.getResource("list-array-compat.parquet").getFile()); + + Configuration conf = new Configuration(); + ParquetReader parquetReader = + AvroParquetReader.builder(testPath).withConf(conf).build(); + GenericData.Record firstRecord; + try { + firstRecord = (GenericData.Record) parquetReader.read(); + } catch (Exception x) { + x.printStackTrace(); + throw x; + } + if (firstRecord == null) { + throw new IOException("Can't process empty Parquet file"); + } + firstRecord.getSchema().toString(true); + } + +} diff --git a/parquet-avro/src/test/resources/list-array-compat.parquet b/parquet-avro/src/test/resources/list-array-compat.parquet new file mode 100644 index 0000000000000000000000000000000000000000..e68ff0c33fa779bf507c8603f44fa078599da961 GIT binary patch literal 4411 zcmdT|TWAzl7(P3**{sQ$80{HmV4=f;TdUJecC*=RWR=F)wbf{1tTs_ZW;2sy$ljct zjiw@~SlC+eA(hexp->7fLQ|1KOR5jG_My_JK9)R4pQQDnE!JKL*#FFBcifr8tYD#; z$(}R+`Okm({>vF=xPK&y5&n@P_t~#5zZ}E?c=ll*LKH%X-cFO>N9_U(h0)HFlRJw0 zy5jv>Z}E+(?0BNHcY3rd|C+RGS26y|?t!t`WclFX&Y7`o82E>$l&mPp+2WKU3jA=t zz#lx&&%nX~F|UdI4pGgBDy$3`D{Zi{R}piHz^4aw-gcn$mrI(G7x>plb<%S{+9wu8 zexOj!YqQnOVDwJ77QXo479GUYV?H3=2O4#dz48fute{Q{{Jt?=AaGvwko&vEn@jCB z_w`Mmv^2pcx>BhiKm^5Ny?jn-hFM=cl7Ok75QSuK(ws+ySkj_Lh2D6~#uJ4(XglK} z_h)zBx!1gcwkz)v#R#1pAz?u1t@*S*zA*Q^J_f(ttdBpPG3eNfA9%=p=jNZkHKlM1 z_98^^>*l8lWwpfj#6f+&MB;d;dth+O=wL$Gn;6+KbEtD_q%+=`N$)!lBG`<>PS8)W<=8qvVV!nO4G+usDNq=JYOA+*O3#dS~|+g_KX94jfC! zo5LQ8Q5hAsq+E*6g~Xsu&LVZ;v)lF&vF*J!oI&C9UdyzKo)wRqQ;mKd*^06rT}&{9 zN?G)PGyk88*zGm~q0JfxV16i5rtH3vyvHPYPJy55L50ge+d zX$D9i5c4yM;P35t4zjD#@>D3ACOc59v;c}$BW0`Im7wUP+EF~S0Z=^OK~TKkYN2>? z^;u3ZbYGpf5(+(QgWBONr06FAVEo{(oA|JpT zCm<4MV*iCV(J#hva1|q*e|3Ta9yLw%}e^E7X-_HfD#gW%I(e!Zdr_vI-8JNV_GF! zEn Date: Mon, 20 Jun 2022 10:33:02 -0400 Subject: [PATCH 4/5] Create TestArrayListCompatibility.java Allow any exception from parquetReader.read() propagate up without intermediate catch. --- .../apache/parquet/avro/TestArrayListCompatibility.java | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/parquet-avro/src/test/java/org/apache/parquet/avro/TestArrayListCompatibility.java b/parquet-avro/src/test/java/org/apache/parquet/avro/TestArrayListCompatibility.java index 1eff1015d5..f565c25cda 100644 --- a/parquet-avro/src/test/java/org/apache/parquet/avro/TestArrayListCompatibility.java +++ b/parquet-avro/src/test/java/org/apache/parquet/avro/TestArrayListCompatibility.java @@ -36,12 +36,7 @@ public void testListArrayCompatibility() throws IOException { ParquetReader parquetReader = AvroParquetReader.builder(testPath).withConf(conf).build(); GenericData.Record firstRecord; - try { - firstRecord = (GenericData.Record) parquetReader.read(); - } catch (Exception x) { - x.printStackTrace(); - throw x; - } + firstRecord = (GenericData.Record) parquetReader.read(); if (firstRecord == null) { throw new IOException("Can't process empty Parquet file"); } From 552f9efd0457dc1aa7892753015830f99981c925 Mon Sep 17 00:00:00 2001 From: "Timothy N. Miller" <94654785+theosib-amazon@users.noreply.github.com> Date: Mon, 20 Jun 2022 10:39:03 -0400 Subject: [PATCH 5/5] Update AvroReadSupport.java Use log4j --- .../main/java/org/apache/parquet/avro/AvroReadSupport.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/parquet-avro/src/main/java/org/apache/parquet/avro/AvroReadSupport.java b/parquet-avro/src/main/java/org/apache/parquet/avro/AvroReadSupport.java index 577edaa8a1..dce9c8ea37 100644 --- a/parquet-avro/src/main/java/org/apache/parquet/avro/AvroReadSupport.java +++ b/parquet-avro/src/main/java/org/apache/parquet/avro/AvroReadSupport.java @@ -28,6 +28,7 @@ import org.apache.parquet.io.InvalidRecordException; import org.apache.parquet.io.api.RecordMaterializer; import org.apache.parquet.schema.MessageType; +import org.apache.log4j.Logger; /** * Avro implementation of {@link ReadSupport} for avro generic, specific, and @@ -37,6 +38,7 @@ * @param the Java type of records created by this ReadSupport */ public class AvroReadSupport extends ReadSupport { + static Logger log = Logger.getLogger(AvroReadSupport.class.getName()); public static String AVRO_REQUESTED_PROJECTION = "parquet.avro.projection"; private static final String AVRO_READ_SCHEMA = "parquet.avro.read.schema"; @@ -144,7 +146,7 @@ public RecordMaterializer prepareForRead( } return new AvroRecordMaterializer(parquetSchema, avroSchema, model); } catch (InvalidRecordException | ClassCastException e) { - System.err.println("Warning, Avro schema doesn't match Parquet schema, falling back to conversion: " + e.toString()); + log.error("Warning, Avro schema doesn't match Parquet schema, falling back to conversion: ", e); // If the Avro schema is bad, fall back to reconstructing it from the Parquet schema avroSchema = new AvroSchemaConverter(configuration).convert(parquetSchema);