siddharthteotia commented on code in PR #18638:
URL: https://github.com/apache/pinot/pull/18638#discussion_r3384694874


##########
pinot-plugins/pinot-input-format/pinot-arrow/src/main/java/org/apache/pinot/plugin/inputformat/arrow/ArrowAccumulators.java:
##########
@@ -0,0 +1,184 @@
+/**
+ * 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.pinot.plugin.inputformat.arrow;
+
+import com.google.common.base.Preconditions;
+import java.io.IOException;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Set;
+import javax.annotation.Nullable;
+import org.apache.arrow.memory.BufferAllocator;
+import org.apache.arrow.vector.FieldVector;
+import org.apache.arrow.vector.VectorSchemaRoot;
+import org.apache.arrow.vector.ipc.ArrowReader;
+import org.apache.arrow.vector.util.VectorAppender;
+import org.apache.pinot.spi.data.FieldSpec;
+import org.apache.pinot.spi.data.Schema;
+import org.apache.pinot.spi.data.readers.ColumnReader;
+
+
+/**
+ * Package-private helper shared by {@link ArrowColumnReaderFactory} and
+ * {@link InMemoryArrowColumnReaderFactory}: walks every record batch in an 
{@link ArrowReader},
+ * concatenates each wanted column's values into a per-column accumulator 
{@link FieldVector} via
+ * Arrow's {@link VectorAppender}, and produces one {@link ArrowColumnReader} 
per accumulator.
+ *
+ * <p>Accumulator vectors are allocated against the caller-supplied {@link 
BufferAllocator}. The
+ * caller (factory) owns and closes them via {@link Result#getAccumulators()}; 
this helper does
+ * not retain references.
+ */
+final class ArrowAccumulators {
+
+  private ArrowAccumulators() {
+  }
+
+  static Result populate(ArrowReader reader, BufferAllocator allocator, Schema 
targetSchema,

Review Comment:
   Feedback on the overall approach in this method:
   
   It' currently implemented in a way that will leak Arrow Buffers imo. The 
function returns result object that contains Arrow Vectors. 
   
   In the build loop, we are doing something like this at a high level:
   
   ```
   for (FieldVector source : perBatchRoot.getFieldVectors()) {
     ...
     Preconditions.checkArgument(source.getField().getDictionary() == null, 
...); // can throw
     FieldVector accumulator = source.getField().createVector(allocator);
     accumulator.allocateNew();          // off-heap Arrow buffers allocated 
here
     accumulators.put(name, accumulator);
     ...
   }
   ...
   if (!anyBatchSeen) {
     throw new IOException("Arrow source contains no non-empty record batches");
   }
   ```
   
   The accumulators map is a local that is only handed to the factory via the 
returned Result. 
   
   If anything throws after the first allocateNew() — a later column being 
dictionary-encoded / or the !anyBatchSeen path, or a loadNextBatch IO error — 
the already-allocated off-heap buffers are never closed. 
   
   If you are relying on `try-with-resources` idiom used in the factory, that 
will not save you: the factory's _accumulatorVectors field is still null 
(populate never returned), so factory.close() → closeAll(null) is a no-op. 
   
   If my Arrow memory serves me right, RootAllocator will then throw on its own 
close because outstanding child buffers remain 
   
   In short, **a half-built collection of Closeables** must be unwound by 
whoever is mid-construction, **because nobody else has a reference yet.**



-- 
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]

Reply via email to