|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.arrow.c; |
| 18 | + |
| 19 | +import static org.assertj.core.api.Assertions.assertThat; |
| 20 | + |
| 21 | +import java.io.IOException; |
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.Collections; |
| 24 | +import java.util.List; |
| 25 | +import java.util.Map; |
| 26 | +import java.util.Set; |
| 27 | +import java.util.function.Function; |
| 28 | +import java.util.stream.Collectors; |
| 29 | +import org.apache.arrow.memory.BufferAllocator; |
| 30 | +import org.apache.arrow.memory.RootAllocator; |
| 31 | +import org.apache.arrow.vector.IntVector; |
| 32 | +import org.apache.arrow.vector.VectorLoader; |
| 33 | +import org.apache.arrow.vector.VectorSchemaRoot; |
| 34 | +import org.apache.arrow.vector.VectorUnloader; |
| 35 | +import org.apache.arrow.vector.dictionary.Dictionary; |
| 36 | +import org.apache.arrow.vector.dictionary.DictionaryProvider; |
| 37 | +import org.apache.arrow.vector.ipc.ArrowReader; |
| 38 | +import org.apache.arrow.vector.ipc.message.ArrowRecordBatch; |
| 39 | +import org.apache.arrow.vector.types.pojo.ArrowType; |
| 40 | +import org.apache.arrow.vector.types.pojo.Field; |
| 41 | +import org.apache.arrow.vector.types.pojo.Schema; |
| 42 | +import org.junit.jupiter.api.Test; |
| 43 | + |
| 44 | +final class ExceptionTest { |
| 45 | + @Test |
| 46 | + public void testException() throws IOException { |
| 47 | + final Schema schema = |
| 48 | + new Schema(Collections.singletonList(Field.nullable("ints", new ArrowType.Int(32, true)))); |
| 49 | + final List<Object> batches = new ArrayList<>(); |
| 50 | + |
| 51 | + try (BufferAllocator allocator = new RootAllocator(); |
| 52 | + VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator)) { |
| 53 | + final IntVector ints = (IntVector) root.getVector(0); |
| 54 | + VectorUnloader unloader = new VectorUnloader(root); |
| 55 | + |
| 56 | + root.allocateNew(); |
| 57 | + ints.setSafe(0, 1); |
| 58 | + ints.setSafe(1, 2); |
| 59 | + ints.setSafe(2, 4); |
| 60 | + ints.setSafe(3, 8); |
| 61 | + root.setRowCount(4); |
| 62 | + batches.add(unloader.getRecordBatch()); |
| 63 | + |
| 64 | + RuntimeException ex = new RuntimeException("this is an exception test to test TryCopyLastError may works " + |
| 65 | + "but the test cannot make sure there is no problem with it"); |
| 66 | + batches.add(ex); |
| 67 | + |
| 68 | + ArrowReader source = new ExceptionMemoryArrowReader(allocator, schema, batches); |
| 69 | + |
| 70 | + try (final ArrowArrayStream stream = ArrowArrayStream.allocateNew(allocator); |
| 71 | + final VectorSchemaRoot importRoot = VectorSchemaRoot.create(schema, allocator)) { |
| 72 | + final VectorLoader loader = new VectorLoader(importRoot); |
| 73 | + Data.exportArrayStream(allocator, source, stream); |
| 74 | + |
| 75 | + try (final ArrowReader reader = Data.importArrayStream(allocator, stream)) { |
| 76 | + assertThat(reader.getVectorSchemaRoot().getSchema()).isEqualTo(schema); |
| 77 | + |
| 78 | + for (Object batch : batches) { |
| 79 | + try { |
| 80 | + reader.loadNextBatch(); |
| 81 | + } catch (Exception e) { |
| 82 | + continue; |
| 83 | + } |
| 84 | + loader.load((ArrowRecordBatch) batch); |
| 85 | + |
| 86 | + assertThat(reader.getVectorSchemaRoot().getRowCount()).isEqualTo(root.getRowCount()); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + static class ExceptionMemoryArrowReader extends ArrowReader { |
| 94 | + private final Schema schema; |
| 95 | + private final List<Object> batches; // set ArrowRecordBatch or Exception |
| 96 | + private final DictionaryProvider provider; |
| 97 | + private int nextBatch; |
| 98 | + |
| 99 | + ExceptionMemoryArrowReader( |
| 100 | + BufferAllocator allocator, |
| 101 | + Schema schema, |
| 102 | + List<Object> batches) { |
| 103 | + super(allocator); |
| 104 | + this.schema = schema; |
| 105 | + this.batches = batches; |
| 106 | + this.provider = new CDataDictionaryProvider(); |
| 107 | + this.nextBatch = 0; |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public Dictionary lookup(long id) { |
| 112 | + return provider.lookup(id); |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public Set<Long> getDictionaryIds() { |
| 117 | + return provider.getDictionaryIds(); |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public Map<Long, Dictionary> getDictionaryVectors() { |
| 122 | + return getDictionaryIds().stream() |
| 123 | + .collect(Collectors.toMap(Function.identity(), this::lookup)); |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public boolean loadNextBatch() throws IOException { |
| 128 | + if (nextBatch < batches.size()) { |
| 129 | + Object object = batches.get(nextBatch++); |
| 130 | + if (object instanceof RuntimeException) { |
| 131 | + throw (RuntimeException) object; |
| 132 | + } |
| 133 | + VectorLoader loader = new VectorLoader(getVectorSchemaRoot()); |
| 134 | + loader.load((ArrowRecordBatch) object); |
| 135 | + return true; |
| 136 | + } |
| 137 | + return false; |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + public long bytesRead() { |
| 142 | + return 0; |
| 143 | + } |
| 144 | + |
| 145 | + @Override |
| 146 | + protected void closeReadSource() throws IOException { |
| 147 | + try { |
| 148 | + for (Object object : batches) { |
| 149 | + if (object instanceof ArrowRecordBatch) { |
| 150 | + ArrowRecordBatch batch = (ArrowRecordBatch) object; |
| 151 | + batch.close(); |
| 152 | + } |
| 153 | + } |
| 154 | + } catch (Exception e) { |
| 155 | + throw new IOException(e); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + @Override |
| 160 | + protected Schema readSchema() { |
| 161 | + return schema; |
| 162 | + } |
| 163 | + } |
| 164 | +} |
0 commit comments