-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
ArrowArrayConcatenatorTests.cs
579 lines (505 loc) · 26 KB
/
ArrowArrayConcatenatorTests.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
// 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 System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Apache.Arrow.Memory;
using Apache.Arrow.Types;
using Xunit;
namespace Apache.Arrow.Tests
{
public class ArrowArrayConcatenatorTests
{
[Fact]
public void TestStandardCases()
{
foreach ((List<IArrowArray> testTargetArrayList, IArrowArray expectedArray) in GenerateTestData())
{
IArrowArray actualArray = ArrowArrayConcatenatorReflector.InvokeConcatenate(testTargetArrayList);
ArrowReaderVerifier.CompareArrays(expectedArray, actualArray);
}
}
[Fact]
public void TestNullOrEmpty()
{
Assert.Null(ArrowArrayConcatenatorReflector.InvokeConcatenate(null));
Assert.Null(ArrowArrayConcatenatorReflector.InvokeConcatenate(new List<IArrowArray>()));
}
[Fact]
public void TestSingleElement()
{
Int32Array array = new Int32Array.Builder().Append(1).Append(2).Build();
IArrowArray actualArray = ArrowArrayConcatenatorReflector.InvokeConcatenate(new[] { array });
ArrowReaderVerifier.CompareArrays(array, actualArray);
}
private static IEnumerable<Tuple<List<IArrowArray>, IArrowArray>> GenerateTestData()
{
var targetTypes = new List<IArrowType>() {
BooleanType.Default,
Int8Type.Default,
Int16Type.Default,
Int32Type.Default,
Int64Type.Default,
UInt8Type.Default,
UInt16Type.Default,
UInt32Type.Default,
UInt64Type.Default,
FloatType.Default,
DoubleType.Default,
BinaryType.Default,
StringType.Default,
Date32Type.Default,
Date64Type.Default,
TimestampType.Default,
new Decimal128Type(14, 10),
new Decimal256Type(14,10),
new ListType(Int64Type.Default),
new StructType(new List<Field>{
new Field.Builder().Name("Strings").DataType(StringType.Default).Nullable(true).Build(),
new Field.Builder().Name("Ints").DataType(Int32Type.Default).Nullable(true).Build()
}),
new FixedSizeListType(Int32Type.Default, 1),
new UnionType(
new List<Field>{
new Field.Builder().Name("Strings").DataType(StringType.Default).Nullable(true).Build(),
new Field.Builder().Name("Ints").DataType(Int32Type.Default).Nullable(true).Build()
},
new[] { 0, 1 },
UnionMode.Sparse
),
new UnionType(
new List<Field>{
new Field.Builder().Name("Strings").DataType(StringType.Default).Nullable(true).Build(),
new Field.Builder().Name("Ints").DataType(Int32Type.Default).Nullable(true).Build()
},
new[] { 0, 1 },
UnionMode.Dense
),
new MapType(
new Field.Builder().Name("key").DataType(StringType.Default).Nullable(false).Build(),
new Field.Builder().Name("value").DataType(Int32Type.Default).Nullable(true).Build(),
keySorted: false),
};
foreach (IArrowType type in targetTypes)
{
var creator = new TestDataGenerator();
type.Accept(creator);
yield return Tuple.Create(creator.TestTargetArrayList, creator.ExpectedArray);
}
}
private static class ArrowArrayConcatenatorReflector
{
private static readonly MethodInfo s_concatenateInfo = typeof(ArrayData).Assembly.GetType("Apache.Arrow.ArrowArrayConcatenator")
.GetMethod("Concatenate", BindingFlags.Static | BindingFlags.NonPublic);
internal static IArrowArray InvokeConcatenate(IReadOnlyList<IArrowArray> arrowArrayList, MemoryAllocator allocator = default)
{
return s_concatenateInfo.Invoke(null, new object[] { arrowArrayList, allocator }) as IArrowArray;
}
}
private class TestDataGenerator :
IArrowTypeVisitor<BooleanType>,
IArrowTypeVisitor<Int8Type>,
IArrowTypeVisitor<Int16Type>,
IArrowTypeVisitor<Int32Type>,
IArrowTypeVisitor<Int64Type>,
IArrowTypeVisitor<UInt8Type>,
IArrowTypeVisitor<UInt16Type>,
IArrowTypeVisitor<UInt32Type>,
IArrowTypeVisitor<UInt64Type>,
IArrowTypeVisitor<FloatType>,
IArrowTypeVisitor<DoubleType>,
IArrowTypeVisitor<BinaryType>,
IArrowTypeVisitor<StringType>,
IArrowTypeVisitor<Decimal128Type>,
IArrowTypeVisitor<Decimal256Type>,
IArrowTypeVisitor<Date32Type>,
IArrowTypeVisitor<Date64Type>,
IArrowTypeVisitor<TimestampType>,
IArrowTypeVisitor<ListType>,
IArrowTypeVisitor<FixedSizeListType>,
IArrowTypeVisitor<StructType>,
IArrowTypeVisitor<UnionType>,
IArrowTypeVisitor<MapType>
{
private List<List<int?>> _baseData;
private int _baseDataListCount;
private int _baseDataTotalElementCount;
public List<IArrowArray> TestTargetArrayList { get; }
public IArrowArray ExpectedArray { get; private set; }
public TestDataGenerator()
{
_baseData = new List<List<int?>> {
new List<int?> { 1, 2, 3 },
new List<int?> { 100, 101, null },
new List<int?> { 11, null, 12 },
};
_baseDataListCount = _baseData.Count;
_baseDataTotalElementCount = _baseData.Sum(_ => _.Count);
TestTargetArrayList = new List<IArrowArray>(_baseDataListCount);
}
public void Visit(BooleanType type) => GenerateTestData<bool, BooleanArray, BooleanArray.Builder>(type, x => x % 2 == 0);
public void Visit(Int8Type type) => GenerateTestData<sbyte, Int8Array, Int8Array.Builder>(type, x => (sbyte)x);
public void Visit(Int16Type type) => GenerateTestData<short, Int16Array, Int16Array.Builder>(type, x => (short)x);
public void Visit(Int32Type type) => GenerateTestData<int, Int32Array, Int32Array.Builder>(type, x => x);
public void Visit(Int64Type type) => GenerateTestData<long, Int64Array, Int64Array.Builder>(type, x => x);
public void Visit(UInt8Type type) => GenerateTestData<byte, UInt8Array, UInt8Array.Builder>(type, x => (byte)x);
public void Visit(UInt16Type type) => GenerateTestData<ushort, UInt16Array, UInt16Array.Builder>(type, x => (ushort)x);
public void Visit(UInt32Type type) => GenerateTestData<uint, UInt32Array, UInt32Array.Builder>(type, x => (uint)x);
public void Visit(UInt64Type type) => GenerateTestData<ulong, UInt64Array, UInt64Array.Builder>(type, x => (ulong)x);
public void Visit(FloatType type) => GenerateTestData<float, FloatArray, FloatArray.Builder>(type, x => x);
public void Visit(DoubleType type) => GenerateTestData<double, DoubleArray, DoubleArray.Builder>(type, x => x);
public void Visit(Date32Type type) => GenerateTestData<DateTime, Date32Array, Date32Array.Builder>(type, x => DateTime.MinValue.AddDays(x));
public void Visit(Date64Type type) => GenerateTestData<DateTime, Date64Array, Date64Array.Builder>(type, x => DateTime.MinValue.AddDays(x));
public void Visit(Decimal128Type type)
{
Decimal128Array.Builder resultBuilder = new Decimal128Array.Builder(type).Reserve(_baseDataTotalElementCount);
for (int i = 0; i < _baseDataListCount; i++)
{
List<int?> dataList = _baseData[i];
Decimal128Array.Builder builder = new Decimal128Array.Builder(type).Reserve(dataList.Count);
foreach (decimal? value in dataList)
{
if (value.HasValue)
{
builder.Append(value.Value);
resultBuilder.Append(value.Value);
}
else
{
builder.AppendNull();
resultBuilder.AppendNull();
}
}
TestTargetArrayList.Add(builder.Build());
}
ExpectedArray = resultBuilder.Build();
}
public void Visit(Decimal256Type type)
{
Decimal256Array.Builder resultBuilder = new Decimal256Array.Builder(type).Reserve(_baseDataTotalElementCount);
for (int i = 0; i < _baseDataListCount; i++)
{
List<int?> dataList = _baseData[i];
Decimal256Array.Builder builder = new Decimal256Array.Builder(type).Reserve(dataList.Count);
foreach (decimal? value in dataList)
{
if (value.HasValue)
{
builder.Append(value.Value);
resultBuilder.Append(value.Value);
}
else
{
builder.AppendNull();
resultBuilder.AppendNull();
}
}
TestTargetArrayList.Add(builder.Build());
}
ExpectedArray = resultBuilder.Build();
}
public void Visit(TimestampType type)
{
TimestampArray.Builder resultBuilder = new TimestampArray.Builder().Reserve(_baseDataTotalElementCount);
DateTimeOffset basis = DateTimeOffset.UtcNow;
for (int i = 0; i < _baseDataListCount; i++)
{
List<int?> dataList = _baseData[i];
TimestampArray.Builder builder = new TimestampArray.Builder().Reserve(dataList.Count);
foreach (int? value in dataList)
{
if (value.HasValue)
{
DateTimeOffset dateValue = basis.AddMilliseconds(value.Value);
builder.Append(dateValue);
resultBuilder.Append(dateValue);
}
else
{
builder.AppendNull();
resultBuilder.AppendNull();
}
}
TestTargetArrayList.Add(builder.Build());
}
ExpectedArray = resultBuilder.Build();
}
public void Visit(BinaryType type)
{
BinaryArray.Builder resultBuilder = new BinaryArray.Builder().Reserve(_baseDataTotalElementCount);
for (int i = 0; i < _baseDataListCount; i++)
{
List<int?> dataList = _baseData[i];
BinaryArray.Builder builder = new BinaryArray.Builder().Reserve(dataList.Count);
foreach (byte? value in dataList)
{
if (value.HasValue)
{
builder.Append(value.Value);
resultBuilder.Append(value.Value);
}
else
{
builder.AppendNull();
resultBuilder.AppendNull();
}
}
TestTargetArrayList.Add(builder.Build());
}
ExpectedArray = resultBuilder.Build();
}
public void Visit(StringType type)
{
StringArray.Builder resultBuilder = new StringArray.Builder().Reserve(_baseDataTotalElementCount);
for (int i = 0; i < _baseDataListCount; i++)
{
List<int?> dataList = _baseData[i];
StringArray.Builder builder = new StringArray.Builder().Reserve(dataList.Count);
foreach (string value in dataList.Select(_ => _.ToString() ?? null))
{
builder.Append(value);
resultBuilder.Append(value);
}
TestTargetArrayList.Add(builder.Build());
}
ExpectedArray = resultBuilder.Build();
}
public void Visit(ListType type)
{
ListArray.Builder resultBuilder = new ListArray.Builder(type.ValueDataType).Reserve(_baseDataTotalElementCount);
Int64Array.Builder resultValueBuilder = (Int64Array.Builder)resultBuilder.ValueBuilder.Reserve(_baseDataTotalElementCount);
for (int i = 0; i < _baseDataListCount; i++)
{
List<int?> dataList = _baseData[i];
ListArray.Builder builder = new ListArray.Builder(type.ValueField).Reserve(dataList.Count);
Int64Array.Builder valueBuilder = (Int64Array.Builder)builder.ValueBuilder.Reserve(dataList.Count);
foreach (long? value in dataList)
{
if (value.HasValue)
{
builder.Append();
resultBuilder.Append();
valueBuilder.Append(value.Value);
resultValueBuilder.Append(value.Value);
}
else
{
builder.AppendNull();
resultBuilder.AppendNull();
}
}
TestTargetArrayList.Add(builder.Build());
}
ExpectedArray = resultBuilder.Build();
}
public void Visit(FixedSizeListType type)
{
FixedSizeListArray.Builder resultBuilder = new FixedSizeListArray.Builder(type.ValueDataType, type.ListSize).Reserve(_baseDataTotalElementCount);
Int32Array.Builder resultValueBuilder = (Int32Array.Builder)resultBuilder.ValueBuilder.Reserve(_baseDataTotalElementCount);
for (int i = 0; i < _baseDataListCount; i++)
{
List<int?> dataList = _baseData[i];
FixedSizeListArray.Builder builder = new FixedSizeListArray.Builder(type.ValueField, type.ListSize).Reserve(dataList.Count);
Int32Array.Builder valueBuilder = (Int32Array.Builder)builder.ValueBuilder.Reserve(dataList.Count);
foreach (int? value in dataList)
{
if (value.HasValue)
{
builder.Append();
resultBuilder.Append();
valueBuilder.Append(value.Value);
resultValueBuilder.Append(value.Value);
}
else
{
builder.AppendNull();
resultBuilder.AppendNull();
}
}
TestTargetArrayList.Add(builder.Build());
}
ExpectedArray = resultBuilder.Build();
}
public void Visit(StructType type)
{
// TODO: Make data from type fields.
// The following can be improved with a Builder class for StructArray.
StringArray.Builder resultStringBuilder = new StringArray.Builder();
Int32Array.Builder resultInt32Builder = new Int32Array.Builder();
ArrowBuffer nullBitmapBuffer = new ArrowBuffer.BitmapBuilder().Append(true).Append(true).Append(false).Build();
for (int i = 0; i < 3; i++)
{
resultStringBuilder.Append("joe").AppendNull().AppendNull().Append("mark");
resultInt32Builder.Append(1).Append(2).AppendNull().Append(4);
StringArray stringArray = new StringArray.Builder().Append("joe").AppendNull().AppendNull().Append("mark").Build();
Int32Array intArray = new Int32Array.Builder().Append(1).Append(2).AppendNull().Append(4).Build();
List<Array> arrays = new List<Array>
{
stringArray,
intArray
};
TestTargetArrayList.Add(new StructArray(type, 3, arrays, nullBitmapBuffer, 1));
}
StringArray resultStringArray = resultStringBuilder.Build();
Int32Array resultInt32Array = resultInt32Builder.Build();
ExpectedArray = new StructArray(type, 9, new List<Array> { resultStringArray, resultInt32Array }, nullBitmapBuffer, 3);
}
public void Visit(UnionType type)
{
bool isDense = type.Mode == UnionMode.Dense;
StringArray.Builder stringResultBuilder = new StringArray.Builder().Reserve(_baseDataTotalElementCount);
Int32Array.Builder intResultBuilder = new Int32Array.Builder().Reserve(_baseDataTotalElementCount);
ArrowBuffer.Builder<byte> typeResultBuilder = new ArrowBuffer.Builder<byte>().Reserve(_baseDataTotalElementCount);
ArrowBuffer.Builder<int> offsetResultBuilder = new ArrowBuffer.Builder<int>().Reserve(_baseDataTotalElementCount);
int resultNullCount = 0;
for (int i = 0; i < _baseDataListCount; i++)
{
List<int?> dataList = _baseData[i];
StringArray.Builder stringBuilder = new StringArray.Builder().Reserve(dataList.Count);
Int32Array.Builder intBuilder = new Int32Array.Builder().Reserve(dataList.Count);
ArrowBuffer.Builder<byte> typeBuilder = new ArrowBuffer.Builder<byte>().Reserve(dataList.Count);
ArrowBuffer.Builder<int> offsetBuilder = new ArrowBuffer.Builder<int>().Reserve(dataList.Count);
int nullCount = 0;
for (int j = 0; j < dataList.Count; j++)
{
byte index = (byte)Math.Max(j % 3, 1);
int? intValue = (index == 1) ? dataList[j] : null;
string stringValue = (index == 1) ? null : dataList[j]?.ToString();
typeBuilder.Append(index);
if (isDense)
{
if (index == 0)
{
offsetBuilder.Append(stringBuilder.Length);
offsetResultBuilder.Append(stringResultBuilder.Length);
stringBuilder.Append(stringValue);
stringResultBuilder.Append(stringValue);
}
else
{
offsetBuilder.Append(intBuilder.Length);
offsetResultBuilder.Append(intResultBuilder.Length);
intBuilder.Append(intValue);
intResultBuilder.Append(intValue);
}
}
else
{
stringBuilder.Append(stringValue);
stringResultBuilder.Append(stringValue);
intBuilder.Append(intValue);
intResultBuilder.Append(intValue);
}
if (dataList[j] == null)
{
nullCount++;
resultNullCount++;
}
}
ArrowBuffer[] buffers;
if (isDense)
{
buffers = new[] { typeBuilder.Build(), offsetBuilder.Build() };
}
else
{
buffers = new[] { typeBuilder.Build() };
}
TestTargetArrayList.Add(UnionArray.Create(new ArrayData(
type, dataList.Count, nullCount, 0, buffers,
new[] { stringBuilder.Build().Data, intBuilder.Build().Data })));
}
ArrowBuffer[] resultBuffers;
if (isDense)
{
resultBuffers = new[] { typeResultBuilder.Build(), offsetResultBuilder.Build() };
}
else
{
resultBuffers = new[] { typeResultBuilder.Build() };
}
ExpectedArray = UnionArray.Create(new ArrayData(
type, _baseDataTotalElementCount, resultNullCount, 0, resultBuffers,
new[] { stringResultBuilder.Build().Data, intResultBuilder.Build().Data }));
}
public void Visit(MapType type)
{
MapArray.Builder resultBuilder = new MapArray.Builder(type).Reserve(_baseDataTotalElementCount);
StringArray.Builder resultKeyBuilder = (StringArray.Builder)resultBuilder.KeyBuilder.Reserve(_baseDataTotalElementCount);
Int32Array.Builder resultValueBuilder = (Int32Array.Builder)resultBuilder.ValueBuilder.Reserve(_baseDataTotalElementCount);
ArrowBuffer nullBitmapBuilder = new ArrowBuffer.BitmapBuilder().Append(true).Append(true).Append(false).Build();
for (int i = 0; i < _baseData.Count; i++)
{
List<int?> dataList = _baseData[i];
MapArray.Builder builder = new MapArray.Builder(type).Reserve(dataList.Count);
StringArray.Builder keyBuilder = (StringArray.Builder)builder.KeyBuilder.Reserve(dataList.Count);
Int32Array.Builder valueBuilder = (Int32Array.Builder)builder.ValueBuilder.Reserve(dataList.Count);
foreach (int? value in dataList)
{
if (value.HasValue)
{
builder.Append();
resultBuilder.Append();
keyBuilder.Append(value.Value.ToString());
valueBuilder.Append(value.Value);
resultKeyBuilder.Append(value.Value.ToString());
resultValueBuilder.Append(value.Value);
}
else
{
builder.AppendNull();
resultBuilder.AppendNull();
}
}
TestTargetArrayList.Add(builder.Build());
}
ExpectedArray = resultBuilder.Build();
}
public void Visit(IArrowType type)
{
throw new NotImplementedException();
}
private void GenerateTestData<T, TArray, TArrayBuilder>(IArrowType type, Func<int, T> generator)
where TArrayBuilder : IArrowArrayBuilder<T, TArray, TArrayBuilder>
where TArray : IArrowArray
{
var resultBuilder = (IArrowArrayBuilder<T, TArray, TArrayBuilder>)ArrayArrayBuilderFactoryReflector.InvokeBuild(type);
resultBuilder.Reserve(_baseDataTotalElementCount);
for (int i = 0; i < _baseDataListCount; i++)
{
List<int?> dataList = _baseData[i];
var builder = (IArrowArrayBuilder<T, TArray, TArrayBuilder>)ArrayArrayBuilderFactoryReflector.InvokeBuild(type);
builder.Reserve(dataList.Count);
foreach (int? value in dataList)
{
if (value.HasValue)
{
builder.Append(generator(value.Value));
resultBuilder.Append(generator(value.Value));
}
else
{
builder.AppendNull();
resultBuilder.AppendNull();
}
}
TestTargetArrayList.Add(builder.Build(default));
}
ExpectedArray = resultBuilder.Build(default);
}
}
}
}