pvary commented on code in PR #12298: URL: https://github.com/apache/iceberg/pull/12298#discussion_r2382315320
########## parquet/src/main/java/org/apache/iceberg/parquet/ParquetFormatModel.java: ########## @@ -0,0 +1,105 @@ +/* + * 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.iceberg.parquet; + +import java.util.Map; +import org.apache.iceberg.FileFormat; +import org.apache.iceberg.Schema; +import org.apache.iceberg.io.FormatModel; +import org.apache.iceberg.io.InputFile; +import org.apache.iceberg.io.OutputFile; +import org.apache.parquet.schema.MessageType; + +public class ParquetFormatModel<D, S, F> implements FormatModel<D, S> { + private final Class<D> type; + private final ReaderFunction<D> readerFunction; + private final BatchReaderFunction<D, F> batchReaderFunction; + private final WriterFunction<D, S> writerFunction; + + private ParquetFormatModel( + Class<D> type, + ReaderFunction<D> readerFunction, + BatchReaderFunction<D, F> batchReaderFunction, + WriterFunction<D, S> writerFunction) { + this.type = type; + this.readerFunction = readerFunction; + this.batchReaderFunction = batchReaderFunction; + this.writerFunction = writerFunction; + } + + public ParquetFormatModel(Class<D> type) { + this(type, null, null, null); + } + + public ParquetFormatModel( + Class<D> type, ReaderFunction<D> readerFunction, WriterFunction<D, S> writerFunction) { + this(type, readerFunction, null, writerFunction); + } + + public ParquetFormatModel(Class<D> type, BatchReaderFunction<D, F> batchReaderFunction) { + this(type, null, batchReaderFunction, null); + } + + @Override + public FileFormat format() { + return FileFormat.PARQUET; + } + + @Override + public Class<D> type() { + return type; + } + + @Override + public org.apache.iceberg.io.WriteBuilder<D, S> writeBuilder(OutputFile outputFile) { + return new Parquet.WriteBuilderImpl<D, S>(outputFile).writerFunction(writerFunction); + } + + @Override + public org.apache.iceberg.io.ReadBuilder<D, S> readBuilder(InputFile inputFile) { + if (batchReaderFunction != null) { + return new Parquet.ReadBuilderImpl<D, S, F>(inputFile) + .batchReaderFunction(batchReaderFunction); + } else { + return new Parquet.ReadBuilderImpl<D, S, F>(inputFile).readerFunction(readerFunction); + } + } + + public interface ReaderFunction<D> { Review Comment: Done ########## data/src/main/java/org/apache/iceberg/data/GenericFormatModels.java: ########## @@ -0,0 +1,94 @@ +/* + * 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.iceberg.data; + +import org.apache.iceberg.avro.AvroFormatModel; +import org.apache.iceberg.data.avro.DataWriter; +import org.apache.iceberg.data.avro.PlannedDataReader; +import org.apache.iceberg.data.orc.GenericOrcReader; +import org.apache.iceberg.data.orc.GenericOrcWriter; +import org.apache.iceberg.data.parquet.GenericParquetReaders; +import org.apache.iceberg.data.parquet.GenericParquetWriter; +import org.apache.iceberg.deletes.PositionDelete; +import org.apache.iceberg.orc.ORCFormatModel; +import org.apache.iceberg.parquet.ParquetFormatModel; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class GenericFormatModels { + private static final Logger LOG = LoggerFactory.getLogger(GenericFormatModels.class); + + public static void register() { + // ORC, Parquet are optional dependencies. If they are not present, we should just log and + // ignore NoClassDefFoundErrors + registerAvro(); + registerParquet(); + registerOrc(); + } + + private static void registerParquet() { + logAngIgnoreNoClassDefFoundError( + () -> + FormatModelRegistry.register( + new ParquetFormatModel<>( + Record.class, + GenericParquetReaders::buildReader, + (schema, messageType, inputType) -> + GenericParquetWriter.create(schema, messageType)))); + logAngIgnoreNoClassDefFoundError( + () -> FormatModelRegistry.register(new ParquetFormatModel<>(PositionDelete.class))); + } + + private static void registerAvro() { + logAngIgnoreNoClassDefFoundError( + () -> + FormatModelRegistry.register( + new AvroFormatModel<>( + Record.class, + PlannedDataReader::create, + (schema, inputSchema) -> DataWriter.create(schema)))); + logAngIgnoreNoClassDefFoundError( + () -> FormatModelRegistry.register(new AvroFormatModel<>(PositionDelete.class))); + } + + private static void registerOrc() { + logAngIgnoreNoClassDefFoundError( + () -> + FormatModelRegistry.register( + new ORCFormatModel<>( + Record.class, + GenericOrcReader::buildReader, + (schema, typeDescription, unused) -> + GenericOrcWriter.buildWriter(schema, typeDescription)))); + logAngIgnoreNoClassDefFoundError( + () -> FormatModelRegistry.register(new ORCFormatModel<>(PositionDelete.class))); + } + + private GenericFormatModels() {} + + @SuppressWarnings("CatchBlockLogException") + private static void logAngIgnoreNoClassDefFoundError(Runnable runnable) { + try { + runnable.run(); + } catch (NoClassDefFoundError e) { + // Log the exception and ignore it + LOG.info("Exception occurred when trying to register object models: {}", e.getMessage()); Review Comment: Done -- 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]
