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 e75bc99fa8 GH-40788: [C#] Override Accept in MapArray (#40789)
e75bc99fa8 is described below
commit e75bc99fa862a6703d83f44c027a52043851c530
Author: Adam Reeve <[email protected]>
AuthorDate: Tue Mar 26 14:44:54 2024 +1300
GH-40788: [C#] Override Accept in MapArray (#40789)
### Rationale for this change
This allows users to implement `IArrowArrayVisitor<MapArray>` and have the
`Visit(MapArray)` method called instead of `Visit(ListArray)` or
`Visit(IArrowArray)`.
### What changes are included in this PR?
Overrides the `Accept` method to check whether the visitor implements
`Visit(MapArray)`, and if not, delegates to the base implementation to handle
`IArrowArrayVisitor<ListArray>` or fall back to using an `IArrowArrayVisitor`.
### Are these changes tested?
Yes, I've added unit tests.
### Are there any user-facing changes?
Yes, this is a user-facing change.
* GitHub Issue: #40788
Authored-by: Adam Reeve <[email protected]>
Signed-off-by: Curt Hagenlocher <[email protected]>
---
csharp/src/Apache.Arrow/Arrays/MapArray.cs | 13 +++
csharp/test/Apache.Arrow.Tests/MapArrayTests.cs | 110 ++++++++++++++++++++++++
2 files changed, 123 insertions(+)
diff --git a/csharp/src/Apache.Arrow/Arrays/MapArray.cs
b/csharp/src/Apache.Arrow/Arrays/MapArray.cs
index a6676b134e..dad50981ea 100644
--- a/csharp/src/Apache.Arrow/Arrays/MapArray.cs
+++ b/csharp/src/Apache.Arrow/Arrays/MapArray.cs
@@ -135,6 +135,19 @@ namespace Apache.Arrow
{
}
+ public override void Accept(IArrowArrayVisitor visitor)
+ {
+ switch (visitor)
+ {
+ case IArrowArrayVisitor<MapArray> typedVisitor:
+ typedVisitor.Visit(this);
+ break;
+ default:
+ base.Accept(visitor);
+ break;
+ }
+ }
+
public IEnumerable<Tuple<K, V>> GetTuples<TKeyArray, K, TValueArray,
V>(int index, Func<TKeyArray, int, K> getKey, Func<TValueArray, int, V>
getValue)
where TKeyArray : Array where TValueArray : Array
{
diff --git a/csharp/test/Apache.Arrow.Tests/MapArrayTests.cs
b/csharp/test/Apache.Arrow.Tests/MapArrayTests.cs
index 7f35f10426..21decdacc0 100644
--- a/csharp/test/Apache.Arrow.Tests/MapArrayTests.cs
+++ b/csharp/test/Apache.Arrow.Tests/MapArrayTests.cs
@@ -85,8 +85,118 @@ namespace Apache.Arrow.Tests
Assert.Equal(new KeyValuePair<string, int?>[] { kv1, kv2 },
array.GetKeyValuePairs<StringArray, string, Int32Array, int?>(2, GetKey,
GetValue).ToArray());
}
+ [Fact]
+ public void MapArray_Should_AcceptMapVisitor()
+ {
+ var mapArray = BuildMapArray();
+ var visitor = new MapOnlyVisitor();
+ mapArray.Accept(visitor);
+
+ Assert.True(visitor.MapVisited);
+ Assert.False(visitor.BaseVisited);
+ }
+
+ [Fact]
+ public void MapArray_Should_AcceptListVisitor()
+ {
+ var mapArray = BuildMapArray();
+ var visitor = new ListOnlyVisitor();
+ mapArray.Accept(visitor);
+
+ Assert.True(visitor.ListVisited);
+ Assert.False(visitor.BaseVisited);
+ }
+
+ [Fact]
+ public void MapArray_Should_AcceptListAndMapVisitor()
+ {
+ var mapArray = BuildMapArray();
+ var visitor = new MapAndListVisitor();
+ mapArray.Accept(visitor);
+
+ Assert.True(visitor.MapVisited);
+ Assert.False(visitor.ListVisited);
+ Assert.False(visitor.BaseVisited);
+ }
+
+ private static MapArray BuildMapArray()
+ {
+ MapType type = new MapType(StringType.Default, Int64Type.Default);
+ MapArray.Builder builder = new MapArray.Builder(type);
+ var keyBuilder = builder.KeyBuilder as StringArray.Builder;
+ var valueBuilder = builder.ValueBuilder as Int64Array.Builder;
+
+ builder.Append();
+ keyBuilder.Append("test");
+ valueBuilder.Append(1);
+
+ builder.AppendNull();
+
+ builder.Append();
+ keyBuilder.Append("other");
+ valueBuilder.Append(123);
+ keyBuilder.Append("kv");
+ valueBuilder.AppendNull();
+
+ return builder.Build();
+ }
+
private static string GetKey(StringArray array, int index) =>
array.GetString(index);
private static int? GetValue(Int32Array array, int index) =>
array.GetValue(index);
private static long? GetValue(Int64Array array, int index) =>
array.GetValue(index);
+
+ private sealed class MapOnlyVisitor : IArrowArrayVisitor<MapArray>
+ {
+ public bool MapVisited = false;
+ public bool BaseVisited = false;
+
+ public void Visit(MapArray array)
+ {
+ MapVisited = true;
+ }
+
+ public void Visit(IArrowArray array)
+ {
+ BaseVisited = true;
+ }
+ }
+
+ private sealed class ListOnlyVisitor : IArrowArrayVisitor<ListArray>
+ {
+ public bool ListVisited = false;
+ public bool BaseVisited = false;
+
+ public void Visit(ListArray array)
+ {
+ ListVisited = true;
+ }
+
+ public void Visit(IArrowArray array)
+ {
+ BaseVisited = true;
+ }
+ }
+
+ private sealed class MapAndListVisitor : IArrowArrayVisitor<MapArray>,
IArrowArrayVisitor<ListArray>
+ {
+ public bool MapVisited = false;
+ public bool ListVisited = false;
+ public bool BaseVisited = false;
+
+ public void Visit(MapArray array)
+ {
+ MapVisited = true;
+ }
+
+ public void Visit(ListArray array)
+ {
+ ListVisited = true;
+ }
+
+ public void Visit(IArrowArray array)
+ {
+ BaseVisited = true;
+ }
+ }
}
}