CurtHagenlocher commented on code in PR #504:
URL: https://github.com/apache/arrow-dotnet/pull/504#discussion_r4793950609


##########
src/Apache.Arrow/Arrays/BinaryViewArray.cs:
##########
@@ -85,9 +125,21 @@ public TArray Build(MemoryAllocator allocator = default)
             {
                 bool hasValues = ValueBuffer.Length > 0;
                 var bufs = new ArrowBuffer[hasValues ? 3 : 2];
-                bufs[0] = NullCount > 0 ? ValidityBuffer.Build(allocator) : 
ArrowBuffer.Empty;
-                bufs[1] = BinaryViews.Build(allocator);
-                if (hasValues) { bufs[2] = ValueBuffer.Build(allocator); }
+                try
+                {
+                    bufs[0] = NullCount > 0 ? ValidityBuffer.Build(allocator) 
: ArrowBuffer.Empty;
+                    bufs[1] = BinaryViews.Build(allocator);
+                    if (hasValues) { bufs[2] = ValueBuffer.Build(allocator); }
+                }

Review Comment:
   Good catch. Fixed in f5eced1: creating the `ArrayData` and the array is now 
inside the `try`, as in the other leaf builders. 
`BuildingAnArrayOfTheWrongTypeReleasesWhatWasBuilt` covers your example, and 
`BinaryView` is now one of the builders tested against an allocator that 
refuses each allocation in turn.
   



##########
src/Apache.Arrow/Arrays/MapArray.cs:
##########
@@ -39,11 +39,25 @@ public class MapArray : ListArray // MapArray = 
ListArray(StructArray("key", "va
             public MapType DataType { get; }
 
             public Builder(MapType type)
+                : this(type, null)
             {
-                KeyBuilder = 
ArrowArrayBuilderFactory.Build(type.KeyField.DataType);
-                ValueBuilder = 
ArrowArrayBuilderFactory.Build(type.ValueField.DataType);
-                ValueOffsetsBufferBuilder = new ArrowBuffer.Builder<int>();
-                ValidityBufferBuilder = new ArrowBuffer.BitmapBuilder();
+            }
+
+            /// <summary>
+            /// Creates a builder whose working memory comes from <paramref 
name="allocator"/>.
+            /// </summary>
+            /// <param name="type">The map type to build.</param>
+            /// <param name="allocator">
+            /// Allocator for the builder's working memory, which usually 
becomes the built array's
+            /// memory. When <c>null</c>, the builder works in managed memory 
and copies
+            /// what it builds into <see cref="MemoryAllocator.Default"/>, as 
builders always have.
+            /// </param>
+            public Builder(MapType type, MemoryAllocator allocator)
+            {
+                KeyBuilder = 
ArrowArrayBuilderFactory.Build(type.KeyField.DataType, allocator);
+                ValueBuilder = 
ArrowArrayBuilderFactory.Build(type.ValueField.DataType, allocator);
+                ValueOffsetsBufferBuilder = new ArrowBuffer.Builder<int>(0, 
allocator);
+                ValidityBufferBuilder = new ArrowBuffer.BitmapBuilder(0, 
allocator);
                 DataType = type;

Review Comment:
   Agreed. Fixed in f5eced1: if anything after the key builder fails, the 
constructor releases what was created and rethrows. Tests cover an unsupported 
value type and an allocator refusing each allocation after the first.
   



##########
src/Apache.Arrow/Arrays/VariantArray.cs:
##########
@@ -390,6 +390,26 @@ public class Builder
             private static readonly Lazy<(byte[] Metadata, byte[] Value)> 
NullPlaceholder =
                 new Lazy<(byte[], byte[])>(() => new 
VariantBuilder().Encode(VariantValue.Null));
 
+            public Builder()
+                : this(null)
+            {
+            }
+
+            /// <summary>
+            /// Creates a builder whose working memory comes from <paramref 
name="allocator"/>.
+            /// </summary>
+            /// <param name="allocator">
+            /// Allocator for the builder's working memory, which usually 
becomes the built array's
+            /// memory. When <c>null</c>, the builder works in managed memory 
and copies
+            /// what it builds into <see cref="MemoryAllocator.Default"/>, as 
builders always have.
+            /// </param>
+            public Builder(MemoryAllocator allocator)
+            {
+                _metadataBuilder = new BinaryArray.Builder(BinaryType.Default, 
allocator);
+                _valueBuilder = new BinaryArray.Builder(BinaryType.Default, 
allocator);
+                _validityBuilder = new ArrowBuffer.BitmapBuilder(0, allocator);

Review Comment:
   Agreed. Fixed in f5eced1: if creating the second binary builder fails, the 
constructor releases the first. The test refuses each allocation after the 
first.
   
   I checked the other builders whose constructors create children. The list 
builders create one child and then buffer builders that don't allocate at zero 
capacity, and the timestamp-with-offset builder's children don't allocate 
either, so only the map and variant builders needed this.
   



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