Zouxxyy commented on code in PR #7035: URL: https://github.com/apache/paimon/pull/7035#discussion_r2688668193
########## paimon-format/src/main/java/org/apache/paimon/format/parquet/writer/InferVariantShreddingParquetWriter.java: ########## @@ -0,0 +1,110 @@ +/* + * 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.paimon.format.parquet.writer; + +import org.apache.paimon.data.InternalRow; +import org.apache.paimon.data.variant.InferVariantShreddingSchema; +import org.apache.paimon.format.FormatWriter; +import org.apache.paimon.types.RowType; + +import org.apache.parquet.hadoop.ParquetWriter; +import org.apache.parquet.io.OutputFile; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +/** + * A Parquet writer that infers the shredding schema from buffered rows before writing. + * + * <p>This writer buffers rows up to a threshold, infers the optimal schema from them, then writes + * all data using the inferred schema. + */ +public class InferVariantShreddingParquetWriter implements FormatWriter { + + private final OutputFile outputFile; + private final String compression; + private final ParquetBuilder<InternalRow> writerBuilder; + private final InferVariantShreddingSchema shreddingSchemaInfer; + private final int maxBufferRow; + private final List<InternalRow> bufferedRows; + + private ParquetWriter<InternalRow> writer; + + public InferVariantShreddingParquetWriter( + OutputFile outputFile, + String compression, + ParquetBuilder<InternalRow> writerBuilder, + InferVariantShreddingSchema shreddingSchemaInfer, + int maxBufferRow) { + this.outputFile = outputFile; + this.compression = compression; + this.writerBuilder = writerBuilder; + this.shreddingSchemaInfer = shreddingSchemaInfer; + this.maxBufferRow = maxBufferRow; + this.bufferedRows = new ArrayList<>(); + } + + @Override + public void addElement(InternalRow row) throws IOException { + if (writer == null) { + bufferedRows.add(row); Review Comment: Not sure if a copy of the row is needed here; at least, end-to-end Spark tests show it's not required. -- 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]
