Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ARROW-15071: [C#] Fixed a bug in Column.cs ValidateArrayDataTypes method
Fixed a bug in Column.cs ValidateArrayDataTypes method: From: if (Data.Array(i).Data.DataType != Field.DataType) To: if (Data.Array(i).Data.DataType.TypeId != Field.DataType.TypeId) Added unit test in TestTableBasics and others. Closes #11931 from zixi-bwang/CSharpUnitTesting Lead-authored-by: Zixi <zixi.bwang@gmail.com> Co-authored-by: Zixi <89567557+zixi-bwang@users.noreply.github.com> Signed-off-by: Eric Erhardt <eric.erhardt@microsoft.com>
- Loading branch information
Showing
6 changed files
with
279 additions
and
1 deletion.
There are no files selected for viewing
147 changes: 147 additions & 0 deletions
147
csharp/src/Apache.Arrow/Arrays/ArrayDataTypeComparer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
// 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; | ||
using Apache.Arrow.Types; | ||
|
||
namespace Apache.Arrow | ||
{ | ||
internal sealed class ArrayDataTypeComparer : | ||
IArrowTypeVisitor<TimestampType>, | ||
IArrowTypeVisitor<Date32Type>, | ||
IArrowTypeVisitor<Date64Type>, | ||
IArrowTypeVisitor<Time32Type>, | ||
IArrowTypeVisitor<Time64Type>, | ||
IArrowTypeVisitor<FixedSizeBinaryType>, | ||
IArrowTypeVisitor<ListType>, | ||
IArrowTypeVisitor<StructType> | ||
{ | ||
private readonly IArrowType _expectedType; | ||
private bool _dataTypeMatch; | ||
|
||
public ArrayDataTypeComparer(IArrowType expectedType) | ||
{ | ||
_expectedType = expectedType; | ||
} | ||
|
||
public bool DataTypeMatch => _dataTypeMatch; | ||
|
||
public void Visit(TimestampType actualType) | ||
{ | ||
if (_expectedType is TimestampType expectedType | ||
&& expectedType.Timezone == actualType.Timezone | ||
&& expectedType.Unit == actualType.Unit) | ||
{ | ||
_dataTypeMatch = true; | ||
} | ||
} | ||
|
||
public void Visit(Date32Type actualType) | ||
{ | ||
if (_expectedType is Date32Type expectedType | ||
&& expectedType.Unit == actualType.Unit) | ||
{ | ||
_dataTypeMatch = true; | ||
} | ||
} | ||
|
||
public void Visit(Date64Type actualType) | ||
{ | ||
if (_expectedType is Date64Type expectedType | ||
&& expectedType.Unit == actualType.Unit) | ||
{ | ||
_dataTypeMatch = true; | ||
} | ||
} | ||
|
||
public void Visit(Time32Type actualType) | ||
{ | ||
if (_expectedType is Time32Type expectedType | ||
&& expectedType.Unit == actualType.Unit) | ||
{ | ||
_dataTypeMatch = true; | ||
} | ||
} | ||
|
||
public void Visit(Time64Type actualType) | ||
{ | ||
if (_expectedType is Time64Type expectedType | ||
&& expectedType.Unit == actualType.Unit) | ||
{ | ||
_dataTypeMatch = true; | ||
} | ||
} | ||
|
||
public void Visit(FixedSizeBinaryType actualType) | ||
{ | ||
if (_expectedType is FixedSizeBinaryType expectedType | ||
&& expectedType.ByteWidth == actualType.ByteWidth) | ||
{ | ||
_dataTypeMatch = true; | ||
} | ||
} | ||
|
||
public void Visit(ListType actualType) | ||
{ | ||
if (_expectedType is ListType expectedType | ||
&& CompareNested(expectedType, actualType)) | ||
{ | ||
_dataTypeMatch = true; | ||
} | ||
} | ||
|
||
public void Visit(StructType actualType) | ||
{ | ||
if (_expectedType is StructType expectedType | ||
&& CompareNested(expectedType, actualType)) | ||
{ | ||
_dataTypeMatch = true; | ||
} | ||
} | ||
|
||
private static bool CompareNested(NestedType expectedType, NestedType actualType) | ||
{ | ||
if (expectedType.Fields.Count != actualType.Fields.Count) | ||
{ | ||
return false; | ||
} | ||
|
||
for (int i = 0; i < expectedType.Fields.Count; i++) | ||
{ | ||
if (expectedType.Fields[i].DataType.TypeId != actualType.Fields[i].DataType.TypeId) | ||
{ | ||
return false; | ||
} | ||
|
||
var dataTypeMatch = FieldComparer.Compare(expectedType.Fields[i], actualType.Fields[i]); | ||
|
||
if (!dataTypeMatch) | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public void Visit(IArrowType actualType) | ||
{ | ||
if (_expectedType.TypeId == actualType.TypeId) | ||
{ | ||
_dataTypeMatch = true; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// 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.Linq; | ||
|
||
namespace Apache.Arrow | ||
{ | ||
internal static class FieldComparer | ||
{ | ||
public static bool Compare(Field expected, Field actual) | ||
{ | ||
if (ReferenceEquals(expected, actual)) | ||
{ | ||
return true; | ||
} | ||
|
||
if (expected.Name != actual.Name || expected.IsNullable != actual.IsNullable || | ||
expected.HasMetadata != actual.HasMetadata) | ||
{ | ||
return false; | ||
} | ||
|
||
if (expected.HasMetadata) | ||
{ | ||
if (expected.Metadata.Count != actual.Metadata.Count) | ||
{ | ||
return false; | ||
} | ||
|
||
if (!expected.Metadata.Keys.All(k => actual.Metadata.ContainsKey(k) && expected.Metadata[k] == actual.Metadata[k])) | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
var dataTypeComparer = new ArrayDataTypeComparer(expected.DataType); | ||
|
||
actual.DataType.Accept(dataTypeComparer); | ||
|
||
if (!dataTypeComparer.DataTypeMatch) | ||
{ | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters