diff --git a/csharp/src/Apache.Arrow/Arrays/ArrowArrayFactory.cs b/csharp/src/Apache.Arrow/Arrays/ArrowArrayFactory.cs index 319dcab17e75..9010d17525c1 100644 --- a/csharp/src/Apache.Arrow/Arrays/ArrowArrayFactory.cs +++ b/csharp/src/Apache.Arrow/Arrays/ArrowArrayFactory.cs @@ -57,6 +57,8 @@ public static IArrowArray BuildArray(ArrayData data) return new TimestampArray(data); case ArrowTypeId.List: return new ListArray(data); + case ArrowTypeId.Map: + return new MapArray(data); case ArrowTypeId.Struct: return new StructArray(data); case ArrowTypeId.Union: @@ -82,7 +84,6 @@ public static IArrowArray BuildArray(ArrayData data) throw new NotSupportedException("Half-float arrays are not supported by this target framework."); #endif case ArrowTypeId.Interval: - case ArrowTypeId.Map: default: throw new NotSupportedException($"An ArrowArray cannot be built for type {data.DataType.TypeId}."); } diff --git a/csharp/src/Apache.Arrow/Arrays/ListArray.cs b/csharp/src/Apache.Arrow/Arrays/ListArray.cs index 97673cb48e6a..0438f6c8c3e0 100644 --- a/csharp/src/Apache.Arrow/Arrays/ListArray.cs +++ b/csharp/src/Apache.Arrow/Arrays/ListArray.cs @@ -139,6 +139,14 @@ private ListArray(ArrayData data, IArrowArray values) : base(data) Values = values; } + // Constructor for child MapArray + internal ListArray(ArrayData data, IArrowArray values, ArrowTypeId typeId) : base(data) + { + data.EnsureBufferCount(2); + data.EnsureDataType(typeId); + Values = values; + } + public override void Accept(IArrowArrayVisitor visitor) => Accept(this, visitor); diff --git a/csharp/src/Apache.Arrow/Arrays/MapArray.cs b/csharp/src/Apache.Arrow/Arrays/MapArray.cs new file mode 100644 index 000000000000..5c6bd479c076 --- /dev/null +++ b/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 + { + public IArrowArrayBuilder> KeyBuilder { get; } + public IArrowArrayBuilder> ValueBuilder { get; } + + public int Length => ValueOffsetsBufferBuilder.Length; + + private ArrowBuffer.Builder 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(); + ValidityBufferBuilder = new ArrowBuffer.BitmapBuilder(); + DataType = type; + } + + /// + /// Start a new variable-length list slot + /// + /// This function should be called before beginning to append elements to the + /// value builder + /// + /// + 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); + return this; + } + + public Builder Resize(int length) + { + ValueOffsetsBufferBuilder.Resize(length + 1); + ValidityBufferBuilder.Resize(length + 1); + return this; + } + + public Builder Clear() + { + ValueOffsetsBufferBuilder.Clear(); + KeyBuilder.Clear(); + ValueBuilder.Clear(); + ValidityBufferBuilder.Clear(); + return this; + } + + } + + public StructArray KeyValues => base.Values as StructArray; + public IArrowArray Keys => KeyValues.Fields[0]; + public new IArrowArray Values => KeyValues.Fields[1]; + + public MapArray(IArrowType dataType, int length, + ArrowBuffer valueOffsetsBuffer, IArrowArray structs, + ArrowBuffer nullBitmapBuffer, int nullCount = 0, int offset = 0) + : this( + new ArrayData( + dataType, length, nullCount, offset, new[] { nullBitmapBuffer, valueOffsetsBuffer }, + new[] { structs.Data } + ), structs) + { + } + + public MapArray(ArrayData data) + : this(data, ArrowArrayFactory.BuildArray(data.Children[0])) + { + } + + private MapArray(ArrayData data, IArrowArray structs) : base(data, structs, ArrowTypeId.Map) + { + } + + public IEnumerable> GetTuples(int index, Func getKey, Func getValue) + where TKeyArray : Array where TValueArray : Array + { + ReadOnlySpan offsets = ValueOffsets; + // Get key values + int start = offsets[index]; + int end = offsets[index + 1]; + StructArray array = KeyValues.Slice(start, end - start) as StructArray; + + TKeyArray keyArray = array.Fields[0] as TKeyArray; + TValueArray valueArray = array.Fields[1] as TValueArray; + + for (int i = start; i < end; i++) + { + yield return new Tuple(getKey(keyArray, i), getValue(valueArray, i)); + } + } + + public IEnumerable> GetKeyValuePairs(int index, Func getKey, Func getValue) + where TKeyArray : Array where TValueArray : Array + { + ReadOnlySpan offsets = ValueOffsets; + // Get key values + int start = offsets[index]; + int end = offsets[index + 1]; + StructArray array = KeyValues.Slice(start, end - start) as StructArray; + + TKeyArray keyArray = array.Fields[0] as TKeyArray; + TValueArray valueArray = array.Fields[1] as TValueArray; + + for (int i = start; i < end; i++) + { + yield return new KeyValuePair(getKey(keyArray, i), getValue(valueArray, i)); + } + } + } +} diff --git a/csharp/src/Apache.Arrow/Ipc/ArrowReaderImplementation.cs b/csharp/src/Apache.Arrow/Ipc/ArrowReaderImplementation.cs index a1c143012401..d9d595e97408 100644 --- a/csharp/src/Apache.Arrow/Ipc/ArrowReaderImplementation.cs +++ b/csharp/src/Apache.Arrow/Ipc/ArrowReaderImplementation.cs @@ -231,7 +231,6 @@ private ArrayData LoadPrimitiveField( ByteBuffer bodyData, IBufferCreator bufferCreator) { - ArrowBuffer nullArrowBuffer = BuildArrowBuffer(bodyData, recordBatchEnumerator.CurrentBuffer, bufferCreator); if (!recordBatchEnumerator.MoveNextBuffer()) { diff --git a/csharp/src/Apache.Arrow/Ipc/ArrowTypeFlatbufferBuilder.cs b/csharp/src/Apache.Arrow/Ipc/ArrowTypeFlatbufferBuilder.cs index ee119ae5d7f2..7bea2f5aab56 100644 --- a/csharp/src/Apache.Arrow/Ipc/ArrowTypeFlatbufferBuilder.cs +++ b/csharp/src/Apache.Arrow/Ipc/ArrowTypeFlatbufferBuilder.cs @@ -65,7 +65,8 @@ class TypeVisitor : IArrowTypeVisitor, IArrowTypeVisitor, IArrowTypeVisitor, - IArrowTypeVisitor + IArrowTypeVisitor, + IArrowTypeVisitor { private FlatBufferBuilder Builder { get; } @@ -218,9 +219,16 @@ public void Visit(FixedSizeBinaryType type) Flatbuf.FixedSizeBinary.CreateFixedSizeBinary(Builder, type.ByteWidth)); } + public void Visit(MapType type) + { + Result = FieldType.Build( + Flatbuf.Type.Map, + Flatbuf.Map.CreateMap(Builder, type.KeySorted)); + } + public void Visit(IArrowType type) { - throw new NotImplementedException(); + throw new NotImplementedException($"Cannot visit type {type}"); } } diff --git a/csharp/src/Apache.Arrow/Ipc/MessageSerializer.cs b/csharp/src/Apache.Arrow/Ipc/MessageSerializer.cs index a09fff61b4ac..d64159636dd4 100644 --- a/csharp/src/Apache.Arrow/Ipc/MessageSerializer.cs +++ b/csharp/src/Apache.Arrow/Ipc/MessageSerializer.cs @@ -193,6 +193,13 @@ private static Types.IArrowType GetFieldArrowType(Flatbuf.Field field, Field[] c case Flatbuf.Type.Struct_: Debug.Assert(childFields != null); return new Types.StructType(childFields); + case Flatbuf.Type.Map: + if (childFields == null || childFields.Length != 1) + { + throw new InvalidDataException($"Map type must have exactly one struct child."); + } + Flatbuf.Map meta = field.Type().Value; + return new Types.MapType(childFields[0], meta.KeysSorted); default: throw new InvalidDataException($"Arrow primitive '{field.TypeType}' is unsupported."); } diff --git a/csharp/src/Apache.Arrow/Types/ListType.cs b/csharp/src/Apache.Arrow/Types/ListType.cs index a006c2282dd1..b467934ec541 100644 --- a/csharp/src/Apache.Arrow/Types/ListType.cs +++ b/csharp/src/Apache.Arrow/Types/ListType.cs @@ -13,8 +13,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -using System; - namespace Apache.Arrow.Types { public sealed class ListType : NestedType diff --git a/csharp/src/Apache.Arrow/Types/MapType.cs b/csharp/src/Apache.Arrow/Types/MapType.cs new file mode 100644 index 000000000000..b67cfec85345 --- /dev/null +++ b/csharp/src/Apache.Arrow/Types/MapType.cs @@ -0,0 +1,36 @@ +using System.Collections.Generic; + +namespace Apache.Arrow.Types +{ + public sealed class MapType : NestedType // MapType = ListType(StrucType("key", "value")) + { + public override ArrowTypeId TypeId => ArrowTypeId.Map; + public override string Name => "map"; + public readonly bool KeySorted; + + public StructType KeyValueType => Fields[0].DataType as StructType; + public Field KeyField => KeyValueType.Fields[0]; + public Field ValueField => KeyValueType.Fields[1]; + + public MapType(IArrowType key, IArrowType value, bool nullable = true, bool keySorted = false) + : this(new Field("key", key, false), new Field("value", value, nullable), keySorted) + { + } + + public MapType(Field key, Field value, bool keySorted = false) + : this(new StructType(new List() { key, value }), keySorted) + { + } + + public MapType(StructType entries, bool keySorted = false) : this(new Field("entries", entries, false), keySorted) + { + } + + public MapType(Field entries, bool keySorted = false) : base(entries) + { + KeySorted = keySorted; + } + + public override void Accept(IArrowTypeVisitor visitor) => Accept(this, visitor); + } +} diff --git a/csharp/test/Apache.Arrow.Tests/MapArrayTests.cs b/csharp/test/Apache.Arrow.Tests/MapArrayTests.cs new file mode 100644 index 000000000000..034f120f3f01 --- /dev/null +++ b/csharp/test/Apache.Arrow.Tests/MapArrayTests.cs @@ -0,0 +1,92 @@ +// 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 System.Linq; +using Apache.Arrow.Types; +using Xunit; + +namespace Apache.Arrow.Tests +{ + public class MapArrayTests + { + [Fact] + public void MapArray_Should_GetTuple() + { + 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; + + Tuple kv0 = Tuple.Create("test", (long?)1); + Tuple kv1 = Tuple.Create("other", (long?)123); + Tuple kv2 = Tuple.Create("kv", (long?)null); + + builder.Append(); + keyBuilder.Append("test"); + valueBuilder.Append(1); + + builder.AppendNull(); + + builder.Append(); + keyBuilder.Append("other"); + valueBuilder.Append(123); + keyBuilder.Append("kv"); + valueBuilder.AppendNull(); + + MapArray array = builder.Build(); + + Assert.Equal(new Tuple[] { kv0 }, array.GetTuples(0, GetKey, GetValue).ToArray()); + Assert.True(array.IsNull(1)); + Assert.Equal(new Tuple[] { kv1, kv2 }, array.GetTuples(2, GetKey, GetValue).ToArray()); + } + + [Fact] + public void MapArray_Should_GetKeyValuePairs() + { + MapType type = new MapType(StringType.Default, Int32Type.Default); + MapArray.Builder builder = new MapArray.Builder(type); + var keyBuilder = builder.KeyBuilder as StringArray.Builder; + var valueBuilder = builder.ValueBuilder as Int32Array.Builder; + + KeyValuePair kv0 = KeyValuePair.Create("test", (int?)1); + KeyValuePair kv1 = KeyValuePair.Create("other", (int?)123); + KeyValuePair kv2 = KeyValuePair.Create("kv", (int?)null); + + builder.Append(); + keyBuilder.Append("test"); + valueBuilder.Append(1); + + builder.AppendNull(); + + builder.Append(); + keyBuilder.Append("other"); + valueBuilder.Append(123); + keyBuilder.Append("kv"); + valueBuilder.AppendNull(); + + MapArray array = builder.Build(); + + Assert.Equal(new KeyValuePair[] { kv0 }, array.GetKeyValuePairs(0, GetKey, GetValue).ToArray()); + Assert.True(array.IsNull(1)); + Assert.Equal(new KeyValuePair[] { kv1, kv2 }, array.GetKeyValuePairs(2, GetKey, GetValue).ToArray()); + } + + 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); + } +} diff --git a/csharp/test/Apache.Arrow.Tests/TableTests.cs b/csharp/test/Apache.Arrow.Tests/TableTests.cs index 45a14cc25616..b4c4b1faed19 100644 --- a/csharp/test/Apache.Arrow.Tests/TableTests.cs +++ b/csharp/test/Apache.Arrow.Tests/TableTests.cs @@ -60,7 +60,7 @@ public void TestTableFromRecordBatches() Table table1 = Table.TableFromRecordBatches(recordBatch1.Schema, recordBatches); Assert.Equal(20, table1.RowCount); - Assert.Equal(23, table1.ColumnCount); + Assert.Equal(24, table1.ColumnCount); FixedSizeBinaryType type = new FixedSizeBinaryType(17); Field newField1 = new Field(type.Name, type, false); diff --git a/csharp/test/Apache.Arrow.Tests/TestData.cs b/csharp/test/Apache.Arrow.Tests/TestData.cs index 02186378ad91..bc99035c3842 100644 --- a/csharp/test/Apache.Arrow.Tests/TestData.cs +++ b/csharp/test/Apache.Arrow.Tests/TestData.cs @@ -54,6 +54,7 @@ public static RecordBatch CreateSampleRecordBatch(int length, int columnSetCount builder.Field(CreateField(new StructType(new List { CreateField(StringType.Default, i), CreateField(Int32Type.Default, i) }), i)); builder.Field(CreateField(new Decimal128Type(10, 6), i)); builder.Field(CreateField(new Decimal256Type(16, 8), i)); + builder.Field(CreateField(new MapType(StringType.Default, Int32Type.Default), i)); if (createAdvancedTypeArrays) { @@ -126,7 +127,8 @@ private class ArrayCreator : IArrowTypeVisitor, IArrowTypeVisitor, IArrowTypeVisitor, - IArrowTypeVisitor + IArrowTypeVisitor, + IArrowTypeVisitor { private int Length { get; } public IArrowArray Array { get; private set; } @@ -317,6 +319,25 @@ public void Visit(FixedSizeBinaryType type) Array = new FixedSizeBinaryArray(arrayData); } + public void Visit(MapType type) + { + MapArray.Builder builder = new MapArray.Builder(type).Reserve(Length); + var keyBuilder = builder.KeyBuilder.Reserve(Length + 1) as StringArray.Builder; + var valueBuilder = builder.ValueBuilder.Reserve(Length + 1) as Int32Array.Builder; + + for (var i = 0; i < Length; i++) + { + builder.Append(); + keyBuilder.Append(i.ToString()); + valueBuilder.Append(i); + } + //Add a value to check if Values.Length can exceed MapArray.Length + keyBuilder.Append("0"); + valueBuilder.Append(0); + + Array = builder.Build(); + } + private void GenerateArray(IArrowArrayBuilder builder, Func generator) where TArrayBuilder : IArrowArrayBuilder where TArray : IArrowArray diff --git a/csharp/test/Apache.Arrow.Tests/Types/MapTypeTests.cs b/csharp/test/Apache.Arrow.Tests/Types/MapTypeTests.cs new file mode 100644 index 000000000000..ba306ec104e7 --- /dev/null +++ b/csharp/test/Apache.Arrow.Tests/Types/MapTypeTests.cs @@ -0,0 +1,57 @@ +// 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 Apache.Arrow.Types; +using Xunit; + +namespace Apache.Arrow.Tests.Types +{ + public class MapTypeTests + { + [Fact] + public void MapType_Should_HaveCorrectTypeId() + { + var type = new MapType(StringType.Default, Int32Type.Default); + Assert.Equal(ArrowTypeId.Map, type.TypeId); + } + + [Fact] + public void MapType_Should_HaveCorrectStructType() + { + var type = new MapType(BooleanType.Default, Int32Type.Default, true); + Assert.IsType(type.Fields[0].DataType); + Assert.Equal(2, type.KeyValueType.Fields.Count); + + Assert.Equal("entries", type.Fields[0].Name); + Assert.Equal("key", type.KeyField.Name); + Assert.Equal("value", type.ValueField.Name); + + Assert.False(type.Fields[0].IsNullable); + Assert.False(type.KeyField.IsNullable); + Assert.True(type.ValueField.IsNullable); + Assert.False(new MapType(BooleanType.Default, Int32Type.Default, false).ValueField.IsNullable); + + Assert.IsType(type.KeyField.DataType); + Assert.IsType(type.ValueField.DataType); + } + + [Fact] + public void MapType_Should_SetKeySorted() + { + Assert.False(new MapType(BooleanType.Default, Int32Type.Default).KeySorted); + Assert.True(new MapType(StringType.Default, Int32Type.Default, true, true).KeySorted); + } + } +}