Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-30717: [C#] Add ToString() methods to Arrow classes #36566

Merged
merged 7 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading