From 0026c0cda71c44b73a8b635bd5245b49b9cabb80 Mon Sep 17 00:00:00 2001 From: Gavin Murrison <2135106+voidstar69@users.noreply.github.com> Date: Mon, 30 Oct 2023 16:17:15 +0000 Subject: [PATCH] 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 <2135106+voidstar69@users.noreply.github.com> Co-authored-by: voidstar69 Co-authored-by: Weston Pace Signed-off-by: Curt Hagenlocher --- 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 f5909f5adfe48..85b3560c229f2 100644 --- a/csharp/src/Apache.Arrow/ChunkedArray.cs +++ b/csharp/src/Apache.Arrow/ChunkedArray.cs @@ -92,6 +92,8 @@ public ChunkedArray Slice(long offset) return Slice(offset, Length - offset); } + public override string ToString() => $"{nameof(ChunkedArray)}: Length={Length}, DataType={DataType.Name}"; + private static IArrowArray[] Cast(IList arrays) { IArrowArray[] arrowArrays = new IArrowArray[arrays.Count]; diff --git a/csharp/src/Apache.Arrow/Field.cs b/csharp/src/Apache.Arrow/Field.cs index 562b9587bddb9..4fddd1bc4e2de 100644 --- a/csharp/src/Apache.Arrow/Field.cs +++ b/csharp/src/Apache.Arrow/Field.cs @@ -61,5 +61,7 @@ private Field(string name, IArrowType dataType, bool nullable, bool allowBlankNa 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 f87081d2987ac..566c77830265e 100644 --- a/csharp/src/Apache.Arrow/RecordBatch.cs +++ b/csharp/src/Apache.Arrow/RecordBatch.cs @@ -93,5 +93,7 @@ public RecordBatch Clone(MemoryAllocator allocator = default) IEnumerable 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 5d6b2b7bbdc4c..608b967630079 100644 --- a/csharp/src/Apache.Arrow/Schema.cs +++ b/csharp/src/Apache.Arrow/Schema.cs @@ -114,5 +114,7 @@ public Schema SetField(int fieldIndex, Field newField) 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 939ec23f54ff2..dd21cf1d0b39c 100644 --- a/csharp/src/Apache.Arrow/Table.cs +++ b/csharp/src/Apache.Arrow/Table.cs @@ -107,6 +107,8 @@ public Table SetColumn(int columnIndex, Column column) 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 89595f99dc0e4..c4c0b6ec9ff21 100644 --- a/csharp/test/Apache.Arrow.Tests/ArrowStreamWriterTests.cs +++ b/csharp/test/Apache.Arrow.Tests/ArrowStreamWriterTests.cs @@ -541,6 +541,10 @@ public async Task WriteMultipleDictionaryArraysAsync() public void WriteMultipleDictionaryArrays() { List 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 234dd63a79cd2..9e23fa99a769c 100644 --- a/csharp/test/Apache.Arrow.Tests/TableTests.cs +++ b/csharp/test/Apache.Arrow.Tests/TableTests.cs @@ -49,6 +49,8 @@ public void TestTableBasics() 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 @@ public void TestTableFromRecordBatches() 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 @@ public void TestTableFromRecordBatches() 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();