Skip to content

Commit 9e7b79b

Browse files
authored
GH-33970: [C#] Make schema field names case sensitive (#33978)
Remove the use of a comparer that ignores casing when building the fields dictionary for a schema. This allows for fields with names that differ only in casing. Currently, fields with names that only differ in casing will be seen as having the same name and will cause an Exception to be thrown due to being seen as having duplicate keys. * Closes: #33970 Authored-by: Danyaal Khan <[email protected]> Signed-off-by: Eric Erhardt <[email protected]>
1 parent f6bb03e commit 9e7b79b

2 files changed

Lines changed: 36 additions & 6 deletions

File tree

‎csharp/src/Apache.Arrow/Schema.cs‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,7 @@ public Schema(
4747

4848
_fields = fields.ToList();
4949

50-
_fieldsDictionary = fields.ToDictionary(
51-
field => field.Name, field => field,
52-
StringComparer.OrdinalIgnoreCase);
50+
_fieldsDictionary = fields.ToDictionary(field => field.Name, field => field);
5351

5452
Metadata = metadata?.ToDictionary(kv => kv.Key, kv => kv.Value);
5553
}
@@ -61,9 +59,7 @@ internal Schema(List<Field> fields, IReadOnlyDictionary<string, string> metadata
6159

6260
_fields = fields;
6361

64-
_fieldsDictionary = fields.ToDictionary(
65-
field => field.Name, field => field,
66-
StringComparer.OrdinalIgnoreCase);
62+
_fieldsDictionary = fields.ToDictionary(field => field.Name, field => field);
6763

6864
Metadata = metadata;
6965
}

‎csharp/test/Apache.Arrow.Tests/SchemaBuilderTests.cs‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,17 @@ public void FieldNameIsRequired()
5959
});
6060
}
6161

62+
[Fact]
63+
public void FieldNamesAreCaseSensitive()
64+
{
65+
var schema = new Schema.Builder()
66+
.Field(f => f.Name("f0").DataType(Int32Type.Default))
67+
.Field(f => f.Name("F0").DataType(Int8Type.Default))
68+
.Build();
69+
70+
Assert.Equal(2, schema.Fields.Count);
71+
}
72+
6273
[Fact]
6374
public void GetFieldIndex()
6475
{
@@ -69,6 +80,16 @@ public void GetFieldIndex()
6980
Assert.True(schema.GetFieldIndex("f0") == 0 && schema.GetFieldIndex("f1") == 1);
7081
}
7182

83+
[Fact]
84+
public void GetFieldIndexIsCaseSensitive()
85+
{
86+
var schema = new Schema.Builder()
87+
.Field(f => f.Name("f0").DataType(Int32Type.Default))
88+
.Field(f => f.Name("F0").DataType(Int8Type.Default))
89+
.Build();
90+
Assert.True(schema.GetFieldIndex("f0") == 0 && schema.GetFieldIndex("F0") == 1);
91+
}
92+
7293

7394
[Fact]
7495
public void GetFieldByName()
@@ -83,6 +104,19 @@ public void GetFieldByName()
83104
Assert.True(schema.GetFieldByName("f0") == f0 && schema.GetFieldByName("f1") == f1);
84105
}
85106

107+
[Fact]
108+
public void GetFieldByNameIsCaseSensitive()
109+
{
110+
var f0 = new Field.Builder().Name("f0").DataType(Int32Type.Default).Build();
111+
var f0Uppercase = new Field.Builder().Name("F0").DataType(Int8Type.Default).Build();
112+
113+
var schema = new Schema.Builder()
114+
.Field(f0)
115+
.Field(f0Uppercase)
116+
.Build();
117+
Assert.True(schema.GetFieldByName("f0") == f0 && schema.GetFieldByName("F0") == f0Uppercase);
118+
}
119+
86120
[Fact]
87121
public void MetadataConstruction()
88122
{

0 commit comments

Comments
 (0)