Copilot commented on code in PR #274:
URL: https://github.com/apache/arrow-dotnet/pull/274#discussion_r2869499755
##########
test/Apache.Arrow.Tests/CDataInterfacePythonTests.cs:
##########
@@ -1201,6 +1201,107 @@ public unsafe void ImportGuidArray()
CArrowArray.Free(cArray);
}
+ [SkippableFact]
+ public unsafe void ExportBool8Array()
+ {
+ // Export a C# Bool8Array via the C Data Interface and verify
+ // that Python/pyarrow sees it as an arrow.bool8 extension array
+ // with the correct values.
+
+ var builder = new Bool8Array.Builder();
+ builder.Append(true);
+ builder.Append(false);
+ builder.Append(null);
+ builder.Append(false);
+ builder.Append(true);
+ var bool8Array = builder.Build();
+
+ var field = new Field("bools", Bool8Type.Default, true);
+ var schema = new Schema(new[] { field }, null);
+ var batch = new RecordBatch(schema, new[] { bool8Array },
bool8Array.Length);
+
+ CArrowArray* cArray = CArrowArray.Create();
+ CArrowArrayExporter.ExportRecordBatch(batch, cArray);
+
+ CArrowSchema* cSchema = CArrowSchema.Create();
+ CArrowSchemaExporter.ExportSchema(batch.Schema, cSchema);
+
+ long arrayPtr = ((IntPtr)cArray).ToInt64();
+ long schemaPtr = ((IntPtr)cSchema).ToInt64();
+
+ using (Py.GIL())
+ {
+ dynamic pa = Py.Import("pyarrow");
+
+ dynamic pyBatch = pa.RecordBatch._import_from_c(arrayPtr,
schemaPtr);
+ dynamic pyArray = pyBatch.column(0);
+
+ // Build the expected UUID array in Python
+ dynamic expectedArray = pa.array(
+ new PyList(new PyObject[]
+ {
+ PyObject.FromManagedObject(true),
+ PyObject.FromManagedObject(false),
+ PyObject.None,
+ PyObject.FromManagedObject(false),
+ PyObject.FromManagedObject(true),
+ }),
+ type: pa.bool8());
+
+ Assert.True((bool)pyArray.equals(expectedArray));
+ }
+
+ CArrowArray.Free(cArray);
+ CArrowSchema.Free(cSchema);
+ }
+
+ [SkippableFact]
+ public unsafe void ImporBool8Array()
+ {
+ // Create a bool8 array in Python, export it via the C Data
Interface,
+ // and verify that C# resolves it as a Bool8Array with correct
values.
+
+ CArrowArray* cArray = CArrowArray.Create();
+ CArrowSchema* cSchema = CArrowSchema.Create();
+
+ using (Py.GIL())
+ {
+ dynamic pa = Py.Import("pyarrow");
+
+ dynamic pyArray = pa.array(
+ new PyList(new PyObject[]
+ {
+ PyObject.None,
+ PyObject.FromManagedObject(false),
+ PyObject.FromManagedObject(true),
+ PyObject.FromManagedObject(true),
+ }),
+ type: pa.bool8());
+
+ long arrayPtr = ((IntPtr)cArray).ToInt64();
+ long schemaPtr = ((IntPtr)cSchema).ToInt64();
+ pyArray._export_to_c(arrayPtr, schemaPtr);
+ }
+
+ var registry = new ExtensionTypeRegistry();
+ registry.Register(Bool8ExtensionDefinition.Instance);
+
+ Field field = CArrowSchemaImporter.ImportField(cSchema, registry);
+ Assert.IsType<Bool8Type>(field.DataType);
+
+ IArrowArray importedArray =
CArrowArrayImporter.ImportArray(cArray, field.DataType);
+ Assert.IsType<Bool8Array>(importedArray);
+
+ var bool8Array = (Bool8Array)importedArray;
+ Assert.Equal(4, bool8Array.Length);
+ Assert.Null(bool8Array.GetValue(0));
+ Assert.Equal(false, bool8Array.GetValue(1));
+ Assert.Equal(true, bool8Array.GetValue(2));
+ Assert.Equal(true, bool8Array.GetValue(3));
+
+ CArrowArray.Free(cArray);
Review Comment:
CArrowSchema is allocated in this test but never freed.
CArrowSchemaImporter.ImportField calls the schema's release callback but does
not free the struct memory; the caller still needs to call
CArrowSchema.Free(cSchema) (as shown in other tests in this file). Please free
cSchema after importing (ideally in a finally block alongside cArray).
```suggestion
try
{
using (Py.GIL())
{
dynamic pa = Py.Import("pyarrow");
dynamic pyArray = pa.array(
new PyList(new PyObject[]
{
PyObject.None,
PyObject.FromManagedObject(false),
PyObject.FromManagedObject(true),
PyObject.FromManagedObject(true),
}),
type: pa.bool8());
long arrayPtr = ((IntPtr)cArray).ToInt64();
long schemaPtr = ((IntPtr)cSchema).ToInt64();
pyArray._export_to_c(arrayPtr, schemaPtr);
}
var registry = new ExtensionTypeRegistry();
registry.Register(Bool8ExtensionDefinition.Instance);
Field field = CArrowSchemaImporter.ImportField(cSchema,
registry);
Assert.IsType<Bool8Type>(field.DataType);
IArrowArray importedArray =
CArrowArrayImporter.ImportArray(cArray, field.DataType);
Assert.IsType<Bool8Array>(importedArray);
var bool8Array = (Bool8Array)importedArray;
Assert.Equal(4, bool8Array.Length);
Assert.Null(bool8Array.GetValue(0));
Assert.Equal(false, bool8Array.GetValue(1));
Assert.Equal(true, bool8Array.GetValue(2));
Assert.Equal(true, bool8Array.GetValue(3));
}
finally
{
CArrowArray.Free(cArray);
CArrowSchema.Free(cSchema);
}
```
##########
test/Apache.Arrow.Tests/CDataInterfacePythonTests.cs:
##########
@@ -1201,6 +1201,107 @@ public unsafe void ImportGuidArray()
CArrowArray.Free(cArray);
}
+ [SkippableFact]
+ public unsafe void ExportBool8Array()
+ {
+ // Export a C# Bool8Array via the C Data Interface and verify
+ // that Python/pyarrow sees it as an arrow.bool8 extension array
+ // with the correct values.
+
+ var builder = new Bool8Array.Builder();
+ builder.Append(true);
+ builder.Append(false);
+ builder.Append(null);
+ builder.Append(false);
+ builder.Append(true);
+ var bool8Array = builder.Build();
+
+ var field = new Field("bools", Bool8Type.Default, true);
+ var schema = new Schema(new[] { field }, null);
+ var batch = new RecordBatch(schema, new[] { bool8Array },
bool8Array.Length);
+
+ CArrowArray* cArray = CArrowArray.Create();
+ CArrowArrayExporter.ExportRecordBatch(batch, cArray);
+
+ CArrowSchema* cSchema = CArrowSchema.Create();
+ CArrowSchemaExporter.ExportSchema(batch.Schema, cSchema);
+
+ long arrayPtr = ((IntPtr)cArray).ToInt64();
+ long schemaPtr = ((IntPtr)cSchema).ToInt64();
+
+ using (Py.GIL())
+ {
+ dynamic pa = Py.Import("pyarrow");
+
+ dynamic pyBatch = pa.RecordBatch._import_from_c(arrayPtr,
schemaPtr);
+ dynamic pyArray = pyBatch.column(0);
+
+ // Build the expected UUID array in Python
+ dynamic expectedArray = pa.array(
+ new PyList(new PyObject[]
+ {
+ PyObject.FromManagedObject(true),
+ PyObject.FromManagedObject(false),
+ PyObject.None,
+ PyObject.FromManagedObject(false),
+ PyObject.FromManagedObject(true),
+ }),
+ type: pa.bool8());
+
+ Assert.True((bool)pyArray.equals(expectedArray));
+ }
+
+ CArrowArray.Free(cArray);
+ CArrowSchema.Free(cSchema);
+ }
+
+ [SkippableFact]
+ public unsafe void ImporBool8Array()
Review Comment:
Test name looks like a typo: "ImporBool8Array" -> "ImportBool8Array" (helps
discoverability and keeps naming consistent with
ImportGuidArray/ExportBool8Array).
```suggestion
public unsafe void ImportBool8Array()
```
##########
test/Apache.Arrow.Tests/CDataInterfacePythonTests.cs:
##########
@@ -1201,6 +1201,107 @@ public unsafe void ImportGuidArray()
CArrowArray.Free(cArray);
}
+ [SkippableFact]
+ public unsafe void ExportBool8Array()
+ {
+ // Export a C# Bool8Array via the C Data Interface and verify
+ // that Python/pyarrow sees it as an arrow.bool8 extension array
+ // with the correct values.
+
+ var builder = new Bool8Array.Builder();
+ builder.Append(true);
+ builder.Append(false);
+ builder.Append(null);
+ builder.Append(false);
+ builder.Append(true);
+ var bool8Array = builder.Build();
+
+ var field = new Field("bools", Bool8Type.Default, true);
+ var schema = new Schema(new[] { field }, null);
+ var batch = new RecordBatch(schema, new[] { bool8Array },
bool8Array.Length);
+
+ CArrowArray* cArray = CArrowArray.Create();
+ CArrowArrayExporter.ExportRecordBatch(batch, cArray);
+
+ CArrowSchema* cSchema = CArrowSchema.Create();
+ CArrowSchemaExporter.ExportSchema(batch.Schema, cSchema);
+
+ long arrayPtr = ((IntPtr)cArray).ToInt64();
+ long schemaPtr = ((IntPtr)cSchema).ToInt64();
+
+ using (Py.GIL())
+ {
+ dynamic pa = Py.Import("pyarrow");
+
+ dynamic pyBatch = pa.RecordBatch._import_from_c(arrayPtr,
schemaPtr);
+ dynamic pyArray = pyBatch.column(0);
+
+ // Build the expected UUID array in Python
Review Comment:
This comment says "expected UUID array" but this test is constructing a
bool8 array. Please update the comment to match the data being built to avoid
confusion when maintaining these tests.
```suggestion
// Build the expected bool8 array in Python
```
##########
src/Apache.Arrow/Arrays/Bool8Array.cs:
##########
@@ -0,0 +1,151 @@
+// 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;
+using System.Collections.Generic;
+using Apache.Arrow.Arrays;
+using Apache.Arrow.Types;
+
+namespace Apache.Arrow
+{
+ /// <summary>
+ /// Extension definition for the "arrow.uuid" extension type,
+ /// backed by FixedSizeBinary(16).
Review Comment:
The XML summary was copy/pasted from the UUID extension and is incorrect for
this type (it still says "arrow.uuid" and FixedSizeBinary(16)). Please update
it to describe the arrow.bool8 extension and its Int8 storage type so generated
docs are accurate.
```suggestion
/// Extension definition for the "arrow.bool8" extension type,
/// backed by the Int8 storage type.
```
##########
src/Apache.Arrow/Arrays/Bool8Array.cs:
##########
@@ -0,0 +1,151 @@
+// 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;
+using System.Collections.Generic;
+using Apache.Arrow.Arrays;
+using Apache.Arrow.Types;
+
+namespace Apache.Arrow
+{
+ /// <summary>
+ /// Extension definition for the "arrow.uuid" extension type,
+ /// backed by FixedSizeBinary(16).
+ /// </summary>
+ public class Bool8ExtensionDefinition : ExtensionDefinition
+ {
+ public static Bool8ExtensionDefinition Instance = new
Bool8ExtensionDefinition();
+
+ public override string ExtensionName => "arrow.bool8";
+
+ private Bool8ExtensionDefinition() { }
+
+ public override bool TryCreateType(IArrowType storageType, string
metadata, out ExtensionType type)
+ {
+ if (storageType is Int8Type i8Type)
+ {
+ type = new Bool8Type(i8Type);
+ return true;
+ }
+ type = null;
+ return false;
+ }
+ }
+
+ /// <summary>
+ /// Extension type representing 1-byte boolean values
+ /// </summary>
+ public class Bool8Type : ExtensionType
+ {
+ public static Bool8Type Default = new Bool8Type();
+
+ public override string Name => "arrow.bool8";
+ public override string ExtensionMetadata => "";
+
+ public Bool8Type() : base(Int8Type.Default) { }
+
+ internal Bool8Type(Int8Type storageType) : base(storageType) { }
+
+ public override ExtensionArray CreateArray(IArrowArray storageArray)
+ {
+ return new Bool8Array(this, storageArray);
+ }
+ }
+
+ /// <summary>
+ /// Extension array for 1-byte boolean values, backed by an Int8Array.
+ /// </summary>
+ public class Bool8Array : ExtensionArray, IReadOnlyList<bool?>
+ {
+ public Int8Array StorageArray => (Int8Array)Storage;
+
+ public Bool8Array(Bool8Type bool8Type, IArrowArray storage) :
base(bool8Type, storage) { }
+
+ public Bool8Array(IArrowArray storage) : base(Bool8Type.Default,
storage) { }
+
+ public class Builder : PrimitiveArrayBuilder<sbyte, Bool8Array,
Builder>
+ {
+ protected override Bool8Array Build(
+ ArrowBuffer valueBuffer, ArrowBuffer nullBitmapBuffer,
+ int length, int nullCount, int offset) =>
+ new Bool8Array(new Int8Array(valueBuffer, nullBitmapBuffer,
length, nullCount, offset));
+
+ public Builder Append(bool value)
+ {
+ return Append(value ? (sbyte)1 : (sbyte)0);
+ }
+
+ public Builder Append(bool? value)
+ {
+ if (value == null)
+ {
+ return AppendNull();
+ }
+ return Append(value.Value);
+ }
+
+ public Builder AppendRange(IEnumerable<bool> values)
+ {
+ if (values == null)
+ {
+ throw new ArgumentNullException(nameof(values));
+ }
+
+ foreach (bool value in values)
+ {
+ Append(value);
+ }
+
+ return Instance;
+ }
+
+ public Builder AppendRange(IEnumerable<bool?> values)
+ {
+ if (values == null)
+ {
+ throw new ArgumentNullException(nameof(values));
+ }
+
+ foreach (bool? value in values)
+ {
+ Append(value);
+ }
+
+ return Instance;
+ }
+
+ public Builder Set(int index, bool value)
+ {
+ return Set(index, value ? (sbyte)1 : (sbyte)0);
+ }
+ }
+
+ public int Count => Length;
+ public bool? this[int index] => GetValue(index);
+
+ public bool? GetValue(int index) => IsValid(index) ?
StorageArray.GetValue(index).Value != 0 : null;
+
Review Comment:
Bool8Array.GetValue calls IsValid(index) without doing an index bounds check
first. If index is out of range and the array has nulls (NullCount > 0),
IsValid may read past the bitmap span and throw an IndexOutOfRangeException
rather than the expected ArgumentOutOfRangeException. Add an explicit range
check (like GuidArray.GetGuid does) before calling IsValid.
```suggestion
public bool? GetValue(int index)
{
if (index < 0 || index >= Length)
{
throw new ArgumentOutOfRangeException(nameof(index));
}
return IsValid(index) ? StorageArray.GetValue(index).Value != 0
: (bool?)null;
}
```
--
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]