eerhardt commented on a change in pull request #10527:
URL: https://github.com/apache/arrow/pull/10527#discussion_r666495112



##########
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);

Review comment:
       Do we really need to build a new IArrowArray here just to walk over the 
children? That seems wasteful to allocate that object.




-- 
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: github-unsubscr...@arrow.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to