eerhardt commented on a change in pull request #10527:
URL: https://github.com/apache/arrow/pull/10527#discussion_r666494514
##########
File path: csharp/src/Apache.Arrow/Ipc/ArrowStreamWriter.cs
##########
@@ -723,4 +868,62 @@ public virtual void Dispose()
}
}
}
+
+ internal static class DictionaryCollector
+ {
+ internal static void Collect(RecordBatch recordBatch, ref
DictionaryMemo dictionaryMemo)
+ {
+ Schema schema = recordBatch.Schema;
+ for (int i = 0; i < schema.Fields.Count; i++)
+ {
+ {
+ Field field = schema.GetFieldByIndex(i);
+ IArrowArray array = recordBatch.Column(i);
+
+ CollectDictionary(field, array, ref dictionaryMemo);
+ }
+ }
+ }
+
+ private static void CollectDictionary(Field field, IArrowArray array,
ref DictionaryMemo dictionaryMemo)
+ {
+ if (field.DataType.TypeId == ArrowTypeId.Dictionary)
+ {
+ IArrowArray dictionary = (array as DictionaryArray).Dictionary;
+ dictionaryMemo ??= new DictionaryMemo();
+ long id = dictionaryMemo.GetOrAssignId(field);
+
+ dictionaryMemo.AddOrReplaceDictionary(id, dictionary);
+ WalkChildren(dictionary, ref dictionaryMemo);
+ }
+ else
+ {
+ WalkChildren(array, ref dictionaryMemo);
+ }
+ }
+
+ private static void WalkChildren(IArrowArray array, ref DictionaryMemo
dictionaryMemo)
+ {
+ ArrayData[] children = array.Data.Children;
+
+ if (children == null)
+ {
+ return;
+ }
+
+ if (!(array.Data.DataType is NestedType nestedType))
+ {
+ return;
+ }
+
+ for (int i = 0; i < nestedType.Fields.Count; i++)
+ {
+ Field childField = nestedType.Fields[i];
+ ArrayData child = children[i];
+ IArrowArray childArray = ArrowArrayFactory.BuildArray(child);
+
+ CollectDictionary(childField, childArray, ref dictionaryMemo);
+ }
+ }
Review comment:
Seems simpler to say
```suggestion
if (array.Data.DataType is NestedType nestedType)
{
for (int i = 0; i < nestedType.Fields.Count; i++)
{
Field childField = nestedType.Fields[i];
ArrayData child = children[i];
IArrowArray childArray =
ArrowArrayFactory.BuildArray(child);
CollectDictionary(childField, childArray, ref
dictionaryMemo);
}
}
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]