/
RepeatedListVector.java
424 lines (351 loc) · 13 KB
/
RepeatedListVector.java
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
/**
* 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.drill.exec.vector.complex;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import io.netty.buffer.DrillBuf;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import org.apache.drill.common.expression.FieldReference;
import org.apache.drill.common.expression.SchemaPath;
import org.apache.drill.common.types.TypeProtos.MajorType;
import org.apache.drill.common.types.TypeProtos.MinorType;
import org.apache.drill.common.types.Types;
import org.apache.drill.exec.expr.holders.ComplexHolder;
import org.apache.drill.exec.expr.holders.RepeatedListHolder;
import org.apache.drill.exec.memory.BufferAllocator;
import org.apache.drill.exec.memory.OutOfMemoryRuntimeException;
import org.apache.drill.exec.proto.UserBitShared.SerializedField;
import org.apache.drill.exec.record.MaterializedField;
import org.apache.drill.exec.record.TransferPair;
import org.apache.drill.exec.util.JsonStringArrayList;
import org.apache.drill.exec.vector.AddOrGetResult;
import org.apache.drill.exec.util.CallBack;
import org.apache.drill.exec.vector.UInt4Vector;
import org.apache.drill.exec.vector.ValueVector;
import org.apache.drill.exec.vector.VectorDescriptor;
import org.apache.drill.exec.vector.complex.impl.NullReader;
import org.apache.drill.exec.vector.complex.impl.RepeatedListReaderImpl;
import org.apache.drill.exec.vector.complex.reader.FieldReader;
public class RepeatedListVector extends AbstractContainerVector
implements RepeatedValueVector, RepeatedFixedWidthVectorLike {
public final static MajorType TYPE = Types.repeated(MinorType.LIST);
private final RepeatedListReaderImpl reader = new RepeatedListReaderImpl(null, this);
private final DelegateRepeatedVector delegate;
protected static class DelegateRepeatedVector extends BaseRepeatedValueVector {
private final RepeatedListAccessor accessor = new RepeatedListAccessor();
private final RepeatedListMutator mutator = new RepeatedListMutator();
private final EmptyValuePopulator emptyPopulator;
private transient DelegateTransferPair ephPair;
public class RepeatedListAccessor extends BaseRepeatedValueVector.BaseRepeatedAccessor {
@Override
public Object getObject(int index) {
final List<Object> list = new JsonStringArrayList();
final int start = offsets.getAccessor().get(index);
final int until = offsets.getAccessor().get(index+1);
for (int i = start; i < until; i++) {
list.add(vector.getAccessor().getObject(i));
}
return list;
}
public void get(int index, RepeatedListHolder holder) {
assert index <= getValueCapacity();
holder.start = getOffsetVector().getAccessor().get(index);
holder.end = getOffsetVector().getAccessor().get(index+1);
}
public void get(int index, ComplexHolder holder) {
final FieldReader reader = getReader();
reader.setPosition(index);
holder.reader = reader;
}
public void get(int index, int arrayIndex, ComplexHolder holder) {
final RepeatedListHolder listHolder = new RepeatedListHolder();
get(index, listHolder);
int offset = listHolder.start + arrayIndex;
if (offset >= listHolder.end) {
holder.reader = NullReader.INSTANCE;
} else {
FieldReader r = getDataVector().getReader();
r.setPosition(offset);
holder.reader = r;
}
}
}
public class RepeatedListMutator extends BaseRepeatedValueVector.BaseRepeatedMutator {
public int add(int index) {
final int curEnd = getOffsetVector().getAccessor().get(index+1);
getOffsetVector().getMutator().setSafe(index + 1, curEnd + 1);
return curEnd;
}
@Override
public void startNewValue(int index) {
emptyPopulator.populate(index+1);
super.startNewValue(index);
}
@Override
public void setValueCount(int valueCount) {
emptyPopulator.populate(valueCount);
super.setValueCount(valueCount);
}
}
public class DelegateTransferPair implements TransferPair {
private final DelegateRepeatedVector target;
private final TransferPair[] children;
public DelegateTransferPair(DelegateRepeatedVector target) {
this.target = Preconditions.checkNotNull(target);
if (target.getDataVector() == DEFAULT_DATA_VECTOR) {
target.addOrGetVector(VectorDescriptor.create(getDataVector().getField()));
target.getDataVector().allocateNew();
}
this.children = new TransferPair[] {
getOffsetVector().makeTransferPair(target.getOffsetVector()),
getDataVector().makeTransferPair(target.getDataVector())
};
}
@Override
public void transfer() {
for (TransferPair child:children) {
child.transfer();
}
}
@Override
public ValueVector getTo() {
return target;
}
@Override
public void splitAndTransfer(int startIndex, int length) {
throw new UnsupportedOperationException("Repeated list does not support split & transfer operation");
}
@Override
public void copyValueSafe(int srcIndex, int destIndex) {
final RepeatedListHolder holder = new RepeatedListHolder();
getAccessor().get(srcIndex, holder);
target.emptyPopulator.populate(destIndex+1);
final TransferPair vectorTransfer = children[1];
int newIndex = target.getOffsetVector().getAccessor().get(destIndex);
//todo: make this a bulk copy.
for (int i = holder.start; i < holder.end; i++, newIndex++) {
vectorTransfer.copyValueSafe(i, newIndex);
}
target.getOffsetVector().getMutator().setSafe(destIndex + 1, newIndex);
}
}
public DelegateRepeatedVector(SchemaPath path, BufferAllocator allocator) {
this(MaterializedField.create(path, TYPE), allocator);
}
public DelegateRepeatedVector(MaterializedField field, BufferAllocator allocator) {
super(field, allocator);
this.emptyPopulator = new EmptyValuePopulator(getOffsetVector());
}
@Override
public void allocateNew() throws OutOfMemoryRuntimeException {
if (!allocateNewSafe()) {
throw new OutOfMemoryRuntimeException();
}
}
@Override
public TransferPair getTransferPair(FieldReference ref) {
return makeTransferPair(new DelegateRepeatedVector(ref, allocator));
}
@Override
public TransferPair makeTransferPair(ValueVector target) {
return new DelegateTransferPair(DelegateRepeatedVector.class.cast(target));
}
@Override
public RepeatedListAccessor getAccessor() {
return accessor;
}
@Override
public RepeatedListMutator getMutator() {
return mutator;
}
@Override
public FieldReader getReader() {
throw new UnsupportedOperationException();
}
public void copyFromSafe(int fromIndex, int thisIndex, DelegateRepeatedVector from) {
if(ephPair == null || ephPair.target != from) {
ephPair = DelegateTransferPair.class.cast(from.makeTransferPair(this));
}
ephPair.copyValueSafe(fromIndex, thisIndex);
}
}
protected class RepeatedListTransferPair implements TransferPair {
private final TransferPair delegate;
public RepeatedListTransferPair(TransferPair delegate) {
this.delegate = delegate;
}
public void transfer() {
delegate.transfer();
}
@Override
public void splitAndTransfer(int startIndex, int length) {
delegate.splitAndTransfer(startIndex, length);
}
@Override
public ValueVector getTo() {
final DelegateRepeatedVector delegateVector = DelegateRepeatedVector.class.cast(delegate.getTo());
return new RepeatedListVector(getField(), allocator, callBack, delegateVector);
}
@Override
public void copyValueSafe(int from, int to) {
delegate.copyValueSafe(from, to);
}
}
public RepeatedListVector(SchemaPath path, BufferAllocator allocator, CallBack callBack) {
this(MaterializedField.create(path, TYPE), allocator, callBack);
}
public RepeatedListVector(MaterializedField field, BufferAllocator allocator, CallBack callBack) {
this(field, allocator, callBack, new DelegateRepeatedVector(field, allocator));
}
protected RepeatedListVector(MaterializedField field, BufferAllocator allocator, CallBack callBack, DelegateRepeatedVector delegate) {
super(field, allocator, callBack);
this.delegate = Preconditions.checkNotNull(delegate);
final List<MaterializedField> children = Lists.newArrayList(field.getChildren());
final int childSize = children.size();
assert childSize < 3;
final boolean hasChild = childSize > 0;
if (hasChild) {
// the last field is data field
final MaterializedField child = children.get(childSize-1);
addOrGetVector(VectorDescriptor.create(child));
}
}
@Override
public RepeatedListReaderImpl getReader() {
return reader;
}
@Override
public DelegateRepeatedVector.RepeatedListAccessor getAccessor() {
return delegate.getAccessor();
}
@Override
public DelegateRepeatedVector.RepeatedListMutator getMutator() {
return delegate.getMutator();
}
@Override
public UInt4Vector getOffsetVector() {
return delegate.getOffsetVector();
}
@Override
public ValueVector getDataVector() {
return delegate.getDataVector();
}
@Override
public void allocateNew() throws OutOfMemoryRuntimeException {
delegate.allocateNew();
}
@Override
public boolean allocateNewSafe() {
return delegate.allocateNewSafe();
}
@Override
public <T extends ValueVector> AddOrGetResult<T> addOrGetVector(VectorDescriptor descriptor) {
final AddOrGetResult<T> result = delegate.addOrGetVector(descriptor);
if (result.isCreated() && callBack != null) {
callBack.doWork();
}
return result;
}
@Override
public int size() {
return delegate.size();
}
@Override
public int getBufferSize() {
return delegate.getBufferSize();
}
@Override
public void close() {
delegate.close();
}
@Override
public void clear() {
delegate.clear();
}
@Override
public TransferPair getTransferPair() {
return new RepeatedListTransferPair(delegate.getTransferPair());
}
@Override
public TransferPair getTransferPair(FieldReference ref) {
return new RepeatedListTransferPair(delegate.getTransferPair(ref));
}
@Override
public TransferPair makeTransferPair(ValueVector to) {
final RepeatedListVector target = RepeatedListVector.class.cast(to);
return new RepeatedListTransferPair(delegate.makeTransferPair(target.delegate));
}
@Override
public int getValueCapacity() {
return delegate.getValueCapacity();
}
@Override
public DrillBuf[] getBuffers(boolean clear) {
return delegate.getBuffers(clear);
}
@Override
public void load(SerializedField metadata, DrillBuf buf) {
delegate.load(metadata, buf);
}
@Override
public SerializedField getMetadata() {
return delegate.getMetadata();
}
@Override
public Iterator<ValueVector> iterator() {
return delegate.iterator();
}
@Override
public void setInitialCapacity(int numRecords) {
delegate.setInitialCapacity(numRecords);
}
/**
* @deprecated
* prefer using {@link #addOrGetVector(org.apache.drill.exec.vector.VectorDescriptor)} instead.
*/
@Override
public <T extends ValueVector> T addOrGet(String name, MajorType type, Class<T> clazz) {
final AddOrGetResult<T> result = addOrGetVector(VectorDescriptor.create(type));
return result.getVector();
}
@Override
public <T extends ValueVector> T getChild(String name, Class<T> clazz) {
if (name != null) {
return null;
}
return typeify(delegate.getDataVector(), clazz);
}
@Override
public void allocateNew(int valueCount, int innerValueCount) {
clear();
getOffsetVector().allocateNew(valueCount + 1);
getMutator().reset();
}
@Override
public VectorWithOrdinal getChildVectorWithOrdinal(String name) {
if (name != null) {
return null;
}
return new VectorWithOrdinal(delegate.getDataVector(), 0);
}
public void copyFromSafe(int fromIndex, int thisIndex, RepeatedListVector from) {
delegate.copyFromSafe(fromIndex, thisIndex, from.delegate);
}
}