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


##########
test/Apache.Arrow.Tests/RecordBatchBodyReferenceTests.cs:
##########
@@ -0,0 +1,450 @@
+// 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 System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using Apache.Arrow.C;
+using Apache.Arrow.Ipc;
+using Apache.Arrow.Memory;
+using Apache.Arrow.Types;
+using Xunit;
+
+namespace Apache.Arrow.Tests
+{
+    /// <summary>
+    /// The buffers of a record batch read from a stream are views of the 
message body, and each
+    /// holds a reference to it. Anything that shares those buffers, such as a 
slice, a retained
+    /// array, a concatenation or an exported array, keeps the body alive 
after the batch is
+    /// disposed, and the body is released along with the last of them.
+    /// </summary>
+    public class RecordBatchBodyReferenceTests
+    {
+        private static readonly int?[] s_ints = { 1, 2, 3, 4, null };
+        private static readonly string[] s_strings =
+        {
+            "the first string, too long to inline",
+            "the second string, too long to inline",
+            "short",
+            null,
+            "the fifth string, too long to inline",
+        };
+        private static readonly int[] s_runs = { 10, 10, 20, 20, 20 };
+
+        public enum Source
+        {
+            // ArrowStreamReader over a MemoryStream, which copies each body 
out of the stream's buffer
+            MemoryStream,
+            // ArrowStreamReader over any other stream
+            Stream,
+            // ArrowFileReader
+            File,
+        }
+
+        public enum Body
+        {
+            Uncompressed,
+            Compressed,
+            // Compressed, but no buffer gets smaller, so the writer stores 
them all uncompressed
+            StoredUncompressed,
+        }
+
+        public static TheoryData<Source, Body> Formats()
+        {
+            var data = new TheoryData<Source, Body>();
+            foreach (Source source in new[] { Source.MemoryStream, 
Source.Stream, Source.File })
+            {
+                foreach (Body body in new[] { Body.Uncompressed, 
Body.Compressed, Body.StoredUncompressed })
+                {
+                    data.Add(source, body);
+                }
+            }
+            return data;
+        }
+
+        [Theory]
+        [MemberData(nameof(Formats))]
+        public void ASharedSliceOfTheBatchOutlivesIt(Source source, Body body)
+        {
+            using var allocator = new PoisonMemoryAllocator();
+            RecordBatch batch = ReadBatch(source, body, allocator);
+
+            using RecordBatch slice = batch.SliceShared(1, 3);
+            batch.Dispose();
+
+            Assert.Equal(s_ints.Skip(1).Take(3), Ints(slice.Column(0)));
+            Assert.Equal(s_strings.Skip(1).Take(3), Strings(slice.Column(1)));
+        }
+
+        [Theory]
+        [MemberData(nameof(Formats))]
+        public void ASharedSliceOfAnArrayOutlivesTheBatch(Source source, Body 
body)
+        {
+            using var allocator = new PoisonMemoryAllocator();
+            RecordBatch batch = ReadBatch(source, body, allocator);
+
+            using IArrowArray ints = 
ArrowArrayFactory.SliceShared(batch.Column(0), 0, s_ints.Length);
+            using IArrowArray strings = 
ArrowArrayFactory.SliceShared(batch.Column(1), 0, s_strings.Length);
+            batch.Dispose();
+
+            Assert.Equal(s_ints, Ints(ints));
+            Assert.Equal(s_strings, Strings(strings));
+        }
+
+        [Theory]
+        [MemberData(nameof(Formats))]
+        public void RetainedArrayDataOutlivesTheBatch(Source source, Body body)
+        {
+            using var allocator = new PoisonMemoryAllocator();
+            RecordBatch batch = ReadBatch(source, body, allocator);
+
+            using IArrowArray ints = 
ArrowArrayFactory.BuildArray(batch.Column(0).Data.Retain());
+            using IArrowArray strings = 
ArrowArrayFactory.BuildArray(batch.Column(1).Data.Retain());
+            batch.Dispose();
+
+            Assert.Equal(s_ints, Ints(ints));
+            Assert.Equal(s_strings, Strings(strings));
+        }
+
+        [Theory]
+        [MemberData(nameof(Formats))]
+        public void ConcatenatingOneArrayOutlivesTheBatch(Source source, Body 
body)
+        {
+            using var allocator = new PoisonMemoryAllocator();
+            RecordBatch batch = ReadBatch(source, body, allocator);
+
+            using IArrowArray ints = ArrowArrayConcatenator.Concatenate(new[] 
{ batch.Column(0) });
+            batch.Dispose();
+
+            Assert.Equal(s_ints, Ints(ints));
+        }
+
+        [Theory]
+        [MemberData(nameof(Formats))]
+        public void ConcatenatedStringViewsOutliveTheBatches(Source source, 
Body body)
+        {
+            using var allocator = new PoisonMemoryAllocator();
+            RecordBatch first = ReadBatch(source, body, allocator);
+            RecordBatch second = ReadBatch(source, body, allocator);
+
+            // The concatenation shares the inputs' variadic data buffers
+            using IArrowArray strings = ArrowArrayConcatenator.Concatenate(
+                new List<IArrowArray> { first.Column(1), second.Column(1) });
+            first.Dispose();
+            second.Dispose();
+
+            Assert.Equal(s_strings.Concat(s_strings), Strings(strings));
+        }
+
+        [Theory]
+        [MemberData(nameof(Formats))]
+        public void ANormalizedRunEndEncodedArrayOutlivesTheBatch(Source 
source, Body body)
+        {
+            using var allocator = new PoisonMemoryAllocator();
+            RecordBatch batch = ReadBatch(source, body, allocator);
+
+            using RunEndEncodedArray normalized = 
((RunEndEncodedArray)batch.Column(2)).Normalize();
+            batch.Dispose();
+
+            Assert.Equal(s_runs, Runs(normalized));
+        }
+
+        [Theory]
+        [MemberData(nameof(Formats))]
+        public unsafe void AnExportedBatchOutlivesTheBatch(Source source, Body 
body)
+        {
+            using var allocator = new PoisonMemoryAllocator();
+            RecordBatch batch = ReadBatch(source, body, allocator);
+            Schema schema = batch.Schema;
+
+            CArrowArray* cArray = CArrowArray.Create();
+            try
+            {
+                CArrowArrayExporter.ExportRecordBatch(batch, cArray);
+                batch.Dispose();
+
+                using RecordBatch imported = 
CArrowArrayImporter.ImportRecordBatch(cArray, schema);
+                Assert.Equal(s_ints, Ints(imported.Column(0)));
+                Assert.Equal(s_strings, Strings(imported.Column(1)));
+                Assert.Equal(s_runs, Runs(imported.Column(2)));
+            }
+            finally
+            {
+                CArrowArray.Free(cArray);
+            }
+        }
+
+        [Theory]
+        [MemberData(nameof(Formats))]
+        public unsafe void AnExportedArrayOutlivesTheBatch(Source source, Body 
body)
+        {
+            using var allocator = new PoisonMemoryAllocator();
+            RecordBatch batch = ReadBatch(source, body, allocator);
+
+            CArrowArray* cArray = CArrowArray.Create();
+            try
+            {
+                CArrowArrayExporter.ExportArray(batch.Column(1), cArray);
+                batch.Dispose();
+
+                using IArrowArray imported = 
CArrowArrayImporter.ImportArray(cArray, StringViewType.Default);
+                Assert.Equal(s_strings, Strings(imported));
+            }
+            finally
+            {
+                CArrowArray.Free(cArray);
+            }
+        }
+
+        [Theory]
+        [MemberData(nameof(Formats))]
+        public unsafe void 
ABatchReadWithTheDefaultAllocatorCanBeDisposedWhileExported(Source source, Body 
body)
+        {
+            RecordBatch batch = ReadBatch(source, body, new 
NativeMemoryAllocator());
+            Schema schema = batch.Schema;
+
+            CArrowArray* cArray = CArrowArray.Create();
+            try
+            {
+                CArrowArrayExporter.ExportRecordBatch(batch, cArray);
+                batch.Dispose();

Review Comment:
   That's the third time this has been raised. This allocator isn't 
`IDisposable`. `PoisonMemoryAllocator` is.



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