-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Expand file tree
/
Copy pathtest-reader.rb
More file actions
702 lines (648 loc) · 23.6 KB
/
test-reader.rb
File metadata and controls
702 lines (648 loc) · 23.6 KB
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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
# 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.
module ReaderTests
def roundtrip(data)
Dir.mktmpdir do |tmp_dir|
case data
when Arrow::Array
table = Arrow::Table.new(value: data)
when Arrow::RecordBatch
table = data.to_table
else
table = data
end
path = File.join(tmp_dir, "data.#{file_extension}")
table.save(path)
File.open(path, "rb") do |input|
reader = reader_class.new(input)
case data
when Arrow::Array
values = []
reader.each do |record_batch|
values.concat(record_batch.columns[0].to_a)
end
[reader.schema.fields[0].type, values]
else
record_batches = reader.collect do |record_batch|
record_batch.to_h.tap do |hash|
hash.each do |key, value|
hash[key] = value.to_a
end
end
end
[reader.schema, record_batches]
end
end
ensure
GC.start
end
end
def test_custom_metadata_field
field =
Arrow::Field.new("value", :boolean)
.with_metadata("key1" => "value1",
"key2" => "value2")
schema = Arrow::Schema.new([field])
values = [true, nil, false]
record_batch = Arrow::RecordBatch.new(schema, {"value" => values})
schema, record_batches = roundtrip(record_batch)
assert_equal([
{
"key1" => "value1",
"key2" => "value2",
},
[{"value" => values}],
],
[schema.fields[0].metadata, record_batches])
end
def test_custom_metadata_schema
field = Arrow::Field.new("value", :boolean)
schema =
Arrow::Schema.new([field])
.with_metadata("key1" => "value1",
"key2" => "value2")
values = [true, nil, false]
record_batch = Arrow::RecordBatch.new(schema, {"value" => values})
schema, record_batches = roundtrip(record_batch)
assert_equal([
{
"key1" => "value1",
"key2" => "value2",
},
[{"value" => values}],
],
[schema.metadata, record_batches])
end
def test_null
type, values = roundtrip(Arrow::NullArray.new(3))
assert_equal(["Null", [nil, nil, nil]],
[type.to_s, values])
end
def test_boolean
type, values = roundtrip(Arrow::BooleanArray.new([true, nil, false]))
assert_equal(["Boolean", [true, nil, false]],
[type.to_s, values])
end
def test_int8
type, values = roundtrip(Arrow::Int8Array.new([-128, nil, 127]))
assert_equal(["Int8", [-128, nil, 127]],
[type.to_s, values])
end
def test_uint8
type, values = roundtrip(Arrow::UInt8Array.new([0, nil, 255]))
assert_equal(["UInt8", [0, nil, 255]],
[type.to_s, values])
end
def test_int16
type, values = roundtrip(Arrow::Int16Array.new([-32768, nil, 32767]))
assert_equal(["Int16", [-32768, nil, 32767]],
[type.to_s, values])
end
def test_uint16
type, values = roundtrip(Arrow::UInt16Array.new([0, nil, 65535]))
assert_equal(["UInt16", [0, nil, 65535]],
[type.to_s, values])
end
def test_int32
array = Arrow::Int32Array.new([-2147483648, nil, 2147483647])
type, values = roundtrip(array)
assert_equal(["Int32", [-2147483648, nil, 2147483647]],
[type.to_s, values])
end
def test_uint32
array = Arrow::UInt32Array.new([0, nil, 4294967295])
type, values = roundtrip(array)
assert_equal(["UInt32", [0, nil, 4294967295]],
[type.to_s, values])
end
def test_int64
array = Arrow::Int64Array.new([
-9223372036854775808,
nil,
9223372036854775807
])
type, values = roundtrip(array)
assert_equal(["Int64", [-9223372036854775808, nil, 9223372036854775807]],
[type.to_s, values])
end
def test_uint64
array = Arrow::UInt64Array.new([0, nil, 18446744073709551615])
type, values = roundtrip(array)
assert_equal(["UInt64", [0, nil, 18446744073709551615]],
[type.to_s, values])
end
def test_float32
type, values = roundtrip(Arrow::FloatArray.new([-0.5, nil, 0.5]))
assert_equal(["Float32", [-0.5, nil, 0.5]],
[type.to_s, values])
end
def test_float64
type, values = roundtrip(Arrow::DoubleArray.new([-0.5, nil, 0.5]))
assert_equal(["Float64", [-0.5, nil, 0.5]],
[type.to_s, values])
end
def test_date32
date_2017_08_28 = 17406
date_2025_12_09 = 20431
array = Arrow::Date32Array.new([date_2017_08_28, nil, date_2025_12_09])
type, values = roundtrip(array)
assert_equal(["Date32", [date_2017_08_28, nil, date_2025_12_09]],
[type.to_s, values])
end
def test_date64
date_2017_08_28_00_00_00 = 1503878400000
date_2025_12_10_00_00_00 = 1765324800000
array = Arrow::Date64Array.new([
date_2017_08_28_00_00_00,
nil,
date_2025_12_10_00_00_00,
])
type, values = roundtrip(array)
assert_equal([
"Date64",
[date_2017_08_28_00_00_00, nil, date_2025_12_10_00_00_00],
],
[type.to_s, values])
end
def test_time32_second
time_00_00_10 = 10
time_00_01_10 = 60 + 10
array = Arrow::Time32Array.new(:second,
[time_00_00_10, nil, time_00_01_10])
type, values = roundtrip(array)
assert_equal(["Time32(second)", [time_00_00_10, nil, time_00_01_10]],
[type.to_s, values])
end
def test_time32_millisecond
time_00_00_10_000 = 10 * 1000
time_00_01_10_000 = (60 + 10) * 1000
array = Arrow::Time32Array.new(:milli,
[
time_00_00_10_000,
nil,
time_00_01_10_000,
])
type, values = roundtrip(array)
assert_equal([
"Time32(millisecond)",
[time_00_00_10_000, nil, time_00_01_10_000],
],
[type.to_s, values])
end
def test_time64_microsecond
time_00_00_10_000_000 = 10 * 1_000_000
time_00_01_10_000_000 = (60 + 10) * 1_000_000
array = Arrow::Time64Array.new(:micro,
[
time_00_00_10_000_000,
nil,
time_00_01_10_000_000,
])
type, values = roundtrip(array)
assert_equal([
"Time64(microsecond)",
[time_00_00_10_000_000, nil, time_00_01_10_000_000],
],
[type.to_s, values])
end
def test_time64_nanosecond
time_00_00_10_000_000_000 = 10 * 1_000_000_000
time_00_01_10_000_000_000 = (60 + 10) * 1_000_000_000
array = Arrow::Time64Array.new(:nano,
[
time_00_00_10_000_000_000,
nil,
time_00_01_10_000_000_000,
])
type, values = roundtrip(array)
assert_equal([
"Time64(nanosecond)",
[
time_00_00_10_000_000_000,
nil,
time_00_01_10_000_000_000,
],
],
[type.to_s, values])
end
def test_timestamp_second
timestamp_2019_11_17_15_09_11 = 1574003351
timestamp_2025_12_16_05_33_58 = 1765863238
array = Arrow::TimestampArray.new(:second,
[
timestamp_2019_11_17_15_09_11,
nil,
timestamp_2025_12_16_05_33_58,
])
type, values = roundtrip(array)
assert_equal([
"Timestamp(second)",
[
timestamp_2019_11_17_15_09_11,
nil,
timestamp_2025_12_16_05_33_58,
],
],
[type.to_s, values])
end
def test_timestamp_millisecond
timestamp_2019_11_17_15_09_11 = 1574003351 * 1_000
timestamp_2025_12_16_05_33_58 = 1765863238 * 1_000
array = Arrow::TimestampArray.new(:milli,
[
timestamp_2019_11_17_15_09_11,
nil,
timestamp_2025_12_16_05_33_58,
])
type, values = roundtrip(array)
assert_equal([
"Timestamp(millisecond)",
[
timestamp_2019_11_17_15_09_11,
nil,
timestamp_2025_12_16_05_33_58,
],
],
[type.to_s, values])
end
def test_timestamp_microsecond
timestamp_2019_11_17_15_09_11 = 1574003351 * 1_000_000
timestamp_2025_12_16_05_33_58 = 1765863238 * 1_000_000
array = Arrow::TimestampArray.new(:micro,
[
timestamp_2019_11_17_15_09_11,
nil,
timestamp_2025_12_16_05_33_58,
])
type, values = roundtrip(array)
assert_equal([
"Timestamp(microsecond)",
[
timestamp_2019_11_17_15_09_11,
nil,
timestamp_2025_12_16_05_33_58,
],
],
[type.to_s, values])
end
def test_timestamp_nanosecond
timestamp_2019_11_17_15_09_11 = 1574003351 * 1_000_000_000
timestamp_2025_12_16_05_33_58 = 1765863238 * 1_000_000_000
array = Arrow::TimestampArray.new(:nano,
[
timestamp_2019_11_17_15_09_11,
nil,
timestamp_2025_12_16_05_33_58,
])
type, values = roundtrip(array)
assert_equal([
"Timestamp(nanosecond)",
[
timestamp_2019_11_17_15_09_11,
nil,
timestamp_2025_12_16_05_33_58,
],
],
[type.to_s, values])
end
def test_timestamp_time_zone
time_zone = "UTC"
timestamp_2019_11_17_15_09_11 = 1574003351
timestamp_2025_12_16_05_33_58 = 1765863238
data_type = Arrow::TimestampDataType.new(:second, time_zone)
array = Arrow::TimestampArray.new(data_type,
[
timestamp_2019_11_17_15_09_11,
nil,
timestamp_2025_12_16_05_33_58,
])
type, values = roundtrip(array)
assert_equal([
"Timestamp(second, #{time_zone})",
[
timestamp_2019_11_17_15_09_11,
nil,
timestamp_2025_12_16_05_33_58,
],
],
[type.to_s, values])
end
def test_year_month_interval
type, values = roundtrip(Arrow::MonthIntervalArray.new([0, nil, 100]))
assert_equal(["YearMonthInterval", [0, nil, 100]],
[type.to_s, values])
end
def test_day_time_interval
array = Arrow::DayTimeIntervalArray.new([
{day: 1, millisecond: 100},
nil,
{day: 3, millisecond: 300},
])
type, values = roundtrip(array)
assert_equal([
"DayTimeInterval",
[
[1, 100],
nil,
[3, 300],
],
],
[type.to_s, values])
end
def test_month_day_nano_interval
array = Arrow::MonthDayNanoIntervalArray.new([
{
month: 1,
day: 1,
nanosecond: 100,
},
nil,
{
month: 3,
day: 3,
nanosecond: 300,
},
])
type, values = roundtrip(array)
assert_equal([
"MonthDayNanoInterval",
[
[1, 1, 100],
nil,
[3, 3, 300],
],
],
[type.to_s, values])
end
def test_duration_second
type, values = roundtrip(Arrow::DurationArray.new(:second, [0, nil, 100]))
assert_equal(["Duration(second)", [0, nil, 100]],
[type.to_s, values])
end
def test_duration_millisecond
array = Arrow::DurationArray.new(:milli, [0, nil, 100_000])
type, values = roundtrip(array)
assert_equal(["Duration(millisecond)", [0, nil, 100_000]],
[type.to_s, values])
end
def test_duration_microsecond
array = Arrow::DurationArray.new(:micro, [0, nil, 100_000_000])
type, values = roundtrip(array)
assert_equal(["Duration(microsecond)", [0, nil, 100_000_000]],
[type.to_s, values])
end
def test_duration_nanosecond
array = Arrow::DurationArray.new(:nano, [0, nil, 100_000_000_000])
type, values = roundtrip(array)
assert_equal(["Duration(nanosecond)", [0, nil, 100_000_000_000]],
[type.to_s, values])
end
def test_binary
array = Arrow::BinaryArray.new(["Hello".b, nil, "World".b])
type, values = roundtrip(array)
assert_equal(["Binary", ["Hello".b, nil, "World".b]],
[type.to_s, values])
end
def test_large_binary
array = Arrow::LargeBinaryArray.new(["Hello".b, nil, "World".b])
type, values = roundtrip(array)
assert_equal(["LargeBinary", ["Hello".b, nil, "World".b]],
[type.to_s, values])
end
def test_utf8
array = Arrow::StringArray.new(["Hello", nil, "World"])
type, values = roundtrip(array)
assert_equal(["UTF8", ["Hello", nil, "World"]],
[type.to_s, values])
end
def test_large_utf8
array = Arrow::LargeStringArray.new(["Hello", nil, "World"])
type, values = roundtrip(array)
assert_equal(["LargeUTF8", ["Hello", nil, "World"]],
[type.to_s, values])
end
def test_fixed_size_binary
data_type = Arrow::FixedSizeBinaryDataType.new(4)
array = Arrow::FixedSizeBinaryArray.new(data_type,
["0124".b, nil, "abcd".b])
type, values = roundtrip(array)
assert_equal(["FixedSizeBinary(4)", ["0124".b, nil, "abcd".b]],
[type.to_s, values])
end
def test_decimal128
positive_small = "1.200"
positive_large = ("1234567890" * 3) + "12345.678"
negative_small = "-1.200"
negative_large = "-" + ("1234567890" * 3) + "12345.678"
array = Arrow::Decimal128Array.new({precision: 38, scale: 3},
[
positive_large,
positive_small,
nil,
negative_small,
negative_large,
])
type, values = roundtrip(array)
assert_equal([
"Decimal128(38, 3)",
[
BigDecimal(positive_large),
BigDecimal(positive_small),
nil,
BigDecimal(negative_small),
BigDecimal(negative_large),
],
],
[type.to_s, values])
end
def test_decimal256
positive_small = "1.200"
positive_large = ("1234567890" * 7) + "123.456"
negative_small = "-1.200"
negative_large = "-" + ("1234567890" * 7) + "123.456"
array = Arrow::Decimal256Array.new({precision: 76, scale: 3},
[
positive_large,
positive_small,
nil,
negative_small,
negative_large,
])
type, values = roundtrip(array)
assert_equal([
"Decimal256(76, 3)",
[
BigDecimal(positive_large),
BigDecimal(positive_small),
nil,
BigDecimal(negative_small),
BigDecimal(negative_large),
],
],
[type.to_s, values])
end
def test_list
data_type = Arrow::ListDataType.new(name: "count", type: :int8)
array = Arrow::ListArray.new(data_type, [[-128, 127], nil, [-1, 0, 1]])
type, values = roundtrip(array)
assert_equal(["List<count: Int8>", [[-128, 127], nil, [-1, 0, 1]]],
[type.to_s, values])
end
def test_large_list
data_type = Arrow::LargeListDataType.new(name: "count",
type: :int8)
array = Arrow::LargeListArray.new(data_type,
[[-128, 127], nil, [-1, 0, 1]])
type, values = roundtrip(array)
assert_equal([
"LargeList<count: Int8>",
[
[-128, 127],
nil,
[-1, 0, 1],
],
],
[type.to_s, values])
end
def test_fixed_size_list
data_type = Arrow::FixedSizeListDataType.new({
name: "count",
type: :int8,
},
2)
array = Arrow::FixedSizeListArray.new(data_type,
[[-128, 127], nil, [-1, 1]])
type, values = roundtrip(array)
assert_equal([
"FixedSizeList<count: Int8>(2)",
[
[-128, 127],
nil,
[-1, 1],
],
],
[type.to_s, values])
end
def test_struct
data_type = Arrow::StructDataType.new(count: :int8,
visible: :boolean)
array = Arrow::StructArray.new(data_type,
[[-128, nil], nil, [nil, true]])
type, values = roundtrip(array)
assert_equal([
"Struct<count: Int8, visible: Boolean>",
[
[-128, nil],
nil,
[nil, true],
],
],
[type.to_s, values])
end
def test_dense_union
fields = [
Arrow::Field.new("number", :int8),
Arrow::Field.new("text", :string),
]
type_ids = [11, 13]
data_type = Arrow::DenseUnionDataType.new(fields, type_ids)
types = Arrow::Int8Array.new([11, 13, 11, 13, 13])
value_offsets = Arrow::Int32Array.new([0, 0, 1, 1, 2])
children = [
Arrow::Int8Array.new([1, nil]),
Arrow::StringArray.new(["a", "b", "c"])
]
array = Arrow::DenseUnionArray.new(data_type,
types,
value_offsets,
children)
type, values = roundtrip(array)
assert_equal([
"DenseUnion<number: Int8=11, text: UTF8=13>",
[1, "a", nil, "b", "c"],
],
[type.to_s, values])
end
def test_sparse_union
fields = [
Arrow::Field.new("number", :int8),
Arrow::Field.new("text", :string),
]
type_ids = [11, 13]
data_type = Arrow::SparseUnionDataType.new(fields, type_ids)
types = Arrow::Int8Array.new([11, 13, 11, 13, 11])
children = [
Arrow::Int8Array.new([1, nil, nil, nil, 5]),
Arrow::StringArray.new([nil, "b", nil, "d", nil])
]
array = Arrow::SparseUnionArray.new(data_type, types, children)
type, values = roundtrip(array)
assert_equal([
"SparseUnion<number: Int8=11, text: UTF8=13>",
[1, "b", nil, "d", 5],
],
[type.to_s, values])
end
def test_map
data_type = Arrow::MapDataType.new(:string, :int8)
array = Arrow::MapArray.new(data_type,
[
{"a" => -128, "b" => 127},
nil,
{"c" => nil},
])
type, values = roundtrip(array)
assert_equal([
"Map<UTF8, Int8>",
[
{"a" => -128, "b" => 127},
nil,
{"c" => nil},
],
],
[type.to_s, values])
end
def test_dictionary
values = ["a", "b", "c", nil, "a"]
string_array = Arrow::StringArray.new(values)
array = string_array.dictionary_encode
type, values = roundtrip(array)
assert_equal([
"Dictionary<index=Int32, value=UTF8, ordered=false>",
["a", "b", "c", nil, "a"],
],
[type.to_s, values])
end
end
class TestFileReader < Test::Unit::TestCase
include ReaderTests
def file_extension
"arrow"
end
def reader_class
ArrowFormat::FileReader
end
end
class TestStreamingReader < Test::Unit::TestCase
include ReaderTests
def file_extension
"arrows"
end
def reader_class
ArrowFormat::StreamingReader
end
end