-
Notifications
You must be signed in to change notification settings - Fork 4k
GH-44501: [C#] Use an index lookup for O(1) field index access
#44633
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
|
||
| public IReadOnlyDictionary<string, string> Metadata { get; } | ||
|
|
||
|
|
@@ -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) | ||
|
|
@@ -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); | ||
|
||
| } | ||
|
|
||
| public Field GetFieldByIndex(int i) => _fieldsList[i]; | ||
|
|
@@ -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(); | ||
|
||
| } | ||
|
|
||
| public Schema RemoveField(int fieldIndex) | ||
|
|
||
| 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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| { | ||
| [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}, | ||
| }; | ||
| } | ||
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?