Skip to content

Commit

Permalink
Normalize map types on creation
Browse files Browse the repository at this point in the history
  • Loading branch information
CurtHagenlocher committed Dec 11, 2023
1 parent ee8fe2f commit 03ba59f
Showing 1 changed file with 44 additions and 4 deletions.
48 changes: 44 additions & 4 deletions csharp/src/Apache.Arrow/Types/MapType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using System.Collections.Generic;

namespace Apache.Arrow.Types
{
public sealed class MapType : NestedType // MapType = ListType(StructType("key", "value"))
{
private const string EntriesKey = "entries";
private const string KeyKey = "key";
private const string ValueKey = "value";

public override ArrowTypeId TypeId => ArrowTypeId.Map;
public override string Name => "map";
public readonly bool KeySorted;
Expand All @@ -28,20 +33,23 @@ public sealed class MapType : NestedType // MapType = ListType(StructType("key",
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)
: base(Entries(key, value, nullable))
{
KeySorted = keySorted;
}

public MapType(Field key, Field value, bool keySorted = false)
: this(new StructType(new List<Field>() { key, value }), keySorted)
: base(Entries(key, value))
{
KeySorted = keySorted;
}

public MapType(StructType entries, bool keySorted = false) : this(new Field("entries", entries, false), keySorted)
public MapType(StructType entries, bool keySorted = false) : base(Entries(entries))
{
KeySorted = keySorted;
}

public MapType(Field entries, bool keySorted = false) : base(entries)
public MapType(Field entries, bool keySorted = false) : base(Entries(entries))
{
KeySorted = keySorted;
}
Expand All @@ -54,5 +62,37 @@ public MapType UnsortedKey()

return new MapType(Fields[0], keySorted: false);
}

private static Field Entries(IArrowType key, IArrowType value, bool nullable) =>
new Field(EntriesKey, NewStruct(new Field(KeyKey, key, false), new Field(ValueKey, value, nullable)), false);

private static Field Entries(Field key, Field value) =>
new Field(EntriesKey, NewStruct(NamedField(KeyKey, key), NamedField(ValueKey, value)), false);

private static Field Entries(StructType entries)
{
return new Field(EntriesKey, Struct(entries), false);
}

private static StructType NewStruct(Field key, Field value) => new StructType(new[] { key, value });

private static StructType Struct(StructType entries)
{
Field key = NamedField(KeyKey, entries.Fields[0]);
Field value = NamedField(ValueKey, entries.Fields[1]);
return object.ReferenceEquals(key, entries.Fields[0]) && object.ReferenceEquals(value, entries.Fields[1]) ? entries : NewStruct(key, value);
}

private static Field Entries(Field entries)
{
StructType structType = (StructType)entries.DataType;
StructType adjustedStruct = Struct(structType);
return StringComparer.Ordinal.Equals(entries.Name, EntriesKey) && object.ReferenceEquals(structType, adjustedStruct) ? entries : new Field(EntriesKey, adjustedStruct, false);
}

private static Field NamedField(string name, Field field)
{
return StringComparer.Ordinal.Equals(name, field.Name) ? field : new Field(name, field.DataType, field.IsNullable, field.Metadata);
}
}
}

0 comments on commit 03ba59f

Please sign in to comment.