This is an automated email from the ASF dual-hosted git repository.

curth pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow.git


The following commit(s) were added to refs/heads/main by this push:
     new 0026c0cda7 GH-30717: [C#] Add ToString() methods to Arrow classes 
(#36566)
0026c0cda7 is described below

commit 0026c0cda71c44b73a8b635bd5245b49b9cabb80
Author: Gavin Murrison <[email protected]>
AuthorDate: Mon Oct 30 16:17:15 2023 +0000

    GH-30717: [C#] Add ToString() methods to Arrow classes (#36566)
    
    ### What changes are included in this PR?
    
    Implemented the ToString() method on classes ChunkedArray, Field, 
RecordBatch, Schema and Table.
    I could not find the class DataType mentioned in the issue, perhaps it 
meant DateType?
    
    Closes #30717.
    * Closes: #30717
    
    Lead-authored-by: Gavin Murrison 
<[email protected]>
    Co-authored-by: voidstar69 <[email protected]>
    Co-authored-by: Weston Pace <[email protected]>
    Signed-off-by: Curt Hagenlocher <[email protected]>
---
 csharp/src/Apache.Arrow/ChunkedArray.cs                  | 2 ++
 csharp/src/Apache.Arrow/Field.cs                         | 2 ++
 csharp/src/Apache.Arrow/RecordBatch.cs                   | 2 ++
 csharp/src/Apache.Arrow/Schema.cs                        | 2 ++
 csharp/src/Apache.Arrow/Table.cs                         | 2 ++
 csharp/test/Apache.Arrow.Tests/ArrowStreamWriterTests.cs | 4 ++++
 csharp/test/Apache.Arrow.Tests/TableTests.cs             | 6 ++++++
 7 files changed, 20 insertions(+)

diff --git a/csharp/src/Apache.Arrow/ChunkedArray.cs 
b/csharp/src/Apache.Arrow/ChunkedArray.cs
index f5909f5adf..85b3560c22 100644
--- a/csharp/src/Apache.Arrow/ChunkedArray.cs
+++ b/csharp/src/Apache.Arrow/ChunkedArray.cs
@@ -92,6 +92,8 @@ namespace Apache.Arrow
             return Slice(offset, Length - offset);
         }
 
+        public override string ToString() => $"{nameof(ChunkedArray)}: 
Length={Length}, DataType={DataType.Name}";
+      
         private static IArrowArray[] Cast(IList<Array> arrays)
         {
             IArrowArray[] arrowArrays = new IArrowArray[arrays.Count];
diff --git a/csharp/src/Apache.Arrow/Field.cs b/csharp/src/Apache.Arrow/Field.cs
index 562b9587bd..4fddd1bc4e 100644
--- a/csharp/src/Apache.Arrow/Field.cs
+++ b/csharp/src/Apache.Arrow/Field.cs
@@ -61,5 +61,7 @@ namespace Apache.Arrow
             DataType = dataType ?? NullType.Default;
             IsNullable = nullable;
         }
+
+        public override string ToString() => $"{nameof(Field)}: Name={Name}, 
DataType={DataType.Name}, IsNullable={IsNullable}, Metadata 
count={Metadata?.Count ?? 0}";
     }
 }
diff --git a/csharp/src/Apache.Arrow/RecordBatch.cs 
b/csharp/src/Apache.Arrow/RecordBatch.cs
index f87081d298..566c778302 100644
--- a/csharp/src/Apache.Arrow/RecordBatch.cs
+++ b/csharp/src/Apache.Arrow/RecordBatch.cs
@@ -93,5 +93,7 @@ namespace Apache.Arrow
             IEnumerable<IArrowArray> arrays = _arrays.Select(array => 
ArrowArrayFactory.BuildArray(array.Data.Clone(allocator)));
             return new RecordBatch(Schema, arrays, Length);
         }
+
+        public override string ToString() => $"{nameof(RecordBatch)}: 
{ColumnCount} columns by {Length} rows";
     }
 }
diff --git a/csharp/src/Apache.Arrow/Schema.cs 
b/csharp/src/Apache.Arrow/Schema.cs
index 5d6b2b7bbd..608b967630 100644
--- a/csharp/src/Apache.Arrow/Schema.cs
+++ b/csharp/src/Apache.Arrow/Schema.cs
@@ -114,5 +114,7 @@ namespace Apache.Arrow
 
             return new Schema(fields, Metadata);
         }
+
+        public override string ToString() => $"{nameof(Schema)}: Num 
fields={_fieldsList.Count}, Num metadata={Metadata?.Count ?? 0}";
     }
 }
diff --git a/csharp/src/Apache.Arrow/Table.cs b/csharp/src/Apache.Arrow/Table.cs
index 939ec23f54..dd21cf1d0b 100644
--- a/csharp/src/Apache.Arrow/Table.cs
+++ b/csharp/src/Apache.Arrow/Table.cs
@@ -107,6 +107,8 @@ namespace Apache.Arrow
             return new Table(newSchema, newColumns);
         }
 
+        public override string ToString() => $"{nameof(Table)}: {ColumnCount} 
columns by {RowCount} rows";
+
         // TODO: Flatten for Tables with Lists/Structs?
     }
 }
diff --git a/csharp/test/Apache.Arrow.Tests/ArrowStreamWriterTests.cs 
b/csharp/test/Apache.Arrow.Tests/ArrowStreamWriterTests.cs
index 89595f99dc..c4c0b6ec9f 100644
--- a/csharp/test/Apache.Arrow.Tests/ArrowStreamWriterTests.cs
+++ b/csharp/test/Apache.Arrow.Tests/ArrowStreamWriterTests.cs
@@ -541,6 +541,10 @@ namespace Apache.Arrow.Tests
         public void WriteMultipleDictionaryArrays()
         {
             List<RecordBatch> originalRecordBatches = 
CreateMultipleDictionaryArraysTestData();
+            Assert.Equal("RecordBatch: 10 columns by 3 rows", 
originalRecordBatches[0].ToString());
+            Assert.Equal("Schema: Num fields=10, Num metadata=0", 
originalRecordBatches[0].Schema.ToString());
+            Assert.Equal("Field: Name=dictionaryField_int8, 
DataType=dictionary, IsNullable=False, Metadata count=0",
+                
originalRecordBatches[0].Schema.FieldsLookup["dictionaryField_int8"].Single().ToString());
             TestRoundTripRecordBatches(originalRecordBatches);
         }
 
diff --git a/csharp/test/Apache.Arrow.Tests/TableTests.cs 
b/csharp/test/Apache.Arrow.Tests/TableTests.cs
index 234dd63a79..9e23fa99a7 100644
--- a/csharp/test/Apache.Arrow.Tests/TableTests.cs
+++ b/csharp/test/Apache.Arrow.Tests/TableTests.cs
@@ -49,6 +49,8 @@ namespace Apache.Arrow.Tests
             Table table = MakeTableWithOneColumnOfTwoIntArrays(10);
             Assert.Equal(20, table.RowCount);
             Assert.Equal(1, table.ColumnCount);
+            Assert.Equal("Table: 1 columns by 20 rows", table.ToString());
+            Assert.Equal("ChunkedArray: Length=20, DataType=int32", 
table.Column(0).Data.ToString());
         }
 
         [Fact]
@@ -61,6 +63,7 @@ namespace Apache.Arrow.Tests
             Table table1 = Table.TableFromRecordBatches(recordBatch1.Schema, 
recordBatches);
             Assert.Equal(20, table1.RowCount);
             Assert.Equal(27, table1.ColumnCount);
+            Assert.Equal("ChunkedArray: Length=20, DataType=list", 
table1.Column(0).Data.ToString());
 
             FixedSizeBinaryType type = new FixedSizeBinaryType(17);
             Field newField1 = new Field(type.Name, type, false);
@@ -83,6 +86,9 @@ namespace Apache.Arrow.Tests
         public void TestTableAddRemoveAndSetColumn()
         {
             Table table = MakeTableWithOneColumnOfTwoIntArrays(10);
+            Assert.Equal("Table: 1 columns by 20 rows", table.ToString());
+            Assert.Equal("Field: Name=f0, DataType=int32, IsNullable=True, 
Metadata count=0", table.Column(0).Field.ToString());
+            Assert.Equal("ChunkedArray: Length=20, DataType=int32", 
table.Column(0).Data.ToString());
 
             Array nonEqualLengthIntArray = ColumnTests.MakeIntArray(10);
             Field field1 = new 
Field.Builder().Name("f1").DataType(Int32Type.Default).Build();

Reply via email to