Skip to content
Closed
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
26 changes: 14 additions & 12 deletions csharp/src/Apache.Arrow/Schema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public partial class Schema : IRecordType
private readonly List<Field> _fieldsList;

public ILookup<string, Field> FieldsLookup { get; }
private readonly ILookup<string, int> _fieldsIndexLookup;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we also expose this in a way that lets users get all the indexes for a particular field name?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I'm happy to add this, though it feels like it's adding further to the scope of the PR. I'm happy to implement any API suggestions that you may have; what would be your preferred way?


public IReadOnlyDictionary<string, string> Metadata { get; }

Expand All @@ -43,17 +44,11 @@ public partial class Schema : IRecordType
public Schema(
IEnumerable<Field> fields,
IEnumerable<KeyValuePair<string, string>> metadata)
: this(
fields?.ToList() ?? throw new ArgumentNullException(nameof(fields)),
metadata?.ToDictionary(kv => kv.Key, kv => kv.Value),
false)
{
if (fields is null)
{
throw new ArgumentNullException(nameof(fields));
}

_fieldsList = fields.ToList();
FieldsLookup = _fieldsList.ToLookup(f => f.Name);
_fieldsDictionary = FieldsLookup.ToDictionary(g => g.Key, g => g.First());

Metadata = metadata?.ToDictionary(kv => kv.Key, kv => kv.Value);
}

internal Schema(List<Field> fieldsList, IReadOnlyDictionary<string, string> metadata, bool copyCollections)
Expand All @@ -66,6 +61,10 @@ internal Schema(List<Field> fieldsList, IReadOnlyDictionary<string, string> meta
_fieldsDictionary = FieldsLookup.ToDictionary(g => g.Key, g => g.First());

Metadata = metadata;

_fieldsIndexLookup = _fieldsList
.Select((x, idx) => (Name: x.Name, Index: idx))
.ToLookup(x => x.Name, x => x.Index, StringComparer.CurrentCulture);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think for new code, we should use Ordinal. I don't think CurrentCulture really makes sense here.

Copy link
Author

@vthemelis vthemelis Nov 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I change to Ordinal then this would be a breaking change as this is used in GetFieldIndex(string). Are you okay with changing the behaviour?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course, I can then only use the lookup if the user has provided Ordinal as the comparator but this would defeat the purpose of this PR which is to make lookup of Columns in RecordBatch O(1).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or I could change this in a separate PR that implements your suggestions here #44650 (comment) ?

}

public Field GetFieldByIndex(int i) => _fieldsList[i];
Expand All @@ -80,15 +79,18 @@ public int GetFieldIndex(string name, StringComparer comparer)

public int GetFieldIndex(string name, IEqualityComparer<string> comparer = default)
{
comparer ??= StringComparer.CurrentCulture;
if (comparer == null || comparer.Equals(StringComparer.CurrentCulture))
{
return _fieldsIndexLookup[name].First();
}

for (int i = 0; i < _fieldsList.Count; i++)
{
if (comparer.Equals(_fieldsList[i].Name, name))
return i;
}

return -1;
throw new InvalidOperationException();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a breaking change. Is there a specific reason for it?

Copy link
Author

@vthemelis vthemelis Nov 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually fixing a regression that was added in #44576

The function would previously throw InvalidOperationException due to .First(...). The description of the PR is erroneous in asserting that -1 is returned in the case of no match as the exception would first be thrown by the argument of the .IndexOf(...) function.

I noticed this because I wrote my unit tests before that PR was merged. Are you happy with keeping the return -1 regression?

}

public Schema RemoveField(int fieldIndex)
Expand Down
68 changes: 68 additions & 0 deletions csharp/test/Apache.Arrow.Tests/SchemaTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// 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;
using Apache.Arrow.Types;
using System;
using System.Collections.Generic;
using Xunit;

namespace Apache.Arrow.Tests;

public class SchemaTests
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a test that verifies we can use a different comparer to the default, eg. getting a field named "f0" using "F0" and StringComparer.OrdinalIgnoreCase?

{
[Fact]
public void ThrowsWhenFieldsAreNull()
{
Assert.Throws<ArgumentNullException>(() => new Schema(null, null));
}

[Theory]
[MemberData(nameof(StringComparers))]
public void CanRetrieveFieldIndexByName(StringComparer comparer)
{
var field0 = new Field("f0", Int32Type.Default, true);
var field1 = new Field("f1", Int64Type.Default, true);
var schema = new Schema([field0, field1], null);

Assert.Equal(0, schema.GetFieldIndex("f0", comparer));
Assert.Equal(1, schema.GetFieldIndex("f1", comparer));
Assert.Throws<InvalidOperationException>(() => schema.GetFieldIndex("nonexistent", comparer));
}

[Theory]
[MemberData(nameof(StringComparers))]
public void CanRetrieveFieldIndexByNonUniqueName(StringComparer comparer)
{
var field0 = new Field("f0", Int32Type.Default, true);
var field1 = new Field("f1", Int64Type.Default, true);

// Repeat fields in the list
var schema = new Schema([field0, field1, field0, field1], null);

Assert.Equal(0, schema.GetFieldIndex("f0", comparer));
Assert.Equal(1, schema.GetFieldIndex("f1", comparer));
Assert.Throws<InvalidOperationException>(() => schema.GetFieldIndex("nonexistent", comparer));
}

public static IEnumerable<object[]> StringComparers() =>
new List<object[]>
{
new object[] {null},
new object[] {StringComparer.Ordinal},
new object[] {StringComparer.OrdinalIgnoreCase},
new object[] {StringComparer.CurrentCulture},
};
}
Loading