Copilot commented on code in PR #303:
URL: https://github.com/apache/arrow-dotnet/pull/303#discussion_r3012923108


##########
src/Apache.Arrow/C/CArrowArrayImporter.cs:
##########
@@ -393,6 +397,32 @@ private ArrowBuffer[] ImportListViewBuffers(CArrowArray* 
cArray)
                 return buffers;
             }
 
+            private ArrowBuffer[] ImportLargeListViewBuffers(CArrowArray* 
cArray)
+            {
+                if (cArray->n_buffers != 3)
+                {
+                    throw new InvalidOperationException("Large list view 
arrays are expected to have exactly three buffers");
+                }
+
+                const int maxLength = int.MaxValue / 8;
+                if (cArray->length > maxLength)
+                {
+                    throw new OverflowException(
+                        $"Cannot import large list view array. Array length 
{cArray->length} " +
+                        $"is greater than the maximum supported large list 
view array length ({maxLength})");
+                }
+
+                int length = checked((int)cArray->offset + 
(int)cArray->length);
+                int bufferLength = length * 8;
+
+                ArrowBuffer[] buffers = new ArrowBuffer[3];
+                buffers[0] = ImportValidityBuffer(cArray);

Review Comment:
   `bufferLength = length * 8` can overflow when `cArray->offset` is large even 
if `cArray->length` passes the current `maxLength` check (since `length` 
includes `offset + length`). If this overflows negative, `ImportCArrayBuffer` 
will return `ArrowBuffer.Empty` and the imported array data will be corrupted 
instead of throwing. Consider validating the combined `length` (offset+length) 
against `int.MaxValue / 8` (or using `checked(length * 8)`) and throwing an 
`OverflowException` when it exceeds the supported range.



##########
src/Apache.Arrow/Arrays/LargeListViewArray.cs:
##########
@@ -0,0 +1,216 @@
+// 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.
+
+using System;
+using Apache.Arrow.Memory;
+using Apache.Arrow.Types;
+
+namespace Apache.Arrow
+{
+    public class LargeListViewArray : Array
+    {
+        public class Builder : IArrowArrayBuilder<LargeListViewArray, Builder>
+        {
+            public IArrowArrayBuilder<IArrowArray, 
IArrowArrayBuilder<IArrowArray>> ValueBuilder { get; }
+
+            public int Length => ValueOffsetsBufferBuilder.Length;
+
+            private ArrowBuffer.Builder<long> ValueOffsetsBufferBuilder { get; 
}
+
+            private ArrowBuffer.Builder<long> SizesBufferBuilder { get; }
+
+            private ArrowBuffer.BitmapBuilder ValidityBufferBuilder { get; }
+
+            public int NullCount { get; protected set; }
+
+            private IArrowType DataType { get; }
+
+            private int Start { get; set; }
+
+            public Builder(IArrowType valueDataType) : this(new 
LargeListViewType(valueDataType))
+            {
+            }
+
+            public Builder(Field valueField) : this(new 
LargeListViewType(valueField))
+            {
+            }
+
+            internal Builder(LargeListViewType dataType)
+            {
+                ValueBuilder = 
ArrowArrayBuilderFactory.Build(dataType.ValueDataType);
+                ValueOffsetsBufferBuilder = new ArrowBuffer.Builder<long>();
+                SizesBufferBuilder = new ArrowBuffer.Builder<long>();
+                ValidityBufferBuilder = new ArrowBuffer.BitmapBuilder();
+                DataType = dataType;
+                Start = -1;
+            }
+
+            /// <summary>
+            /// Start a new variable-length list slot
+            ///
+            /// This function should be called before beginning to append 
elements to the
+            /// value builder.
+            /// </summary>
+            public Builder Append()
+            {
+                AppendPrevious();
+
+                ValidityBufferBuilder.Append(true);
+
+                return this;
+            }
+
+            public Builder AppendNull()
+            {
+                AppendPrevious();
+
+                ValidityBufferBuilder.Append(false);
+                ValueOffsetsBufferBuilder.Append(Start);
+                SizesBufferBuilder.Append(0);
+                NullCount++;
+                Start = -1;
+
+                return this;
+            }
+
+            private void AppendPrevious()
+            {
+                if (Start >= 0)
+                {
+                    ValueOffsetsBufferBuilder.Append(Start);
+                    SizesBufferBuilder.Append(ValueBuilder.Length - Start);
+                }
+                Start = ValueBuilder.Length;
+            }
+
+            public LargeListViewArray Build(MemoryAllocator allocator = 
default)
+            {
+                AppendPrevious();
+
+                ArrowBuffer validityBuffer = NullCount > 0
+                                        ? 
ValidityBufferBuilder.Build(allocator)
+                                        : ArrowBuffer.Empty;
+
+                return new LargeListViewArray(DataType, Length,
+                    ValueOffsetsBufferBuilder.Build(allocator), 
SizesBufferBuilder.Build(allocator),
+                    ValueBuilder.Build(allocator),
+                    validityBuffer, NullCount, 0);
+            }
+
+            public Builder Reserve(int capacity)
+            {
+                ValueOffsetsBufferBuilder.Reserve(capacity);
+                SizesBufferBuilder.Reserve(capacity);
+                ValidityBufferBuilder.Reserve(capacity);
+                return this;
+            }
+
+            public Builder Resize(int length)
+            {
+                ValueOffsetsBufferBuilder.Resize(length);
+                SizesBufferBuilder.Resize(length);
+                ValidityBufferBuilder.Resize(length);
+                return this;
+            }
+
+            public Builder Clear()
+            {
+                ValueOffsetsBufferBuilder.Clear();
+                SizesBufferBuilder.Clear();
+                ValueBuilder.Clear();
+                ValidityBufferBuilder.Clear();
+                NullCount = 0;

Review Comment:
   `Clear()` doesn't reset the internal `Start` cursor. If the builder is 
cleared after `Build()` (or after an `Append()` that hasn't been closed), 
`Start` can remain >= 0 while `ValueBuilder.Length` is reset to 0, and the next 
`AppendPrevious()` will append an invalid (potentially negative) size. 
Resetting `Start` (e.g., back to -1) as part of `Clear()` would make the 
builder safe to reuse after any state.
   ```suggestion
                   NullCount = 0;
                   Start = -1;
   ```



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

Reply via email to