forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArrowReaderVerifier.cs
135 lines (118 loc) · 6.44 KB
/
ArrowReaderVerifier.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// 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.Ipc;
using System;
using System.Linq;
using System.Threading.Tasks;
using Xunit;
namespace Apache.Arrow.Tests
{
public static class ArrowReaderVerifier
{
public static void VerifyReader(ArrowStreamReader reader, RecordBatch originalBatch)
{
RecordBatch readBatch = reader.ReadNextRecordBatch();
CompareBatches(originalBatch, readBatch);
// There should only be one batch - calling ReadNextRecordBatch again should return null.
Assert.Null(reader.ReadNextRecordBatch());
Assert.Null(reader.ReadNextRecordBatch());
}
public static async Task VerifyReaderAsync(ArrowStreamReader reader, RecordBatch originalBatch)
{
RecordBatch readBatch = await reader.ReadNextRecordBatchAsync();
CompareBatches(originalBatch, readBatch);
// There should only be one batch - calling ReadNextRecordBatchAsync again should return null.
Assert.Null(await reader.ReadNextRecordBatchAsync());
Assert.Null(await reader.ReadNextRecordBatchAsync());
}
public static void CompareBatches(RecordBatch expectedBatch, RecordBatch actualBatch)
{
Assert.True(SchemaComparer.Equals(expectedBatch.Schema, actualBatch.Schema));
Assert.Equal(expectedBatch.Length, actualBatch.Length);
Assert.Equal(expectedBatch.ColumnCount, actualBatch.ColumnCount);
for (int i = 0; i < expectedBatch.ColumnCount; i++)
{
IArrowArray expectedArray = expectedBatch.Arrays.ElementAt(i);
IArrowArray actualArray = actualBatch.Arrays.ElementAt(i);
actualArray.Accept(new ArrayComparer(expectedArray));
}
}
private class ArrayComparer :
IArrowArrayVisitor<Int8Array>,
IArrowArrayVisitor<Int16Array>,
IArrowArrayVisitor<Int32Array>,
IArrowArrayVisitor<Int64Array>,
IArrowArrayVisitor<UInt8Array>,
IArrowArrayVisitor<UInt16Array>,
IArrowArrayVisitor<UInt32Array>,
IArrowArrayVisitor<UInt64Array>,
IArrowArrayVisitor<FloatArray>,
IArrowArrayVisitor<DoubleArray>,
IArrowArrayVisitor<BooleanArray>,
IArrowArrayVisitor<TimestampArray>,
IArrowArrayVisitor<Date32Array>,
IArrowArrayVisitor<Date64Array>,
IArrowArrayVisitor<ListArray>,
IArrowArrayVisitor<StringArray>,
IArrowArrayVisitor<BinaryArray>
{
private readonly IArrowArray _expectedArray;
public ArrayComparer(IArrowArray expectedArray)
{
_expectedArray = expectedArray;
}
public void Visit(Int8Array array) => CompareArrays(array);
public void Visit(Int16Array array) => CompareArrays(array);
public void Visit(Int32Array array) => CompareArrays(array);
public void Visit(Int64Array array) => CompareArrays(array);
public void Visit(UInt8Array array) => CompareArrays(array);
public void Visit(UInt16Array array) => CompareArrays(array);
public void Visit(UInt32Array array) => CompareArrays(array);
public void Visit(UInt64Array array) => CompareArrays(array);
public void Visit(FloatArray array) => CompareArrays(array);
public void Visit(DoubleArray array) => CompareArrays(array);
public void Visit(BooleanArray array) => CompareArrays(array);
public void Visit(TimestampArray array) => CompareArrays(array);
public void Visit(Date32Array array) => CompareArrays(array);
public void Visit(Date64Array array) => CompareArrays(array);
public void Visit(ListArray array) => throw new NotImplementedException();
public void Visit(StringArray array) => throw new NotImplementedException();
public void Visit(BinaryArray array) => throw new NotImplementedException();
public void Visit(IArrowArray array) => throw new NotImplementedException();
private void CompareArrays<T>(PrimitiveArray<T> actualArray)
where T : struct, IEquatable<T>
{
Assert.IsAssignableFrom<PrimitiveArray<T>>(_expectedArray);
PrimitiveArray<T> expectedArray = (PrimitiveArray<T>)_expectedArray;
Assert.Equal(expectedArray.Length, actualArray.Length);
Assert.Equal(expectedArray.NullCount, actualArray.NullCount);
Assert.Equal(expectedArray.Offset, actualArray.Offset);
Assert.True(expectedArray.NullBitmapBuffer.Span.SequenceEqual(actualArray.NullBitmapBuffer.Span));
Assert.True(expectedArray.Values.Slice(0, expectedArray.Length).SequenceEqual(actualArray.Values.Slice(0, actualArray.Length)));
}
private void CompareArrays(BooleanArray actualArray)
{
Assert.IsAssignableFrom<BooleanArray>(_expectedArray);
BooleanArray expectedArray = (BooleanArray)_expectedArray;
Assert.Equal(expectedArray.Length, actualArray.Length);
Assert.Equal(expectedArray.NullCount, actualArray.NullCount);
Assert.Equal(expectedArray.Offset, actualArray.Offset);
Assert.True(expectedArray.NullBitmapBuffer.Span.SequenceEqual(actualArray.NullBitmapBuffer.Span));
int booleanByteCount = BitUtility.ByteCount(expectedArray.Length);
Assert.True(expectedArray.Values.Slice(0, booleanByteCount).SequenceEqual(actualArray.Values.Slice(0, booleanByteCount)));
}
}
}
}