Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-38757: [C#] Implement common interfaces for structure arrays and record batches #38759

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions csharp/src/Apache.Arrow/Arrays/StructArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

namespace Apache.Arrow
{
public class StructArray : Array
public class StructArray : Array, IArrowRecord
{
private IReadOnlyList<IArrowArray> _fields;

Expand All @@ -44,7 +44,21 @@ public StructArray(ArrayData data)
data.EnsureDataType(ArrowTypeId.Struct);
}

public override void Accept(IArrowArrayVisitor visitor) => Accept(this, visitor);
public override void Accept(IArrowArrayVisitor visitor)
{
switch (visitor)
{
case IArrowArrayVisitor<StructArray> structArrayVisitor:
structArrayVisitor.Visit(this);
break;
case IArrowArrayVisitor<IArrowRecord> arrowStructVisitor:
arrowStructVisitor.Visit(this);
break;
default:
visitor.Visit(this);
break;
}
}

private IReadOnlyList<IArrowArray> InitializeFields()
{
Expand All @@ -55,5 +69,14 @@ private IReadOnlyList<IArrowArray> InitializeFields()
}
return result;
}

IRecordType IArrowRecord.Schema => (StructType)Data.DataType;

int IArrowRecord.ColumnCount => _fields.Count;

IArrowArray IArrowRecord.Column(string columnName, IEqualityComparer<string> comparer) =>
_fields[((StructType)Data.DataType).GetFieldIndex(columnName, comparer)];

IArrowArray IArrowRecord.Column(int columnIndex) => _fields[columnIndex];
}
}
29 changes: 29 additions & 0 deletions csharp/src/Apache.Arrow/Interfaces/IArrowRecord.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// 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.Collections.Generic;
using Apache.Arrow.Types;

namespace Apache.Arrow
{
public interface IArrowRecord : IArrowArray
{
IRecordType Schema { get; }
int ColumnCount { get; }

IArrowArray Column(string columnName, IEqualityComparer<string> comparer = default);
IArrowArray Column(int columnIndex);
}
}
34 changes: 32 additions & 2 deletions csharp/src/Apache.Arrow/RecordBatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@
using System.Diagnostics;
using System.Linq;
using Apache.Arrow.Memory;
using Apache.Arrow.Types;

namespace Apache.Arrow
{
public partial class RecordBatch : IDisposable
public partial class RecordBatch : IArrowRecord
{
public Schema Schema { get; }
public int ColumnCount => _arrays.Count;
Expand All @@ -41,7 +42,12 @@ public IArrowArray Column(int i)

public IArrowArray Column(string columnName)
{
int fieldIndex = Schema.GetFieldIndex(columnName);
return Column(columnName, null);
}

public IArrowArray Column(string columnName, IEqualityComparer<string> comparer)
{
int fieldIndex = Schema.GetFieldIndex(columnName, comparer);
return _arrays[fieldIndex];
}

Expand Down Expand Up @@ -94,6 +100,30 @@ public RecordBatch Clone(MemoryAllocator allocator = default)
return new RecordBatch(Schema, arrays, Length);
}

public void Accept(IArrowArrayVisitor visitor)
{
switch (visitor)
{
case IArrowArrayVisitor<RecordBatch> recordBatchVisitor:
recordBatchVisitor.Visit(this);
break;
case IArrowArrayVisitor<IArrowRecord> arrowStructVisitor:
arrowStructVisitor.Visit(this);
break;
default:
visitor.Visit(this);
break;
}
}

public override string ToString() => $"{nameof(RecordBatch)}: {ColumnCount} columns by {Length} rows";

IRecordType IArrowRecord.Schema => this.Schema;
int IArrowArray.NullCount => 0;
int IArrowArray.Offset => 0;
ArrayData IArrowArray.Data => throw new NotSupportedException("Unable to get data for RecordBatch");

bool IArrowArray.IsNull(int index) => false;
bool IArrowArray.IsValid(int index) => true;
}
}
34 changes: 31 additions & 3 deletions csharp/src/Apache.Arrow/Schema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using Apache.Arrow.Types;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;

namespace Apache.Arrow
{
public partial class Schema
public partial class Schema : IRecordType
{
[Obsolete("Use `FieldsList` or `FieldsLookup` instead")]
public IReadOnlyDictionary<string, Field> Fields => _fieldsDictionary;
Expand Down Expand Up @@ -71,11 +72,17 @@ internal Schema(List<Field> fieldsList, IReadOnlyDictionary<string, string> meta

public Field GetFieldByName(string name) => FieldsLookup[name].FirstOrDefault();

public int GetFieldIndex(string name, StringComparer comparer = default)
public int GetFieldIndex(string name, StringComparer comparer)
{
IEqualityComparer<string> equalityComparer = (IEqualityComparer<string>)comparer;
return GetFieldIndex(name, equalityComparer);
}

public int GetFieldIndex(string name, IEqualityComparer<string> comparer = default)
{
comparer ??= StringComparer.CurrentCulture;

return _fieldsList.IndexOf(_fieldsList.First(x => comparer.Compare(x.Name, name) == 0));
return _fieldsList.IndexOf(_fieldsList.First(x => comparer.Equals(x.Name, name)));
}

public Schema RemoveField(int fieldIndex)
Expand Down Expand Up @@ -115,6 +122,27 @@ public Schema SetField(int fieldIndex, Field newField)
return new Schema(fields, Metadata);
}

public void Accept(IArrowTypeVisitor visitor)
{
if (visitor is IArrowTypeVisitor<Schema> schemaVisitor)
{
schemaVisitor.Visit(this);
}
else if (visitor is IArrowTypeVisitor<IRecordType> interfaceVisitor)
{
interfaceVisitor.Visit(this);
}
else
{
visitor.Visit(this);
}
}

public override string ToString() => $"{nameof(Schema)}: Num fields={_fieldsList.Count}, Num metadata={Metadata?.Count ?? 0}";

int IRecordType.FieldCount => _fieldsList.Count;
string IArrowType.Name => "RecordBatch";
ArrowTypeId IArrowType.TypeId => ArrowTypeId.RecordBatch;
bool IArrowType.IsFixedWidth => false;
}
}
1 change: 1 addition & 0 deletions csharp/src/Apache.Arrow/Types/IArrowType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public enum ArrowTypeId
Map,
FixedSizeList,
Duration,
RecordBatch,
}

public interface IArrowType
Expand Down
28 changes: 28 additions & 0 deletions csharp/src/Apache.Arrow/Types/IRecordType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// 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.Collections.Generic;

namespace Apache.Arrow.Types
{
public interface IRecordType : IArrowType
{
int FieldCount { get; }

Field GetFieldByIndex(int index);
Field GetFieldByName(string name);
int GetFieldIndex(string name, IEqualityComparer<string> comparer);
}
}
24 changes: 22 additions & 2 deletions csharp/src/Apache.Arrow/Types/StructType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@

namespace Apache.Arrow.Types
{
public sealed class StructType : NestedType
public sealed class StructType : NestedType, IRecordType
{
public override ArrowTypeId TypeId => ArrowTypeId.Struct;
public override string Name => "struct";

public StructType(IReadOnlyList<Field> fields) : base(fields)
{ }

public Field GetFieldByIndex(int index) => Fields[index];

public Field GetFieldByName(string name,
IEqualityComparer<string> comparer = default)
{
Expand Down Expand Up @@ -56,6 +58,24 @@ public int GetFieldIndex(string name,
return -1;
}

public override void Accept(IArrowTypeVisitor visitor) => Accept(this, visitor);
public override void Accept(IArrowTypeVisitor visitor)
{
if (visitor is IArrowTypeVisitor<StructType> structTypeVisitor)
{
structTypeVisitor.Visit(this);
}
else if (visitor is IArrowTypeVisitor<IRecordType> interfaceVisitor)
{
interfaceVisitor.Visit(this);
}
else
{
visitor.Visit(this);
}
}

int IRecordType.FieldCount => Fields.Count;

Field IRecordType.GetFieldByName(string name) => GetFieldByName(name);
}
}
11 changes: 11 additions & 0 deletions csharp/test/Apache.Arrow.Tests/FieldComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// limitations under the License.

using System.Linq;
using Apache.Arrow.Types;
using Xunit;

namespace Apache.Arrow.Tests
Expand All @@ -40,5 +41,15 @@ public static void Compare(Field expected, Field actual)

actual.DataType.Accept(new ArrayTypeComparer(expected.DataType));
}

public static void Compare(IRecordType expected, IRecordType actual)
{
Assert.Equal(expected.FieldCount, actual.FieldCount);

for (int i = 0; i < expected.FieldCount; i++)
{
Compare(expected.GetFieldByIndex(i), actual.GetFieldByIndex(i));
}
}
}
}
Loading
Loading