pvary commented on code in PR #17723: URL: https://github.com/apache/iceberg/pull/17723#discussion_r3852230067
########## arrow/src/main/java/org/apache/iceberg/arrow/vectorized/ListVectorBuilder.java: ########## @@ -0,0 +1,254 @@ +/* + * 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.arrow.vectorized; + +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import org.apache.arrow.memory.BufferAllocator; +import org.apache.arrow.vector.BaseVariableWidthVector; +import org.apache.arrow.vector.BigIntVector; +import org.apache.arrow.vector.FieldVector; +import org.apache.arrow.vector.FixedSizeBinaryVector; +import org.apache.arrow.vector.Float4Vector; +import org.apache.arrow.vector.Float8Vector; +import org.apache.arrow.vector.IntVector; +import org.apache.arrow.vector.complex.ListVector; +import org.apache.iceberg.arrow.ArrowSchemaUtil; +import org.apache.iceberg.parquet.ParquetUtil; +import org.apache.iceberg.types.Types; +import org.apache.parquet.column.Dictionary; +import org.apache.parquet.schema.LogicalTypeAnnotation; +import org.apache.parquet.schema.PrimitiveType; + +/** + * Owns the Arrow {@link ListVector} construction state for {@link VectorizedListReader}. + * + * <p>Encapsulates allocation and per-list open/close semantics: {@link + * ListVector#startNewValue(int)}, {@link ListVector#endValue(int, int)}, {@link + * ListVector#setNull(int)}, plus the companion {@link IntVector} of list repetition levels and the + * {@link NullabilityHolder} that tracks list-level nullability for the batch. + * + * <p>Isolating this state gives us a clean seam to swap the per-row {@code startNewValue} / {@code + * endValue} protocol for a bulk-fill implementation that writes the offsets buffer directly from + * Parquet rep/def-level arrays. + * + * <p>Not thread-safe. + */ +class ListVectorBuilder implements AutoCloseable { + + private final Types.NestedField icebergField; + private final BufferAllocator allocator; + private final int definitionLevel; + private final boolean isListRequired; + private final boolean isElementRequired; + + private ListVector listVector; + private IntVector listRepetitionLevels; + private NullabilityHolder nullabilityHolder; + + // Per-batch state; reset by prepareBatch(). + private int listIndex; + private int listSize; + private int listDefinitionLevel; + private int elementIndex; + + ListVectorBuilder( + Types.NestedField icebergField, + BufferAllocator allocator, + int definitionLevel, + boolean isListRequired, + boolean isElementRequired) { + this.icebergField = icebergField; + this.allocator = allocator; + this.definitionLevel = definitionLevel; + this.isListRequired = isListRequired; + this.isElementRequired = isElementRequired; + } + + /** + * Prepares the builder for a new batch. Resets or reallocates the underlying Arrow vectors and + * the {@link NullabilityHolder}, and resets all per-batch counters. + * + * @param numRowsToRead upper bound on top-level list rows this batch may produce + * @param estimatedSize estimated total number of lists in this batch (from the Parquet + * repetition-level histogram); used to size the nullability holder and repetition-level + * vector + */ + void prepareBatch(int numRowsToRead, int estimatedSize) { Review Comment: I see it, but I don't think it does what we need: - getSizeStatistics() - we need null checks for old writers. - estimatedSize is a row-group list count, but it's passed to prepareBatch(...) as a per-batch size, which allocates a fresh IntVector/NullabilityHolder of that size every batch. The fallback uses batchSize, so the two paths differ by orders of magnitude. - It sizes the list-level structures, not the child data vector — elements still go through copyFromSafe/setSafe, which is where the reallocation actually happens. If we would read the exact size first, then we don't need to copy and resize. -- 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]
