viirya commented on code in PR #584:
URL: https://github.com/apache/datafusion-comet/pull/584#discussion_r1645065115


##########
common/src/main/java/org/apache/arrow/c/CometBufferImportTypeVisitor.java:
##########
@@ -0,0 +1,378 @@
+/*
+ * 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.arrow.c;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
+import org.apache.arrow.memory.ArrowBuf;
+import org.apache.arrow.memory.BufferAllocator;
+import org.apache.arrow.util.AutoCloseables;
+import org.apache.arrow.util.VisibleForTesting;
+import org.apache.arrow.vector.DateDayVector;
+import org.apache.arrow.vector.DateMilliVector;
+import org.apache.arrow.vector.DurationVector;
+import org.apache.arrow.vector.Float4Vector;
+import org.apache.arrow.vector.Float8Vector;
+import org.apache.arrow.vector.IntervalDayVector;
+import org.apache.arrow.vector.IntervalMonthDayNanoVector;
+import org.apache.arrow.vector.IntervalYearVector;
+import org.apache.arrow.vector.LargeVarBinaryVector;
+import org.apache.arrow.vector.LargeVarCharVector;
+import org.apache.arrow.vector.TimeMicroVector;
+import org.apache.arrow.vector.TimeMilliVector;
+import org.apache.arrow.vector.TimeNanoVector;
+import org.apache.arrow.vector.TimeSecVector;
+import org.apache.arrow.vector.TimeStampVector;
+import org.apache.arrow.vector.VarBinaryVector;
+import org.apache.arrow.vector.VarCharVector;
+import org.apache.arrow.vector.complex.DenseUnionVector;
+import org.apache.arrow.vector.complex.LargeListVector;
+import org.apache.arrow.vector.complex.ListVector;
+import org.apache.arrow.vector.complex.MapVector;
+import org.apache.arrow.vector.complex.UnionVector;
+import org.apache.arrow.vector.ipc.message.ArrowFieldNode;
+import org.apache.arrow.vector.types.pojo.ArrowType;
+import org.apache.arrow.vector.util.DataSizeRoundingUtil;
+
+import static org.apache.arrow.c.NativeUtil.NULL;
+import static org.apache.arrow.util.Preconditions.checkState;
+
+/**
+ * Import buffers from a C Data Interface struct. We copy it from Arrow 
`BufferImportTypeVisitor`
+ * and fix the issue: https://github.com/apache/arrow/issues/42156.
+ */
+class CometBufferImportTypeVisitor
+    implements ArrowType.ArrowTypeVisitor<List<ArrowBuf>>, AutoCloseable {
+  private final BufferAllocator allocator;
+  private final ReferenceCountedArrowArray underlyingAllocation;
+  private final ArrowFieldNode fieldNode;
+  private final ArrowArray.Snapshot snapshot;
+  private final long[] buffers;
+  private final List<ArrowBuf> imported;
+
+  CometBufferImportTypeVisitor(
+      BufferAllocator allocator,
+      ReferenceCountedArrowArray underlyingAllocation,
+      ArrowFieldNode fieldNode,
+      ArrowArray.Snapshot snapshot,
+      long[] buffers) {
+    this.allocator = allocator;
+    this.underlyingAllocation = underlyingAllocation;
+    this.fieldNode = fieldNode;
+    this.snapshot = snapshot;
+    this.buffers = buffers;
+    this.imported = new ArrayList<>();
+  }
+
+  @Override
+  public void close() throws Exception {
+    AutoCloseables.close(imported);
+  }
+
+  @VisibleForTesting
+  ArrowBuf importBuffer(ArrowType type, int index, long capacity) {
+    return importBuffer(type, index, 0, capacity);
+  }
+
+  @VisibleForTesting
+  ArrowBuf importBuffer(ArrowType type, int index, long offset, long capacity) 
{
+    checkState(
+        buffers.length > index,
+        "Expected at least %s buffers for type %s, but found %s",
+        index + 1,
+        type,
+        buffers.length);
+    long bufferPtr = buffers[index] + offset;
+
+    if (bufferPtr == NULL) {
+      // C array may be NULL but only accept that if expected capacity is zero 
too
+      if (capacity != 0) {
+        throw new IllegalStateException(
+            String.format("Buffer %s for type %s cannot be null", index, 
type));
+      } else {
+        // no data in the C array, return an empty buffer
+        return allocator.getEmpty();
+      }
+    }
+
+    ArrowBuf buf = underlyingAllocation.unsafeAssociateAllocation(allocator, 
capacity, bufferPtr);
+    imported.add(buf);
+    return buf;
+  }
+
+  private ArrowBuf importFixedBits(ArrowType type, int index, long 
bitsPerSlot) {
+    final long capacity = DataSizeRoundingUtil.divideBy8Ceil(bitsPerSlot * 
fieldNode.getLength());
+    return importBuffer(type, index, capacity);
+  }
+
+  private ArrowBuf importFixedBytes(ArrowType type, int index, long 
bytesPerSlot) {
+    final long capacity = bytesPerSlot * fieldNode.getLength();
+    return importBuffer(type, index, capacity);
+  }
+
+  private ArrowBuf importOffsets(ArrowType type, long bytesPerSlot) {
+    final long capacity = bytesPerSlot * (fieldNode.getLength() + 1);
+    final long offset = snapshot.offset * bytesPerSlot;
+    return importBuffer(type, 1, offset, capacity);
+  }

Review Comment:
   This class adds offset to buffer pointer. This is major change to Arrow Java 
`BufferImportTypeVisitor`.



-- 
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: github-unsubscr...@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org
For additional commands, e-mail: github-h...@datafusion.apache.org

Reply via email to