forked from apache/arrow-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreamTest.java
More file actions
436 lines (396 loc) · 15.9 KB
/
Copy pathStreamTest.java
File metadata and controls
436 lines (396 loc) · 15.9 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
/*
* 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.
*/
package org.apache.arrow.c;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.apache.arrow.memory.BufferAllocator;
import org.apache.arrow.memory.RootAllocator;
import org.apache.arrow.util.AutoCloseables;
import org.apache.arrow.vector.FieldVector;
import org.apache.arrow.vector.IntVector;
import org.apache.arrow.vector.VarCharVector;
import org.apache.arrow.vector.VectorLoader;
import org.apache.arrow.vector.VectorSchemaRoot;
import org.apache.arrow.vector.VectorUnloader;
import org.apache.arrow.vector.ViewVarBinaryVector;
import org.apache.arrow.vector.ViewVarCharVector;
import org.apache.arrow.vector.compare.Range;
import org.apache.arrow.vector.compare.RangeEqualsVisitor;
import org.apache.arrow.vector.dictionary.Dictionary;
import org.apache.arrow.vector.dictionary.DictionaryProvider;
import org.apache.arrow.vector.ipc.ArrowReader;
import org.apache.arrow.vector.ipc.message.ArrowRecordBatch;
import org.apache.arrow.vector.types.pojo.ArrowType;
import org.apache.arrow.vector.types.pojo.DictionaryEncoding;
import org.apache.arrow.vector.types.pojo.Field;
import org.apache.arrow.vector.types.pojo.FieldType;
import org.apache.arrow.vector.types.pojo.Schema;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
final class StreamTest {
private RootAllocator allocator = null;
@BeforeEach
public void setUp() {
allocator = new RootAllocator(Long.MAX_VALUE);
}
@AfterEach
public void tearDown() {
allocator.close();
}
@Test
public void testRoundtripInts() throws Exception {
final Schema schema =
new Schema(Collections.singletonList(Field.nullable("ints", new ArrowType.Int(32, true))));
final List<ArrowRecordBatch> batches = new ArrayList<>();
try (final VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator)) {
final IntVector ints = (IntVector) root.getVector(0);
VectorUnloader unloader = new VectorUnloader(root);
root.allocateNew();
ints.setSafe(0, 1);
ints.setSafe(1, 2);
ints.setSafe(2, 4);
ints.setSafe(3, 8);
root.setRowCount(4);
batches.add(unloader.getRecordBatch());
root.allocateNew();
ints.setSafe(0, 1);
ints.setNull(1);
ints.setSafe(2, 4);
ints.setNull(3);
root.setRowCount(4);
batches.add(unloader.getRecordBatch());
roundtrip(schema, batches);
}
}
@Test
public void roundtripStrings() throws Exception {
final Schema schema =
new Schema(
Arrays.asList(
Field.nullable("ints", new ArrowType.Int(32, true)),
Field.nullable("strs", new ArrowType.Utf8())));
final List<ArrowRecordBatch> batches = new ArrayList<>();
try (final VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator)) {
final IntVector ints = (IntVector) root.getVector(0);
final VarCharVector strs = (VarCharVector) root.getVector(1);
VectorUnloader unloader = new VectorUnloader(root);
root.allocateNew();
ints.setSafe(0, 1);
ints.setSafe(1, 2);
ints.setSafe(2, 4);
ints.setSafe(3, 8);
strs.setSafe(0, "".getBytes(StandardCharsets.UTF_8));
strs.setSafe(1, "a".getBytes(StandardCharsets.UTF_8));
strs.setSafe(2, "bc".getBytes(StandardCharsets.UTF_8));
strs.setSafe(3, "defg".getBytes(StandardCharsets.UTF_8));
root.setRowCount(4);
batches.add(unloader.getRecordBatch());
root.allocateNew();
ints.setSafe(0, 1);
ints.setNull(1);
ints.setSafe(2, 4);
ints.setNull(3);
strs.setSafe(0, "".getBytes(StandardCharsets.UTF_8));
strs.setNull(1);
strs.setSafe(2, "bc".getBytes(StandardCharsets.UTF_8));
strs.setNull(3);
root.setRowCount(4);
batches.add(unloader.getRecordBatch());
roundtrip(schema, batches);
}
}
@Test
public void roundtripStringViews() throws Exception {
final Schema schema =
new Schema(
Arrays.asList(
Field.nullable("ints", new ArrowType.Int(32, true)),
Field.nullable("string_views", new ArrowType.Utf8View())));
final List<ArrowRecordBatch> batches = new ArrayList<>();
try (final VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator)) {
final IntVector ints = (IntVector) root.getVector(0);
final ViewVarCharVector strs = (ViewVarCharVector) root.getVector(1);
VectorUnloader unloader = new VectorUnloader(root);
root.allocateNew();
ints.setSafe(0, 1);
ints.setSafe(1, 2);
ints.setSafe(2, 4);
ints.setSafe(3, 8);
strs.setSafe(0, "".getBytes(StandardCharsets.UTF_8));
strs.setSafe(1, "a".getBytes(StandardCharsets.UTF_8));
strs.setSafe(2, "bc1234567890bc".getBytes(StandardCharsets.UTF_8));
strs.setSafe(3, "defg1234567890defg".getBytes(StandardCharsets.UTF_8));
root.setRowCount(4);
batches.add(unloader.getRecordBatch());
root.allocateNew();
ints.setSafe(0, 1);
ints.setNull(1);
ints.setSafe(2, 4);
ints.setNull(3);
strs.setSafe(0, "".getBytes(StandardCharsets.UTF_8));
strs.setNull(1);
strs.setSafe(2, "bc1234567890bc".getBytes(StandardCharsets.UTF_8));
strs.setNull(3);
root.setRowCount(4);
batches.add(unloader.getRecordBatch());
roundtrip(schema, batches);
}
}
@Test
public void roundtripBinaryViews() throws Exception {
final Schema schema =
new Schema(
Arrays.asList(
Field.nullable("ints", new ArrowType.Int(32, true)),
Field.nullable("binary_views", new ArrowType.BinaryView())));
final List<ArrowRecordBatch> batches = new ArrayList<>();
try (final VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator)) {
final IntVector ints = (IntVector) root.getVector(0);
final ViewVarBinaryVector strs = (ViewVarBinaryVector) root.getVector(1);
VectorUnloader unloader = new VectorUnloader(root);
root.allocateNew();
ints.setSafe(0, 1);
ints.setSafe(1, 2);
ints.setSafe(2, 4);
ints.setSafe(3, 8);
strs.setSafe(0, new byte[0]);
strs.setSafe(1, new byte[] {97});
strs.setSafe(2, new byte[] {98, 99, 49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 98, 99});
strs.setSafe(
3,
new byte[] {
100, 101, 102, 103, 49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 100, 101, 102, 103
});
root.setRowCount(4);
batches.add(unloader.getRecordBatch());
root.allocateNew();
ints.setSafe(0, 1);
ints.setNull(1);
ints.setSafe(2, 4);
ints.setNull(3);
strs.setSafe(0, new byte[0]);
strs.setNull(1);
strs.setSafe(2, new byte[] {98, 99, 49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 98, 99});
strs.setNull(3);
root.setRowCount(4);
batches.add(unloader.getRecordBatch());
roundtrip(schema, batches);
}
}
@Test
public void roundtripDictionary() throws Exception {
final ArrowType.Int indexType = new ArrowType.Int(32, true);
final DictionaryEncoding encoding = new DictionaryEncoding(0L, false, indexType);
final Schema schema =
new Schema(
Collections.singletonList(
new Field(
"dict",
new FieldType(/* nullable= */ true, indexType, encoding),
Collections.emptyList())));
final List<ArrowRecordBatch> batches = new ArrayList<>();
try (final CDataDictionaryProvider provider = new CDataDictionaryProvider();
final VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator)) {
final VarCharVector dictionary = new VarCharVector("values", allocator);
dictionary.allocateNew();
dictionary.setSafe(0, "foo".getBytes(StandardCharsets.UTF_8));
dictionary.setSafe(1, "bar".getBytes(StandardCharsets.UTF_8));
dictionary.setNull(2);
dictionary.setValueCount(3);
provider.put(new Dictionary(dictionary, encoding));
final IntVector encoded = (IntVector) root.getVector(0);
VectorUnloader unloader = new VectorUnloader(root);
root.allocateNew();
encoded.setSafe(0, 0);
encoded.setSafe(1, 1);
encoded.setSafe(2, 0);
encoded.setSafe(3, 2);
root.setRowCount(4);
batches.add(unloader.getRecordBatch());
root.allocateNew();
encoded.setSafe(0, 0);
encoded.setNull(1);
encoded.setSafe(2, 1);
encoded.setNull(3);
root.setRowCount(4);
batches.add(unloader.getRecordBatch());
roundtrip(schema, batches, provider);
}
}
@Test
public void importReleasedStream() {
try (final ArrowArrayStream stream = ArrowArrayStream.allocateNew(allocator)) {
Exception e =
assertThrows(
IllegalStateException.class, () -> Data.importArrayStream(allocator, stream));
assertThat(e).hasMessageContaining("Cannot import released ArrowArrayStream");
}
}
@Test
public void getNextError() throws Exception {
final Schema schema =
new Schema(Collections.singletonList(Field.nullable("ints", new ArrowType.Int(32, true))));
final List<ArrowRecordBatch> batches = new ArrayList<>();
try (final ArrowReader source =
new InMemoryArrowReader(
allocator, schema, batches, new DictionaryProvider.MapDictionaryProvider()) {
@Override
public boolean loadNextBatch() throws IOException {
throw new IOException("Failed to load batch!");
}
};
final ArrowArrayStream stream = ArrowArrayStream.allocateNew(allocator)) {
Data.exportArrayStream(allocator, source, stream);
try (final ArrowReader reader = Data.importArrayStream(allocator, stream)) {
assertThat(reader.getVectorSchemaRoot().getSchema()).isEqualTo(schema);
final IOException e = assertThrows(IOException.class, reader::loadNextBatch);
assertThat(e).hasMessageContaining("Failed to load batch!");
assertThat(e).hasMessageContaining("[errno ");
}
}
}
@Test
public void getSchemaError() throws Exception {
final Schema schema =
new Schema(Collections.singletonList(Field.nullable("ints", new ArrowType.Int(32, true))));
final List<ArrowRecordBatch> batches = new ArrayList<>();
try (final ArrowReader source =
new InMemoryArrowReader(
allocator, schema, batches, new DictionaryProvider.MapDictionaryProvider()) {
@Override
protected Schema readSchema() {
throw new IllegalArgumentException("Failed to read schema!");
}
};
final ArrowArrayStream stream = ArrowArrayStream.allocateNew(allocator)) {
Data.exportArrayStream(allocator, source, stream);
try (final ArrowReader reader = Data.importArrayStream(allocator, stream)) {
final IOException e = assertThrows(IOException.class, reader::getVectorSchemaRoot);
assertThat(e).hasMessageContaining("Failed to read schema!");
assertThat(e).hasMessageContaining("[errno ");
}
}
}
void roundtrip(Schema schema, List<ArrowRecordBatch> batches, DictionaryProvider provider)
throws Exception {
ArrowReader source = new InMemoryArrowReader(allocator, schema, batches, provider);
try (final ArrowArrayStream stream = ArrowArrayStream.allocateNew(allocator);
final VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator)) {
final VectorLoader loader = new VectorLoader(root);
Data.exportArrayStream(allocator, source, stream);
try (final ArrowReader reader = Data.importArrayStream(allocator, stream)) {
assertThat(reader.getVectorSchemaRoot().getSchema()).isEqualTo(schema);
for (ArrowRecordBatch batch : batches) {
assertThat(reader.loadNextBatch()).isTrue();
loader.load(batch);
assertThat(reader.getVectorSchemaRoot().getRowCount()).isEqualTo(root.getRowCount());
for (int i = 0; i < root.getFieldVectors().size(); i++) {
final FieldVector expected = root.getVector(i);
final FieldVector actual = reader.getVectorSchemaRoot().getVector(i);
assertVectorsEqual(expected, actual);
}
}
assertThat(reader.loadNextBatch()).isFalse();
assertThat(reader.getDictionaryIds()).isEqualTo(provider.getDictionaryIds());
for (Map.Entry<Long, Dictionary> entry : reader.getDictionaryVectors().entrySet()) {
final FieldVector expected = provider.lookup(entry.getKey()).getVector();
final FieldVector actual = entry.getValue().getVector();
assertVectorsEqual(expected, actual);
}
}
}
}
void roundtrip(Schema schema, List<ArrowRecordBatch> batches) throws Exception {
roundtrip(schema, batches, new CDataDictionaryProvider());
}
private static void assertVectorsEqual(FieldVector expected, FieldVector actual) {
assertThat(actual.getField().getType()).isEqualTo(expected.getField().getType());
assertThat(actual.getValueCount()).isEqualTo(expected.getValueCount());
final Range range =
new Range(/* leftStart= */ 0, /* rightStart= */ 0, expected.getValueCount());
assertThat(new RangeEqualsVisitor(expected, actual).rangeEquals(range))
.as("Vectors were not equal.\nExpected: %s\nGot: %s", expected, actual)
.isTrue();
}
/** An ArrowReader backed by a fixed list of batches. */
static class InMemoryArrowReader extends ArrowReader {
private final Schema schema;
private final List<ArrowRecordBatch> batches;
private final DictionaryProvider provider;
private int nextBatch;
InMemoryArrowReader(
BufferAllocator allocator,
Schema schema,
List<ArrowRecordBatch> batches,
DictionaryProvider provider) {
super(allocator);
this.schema = schema;
this.batches = batches;
this.provider = provider;
this.nextBatch = 0;
}
@Override
public Dictionary lookup(long id) {
return provider.lookup(id);
}
@Override
public Set<Long> getDictionaryIds() {
return provider.getDictionaryIds();
}
@Override
public Map<Long, Dictionary> getDictionaryVectors() {
return getDictionaryIds().stream()
.collect(Collectors.toMap(Function.identity(), this::lookup));
}
@Override
public boolean loadNextBatch() throws IOException {
if (nextBatch < batches.size()) {
VectorLoader loader = new VectorLoader(getVectorSchemaRoot());
loader.load(batches.get(nextBatch++));
return true;
}
return false;
}
@Override
public long bytesRead() {
return 0;
}
@Override
protected void closeReadSource() throws IOException {
try {
AutoCloseables.close(batches);
} catch (Exception e) {
throw new IOException(e);
}
}
@Override
protected Schema readSchema() {
return schema;
}
}
}