Skip to content

Commit

Permalink
apacheGH-30717: [C#] Add ToString() methods to Arrow classes (apache#…
Browse files Browse the repository at this point in the history
…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 apache#30717.
* Closes: apache#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]>
  • Loading branch information
3 people authored Oct 30, 2023
1 parent 23b62a4 commit 0026c0c
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 0 deletions.
2 changes: 2 additions & 0 deletions csharp/src/Apache.Arrow/ChunkedArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Array> arrays)
{
IArrowArray[] arrowArrays = new IArrowArray[arrays.Count];
Expand Down
2 changes: 2 additions & 0 deletions csharp/src/Apache.Arrow/Field.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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}";
}
}
2 changes: 2 additions & 0 deletions csharp/src/Apache.Arrow/RecordBatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,7 @@ public RecordBatch Clone(MemoryAllocator allocator = default)
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";
}
}
2 changes: 2 additions & 0 deletions csharp/src/Apache.Arrow/Schema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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}";
}
}
2 changes: 2 additions & 0 deletions csharp/src/Apache.Arrow/Table.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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?
}
}
4 changes: 4 additions & 0 deletions csharp/test/Apache.Arrow.Tests/ArrowStreamWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,10 @@ public async Task WriteMultipleDictionaryArraysAsync()
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);
}

Expand Down
6 changes: 6 additions & 0 deletions csharp/test/Apache.Arrow.Tests/TableTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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);
Expand All @@ -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();
Expand Down

0 comments on commit 0026c0c

Please sign in to comment.