CurtHagenlocher commented on code in PR #37885: URL: https://github.com/apache/arrow/pull/37885#discussion_r1342716505
########## csharp/src/Apache.Arrow/Arrays/MapArray.cs: ########## @@ -0,0 +1,174 @@ +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to You under the Apache License, Version 2.0 +// (the "License"); you may not use this file except in compliance with +// the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using System; +using System.Collections.Generic; +using Apache.Arrow.Memory; +using Apache.Arrow.Types; + +namespace Apache.Arrow +{ + public class MapArray : ListArray // MapArray = ListArray(StrucArray("key", "value")) + { + // Same as ListArray.Builder, but with KeyBuilder + public new class Builder : IArrowArrayBuilder<MapArray, Builder> + { + public IArrowArrayBuilder<IArrowArray, IArrowArrayBuilder<IArrowArray>> KeyBuilder { get; } + public IArrowArrayBuilder<IArrowArray, IArrowArrayBuilder<IArrowArray>> ValueBuilder { get; } + + public int Length => ValueOffsetsBufferBuilder.Length; + + private ArrowBuffer.Builder<int> ValueOffsetsBufferBuilder { get; } + + private ArrowBuffer.BitmapBuilder ValidityBufferBuilder { get; } + + public int NullCount { get; protected set; } + + public MapType DataType { get; } + + public Builder(MapType type) + { + KeyBuilder = ArrowArrayBuilderFactory.Build(type.KeyField.DataType); + ValueBuilder = ArrowArrayBuilderFactory.Build(type.ValueField.DataType); + ValueOffsetsBufferBuilder = new ArrowBuffer.Builder<int>(); + ValidityBufferBuilder = new ArrowBuffer.BitmapBuilder(); + DataType = type; + } + + /// <summary> + /// Start a new variable-length list slot + /// + /// This function should be called before beginning to append elements to the + /// value builder + /// </summary> + /// <returns></returns> + public Builder Append() + { + ValueOffsetsBufferBuilder.Append(KeyBuilder.Length); + ValidityBufferBuilder.Append(true); + + return this; + } + + public Builder AppendNull() + { + ValueOffsetsBufferBuilder.Append(KeyBuilder.Length); + ValidityBufferBuilder.Append(false); + NullCount++; + + return this; + } + + public MapArray Build(MemoryAllocator allocator = default) + { + ValueOffsetsBufferBuilder.Append(KeyBuilder.Length); + + ArrowBuffer validityBuffer = NullCount > 0 ? ValidityBufferBuilder.Build(allocator) : ArrowBuffer.Empty; + + StructArray structs = new StructArray( + DataType.KeyValueType, KeyBuilder.Length, + new IArrowArray[] { KeyBuilder.Build(allocator), ValueBuilder.Build(allocator) }, + ArrowBuffer.Empty, 0 + ); + + return new MapArray(DataType, Length - 1, ValueOffsetsBufferBuilder.Build(allocator), structs, validityBuffer, NullCount); + } + + public Builder Reserve(int capacity) + { + ValueOffsetsBufferBuilder.Reserve(capacity + 1); + ValidityBufferBuilder.Reserve(capacity + 1); Review Comment: My guess is that this was originally copied from the code for ListArray, which looks the same. In fact it looks like several of the other implementations have an unnecessary +1 for the validity buffer capacity so I'll fix all of them. -- 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]
