/
ColumnBuilder.java
637 lines (545 loc) · 27.8 KB
/
ColumnBuilder.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
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
/*
* 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.physical.resultSet.impl;
import java.util.ArrayList;
import org.apache.drill.common.types.TypeProtos.DataMode;
import org.apache.drill.common.types.TypeProtos.MinorType;
import org.apache.drill.common.types.Types;
import org.apache.drill.exec.physical.resultSet.impl.ColumnState.PrimitiveColumnState;
import org.apache.drill.exec.physical.resultSet.impl.ListState.ListVectorState;
import org.apache.drill.exec.physical.resultSet.impl.ProjectionFilter.ProjResult;
import org.apache.drill.exec.physical.resultSet.impl.RepeatedListState.RepeatedListColumnState;
import org.apache.drill.exec.physical.resultSet.impl.RepeatedListState.RepeatedListVectorState;
import org.apache.drill.exec.physical.resultSet.impl.SingleVectorState.OffsetVectorState;
import org.apache.drill.exec.physical.resultSet.impl.SingleVectorState.SimpleVectorState;
import org.apache.drill.exec.physical.resultSet.impl.TupleState.MapArrayState;
import org.apache.drill.exec.physical.resultSet.impl.TupleState.MapColumnState;
import org.apache.drill.exec.physical.resultSet.impl.TupleState.MapVectorState;
import org.apache.drill.exec.physical.resultSet.impl.TupleState.SingleDictState;
import org.apache.drill.exec.physical.resultSet.impl.TupleState.SingleMapState;
import org.apache.drill.exec.physical.resultSet.impl.UnionState.UnionColumnState;
import org.apache.drill.exec.physical.resultSet.impl.UnionState.UnionVectorState;
import org.apache.drill.exec.record.MaterializedField;
import org.apache.drill.exec.record.metadata.ColumnMetadata;
import org.apache.drill.exec.record.metadata.PrimitiveColumnMetadata;
import org.apache.drill.exec.record.metadata.VariantMetadata;
import org.apache.drill.exec.vector.NullableVector;
import org.apache.drill.exec.vector.UInt4Vector;
import org.apache.drill.exec.vector.ValueVector;
import org.apache.drill.exec.vector.accessor.writer.AbstractArrayWriter;
import org.apache.drill.exec.vector.accessor.writer.AbstractArrayWriter.ArrayObjectWriter;
import org.apache.drill.exec.vector.accessor.writer.AbstractObjectWriter;
import org.apache.drill.exec.vector.accessor.writer.AbstractTupleWriter.TupleObjectWriter;
import org.apache.drill.exec.vector.accessor.writer.ColumnWriterFactory;
import org.apache.drill.exec.vector.accessor.writer.EmptyListShim;
import org.apache.drill.exec.vector.accessor.writer.ListWriterImpl;
import org.apache.drill.exec.vector.accessor.writer.MapWriter;
import org.apache.drill.exec.vector.accessor.writer.ObjectDictWriter;
import org.apache.drill.exec.vector.accessor.writer.RepeatedListWriter;
import org.apache.drill.exec.vector.accessor.writer.UnionWriterImpl;
import org.apache.drill.exec.vector.accessor.writer.UnionWriterImpl.VariantObjectWriter;
import org.apache.drill.exec.vector.complex.DictVector;
import org.apache.drill.exec.vector.complex.ListVector;
import org.apache.drill.exec.vector.complex.MapVector;
import org.apache.drill.exec.vector.complex.RepeatedDictVector;
import org.apache.drill.exec.vector.complex.RepeatedListVector;
import org.apache.drill.exec.vector.complex.RepeatedMapVector;
import org.apache.drill.exec.vector.complex.RepeatedValueVector;
import org.apache.drill.exec.vector.complex.UnionVector;
/**
* Algorithms for building a column given a metadata description of the column and
* the parent context that will hold the column.
* <p>
* Does not support recursive column creation. For the most part, composite columns
* (maps, map arrays, unions and lists) must start empty. Build the composite first,
* then add its members using the writer for the column. This ensures a uniform API
* for adding columns whether done dynamically at read time or statically at create
* time.
* <p>
* The single exception is the case of a list with exactly one type: in this case
* the list metadata must contain that one type so the code knows how to build
* the nullable array writer for that column.
* <p>
* Merges the project list with the column to be built. If the column is not
* projected (not in the list), then creates a dummy writer. Issues an error if
* the column is projected, but the implied projection type is incompatible with
* the actual type. (Such as trying to project an INT as x[0].)
* <p>
* This class builds the internal structure of a vector. If building a "container"
* vector (map, list, repeated list or union), this class expects the container
* to be added empty, then the members to be added one by one. See
* {@link BuildFromSchema} for the class that builds up a compound structure.
*/
public class ColumnBuilder {
/**
* Implementation of the work to add a new column to this tuple given a
* schema description of the column.
*
* @param parent container of vectors
* @param columnSchema schema of the column as provided by the client
* using the result set loader. This is the schema of the data to be
* loaded
* @return writer for the new column
*/
public ColumnState buildColumn(ContainerState parent, ColumnMetadata columnSchema) {
switch (columnSchema.structureType()) {
case DICT:
return buildDict(parent, columnSchema);
case TUPLE:
return buildMap(parent, columnSchema);
case VARIANT:
return buildVariant(parent, columnSchema);
case MULTI_ARRAY:
return buildRepeatedList(parent, columnSchema);
default:
return buildPrimitive(parent, columnSchema);
}
}
/**
* Build a primitive column. Check if the column is projected. If not,
* allocate a dummy writer for the column. If projected, then allocate
* a vector, a writer, and the column state which binds the two together
* and manages the column.
*
* @param parent schema of the new primitive column
* @param columnSchema implied projection type for the column
* @return column state for the new column
*/
private ColumnState buildPrimitive(ContainerState parent, ColumnMetadata columnSchema) {
final ValueVector vector;
if (parent.projection().projection(columnSchema).isProjected || allowCreation(parent)) {
// Create the vector for the column.
vector = parent.vectorCache().vectorFor(columnSchema.schema());
// In permissive mode, the mode or precision of the vector may differ
// from that requested. Update the schema to match.
if (parent.vectorCache().isPermissive() && !vector.getField().isEquivalent(columnSchema.schema())) {
columnSchema = ((PrimitiveColumnMetadata) columnSchema).mergeWith(vector.getField());
}
} else {
// Column is not projected. No materialized backing for the column.
vector = null;
}
// Create the writer.
final AbstractObjectWriter colWriter =
ColumnWriterFactory.buildColumnWriter(columnSchema, vector);
// Build the vector state which manages the vector.
VectorState vectorState;
if (vector == null) {
vectorState = new NullVectorState();
} else if (columnSchema.isArray()) {
vectorState = new RepeatedVectorState(colWriter.array(), (RepeatedValueVector) vector);
} else if (columnSchema.isNullable()) {
vectorState = new NullableVectorState(
colWriter, (NullableVector) vector);
} else {
vectorState = SimpleVectorState.vectorState(columnSchema,
colWriter.events(), vector);
}
// Create the column state which binds the vector and writer together.
return new PrimitiveColumnState(parent.loader(), colWriter, vectorState);
}
/**
* Check if this is a special case when vector, writer and column state should be
* created for a primitive field though the field itself is not projected. This is
* needed in case when {@code DICT}'s {@code value} is accessed by key, because
* {@code DICT}'s {@code keys} field is not projected but is needed to be initialized
* to ensure the dict vector is constructed properly ({@code DICT} should have both
* {@code keys} and {@code values} vectors as they are paired).
*
* @param parent container containing the primitive
* @return {@code true} if the parent is {@code DICT} and its {@code value} is accessed by key
*/
private boolean allowCreation(ContainerState parent) {
return parent instanceof TupleState.DictState && !parent.projection().isEmpty();
}
/**
* Build a new map (single or repeated) column. Except for maps nested inside
* of unions, no map vector is created
* here, instead we create a tuple state to hold the columns, and defer the
* map vector (or vector container) until harvest time.
*
* @param parent description of the map column
* @param columnSchema implied projection type for the column
* @return column state for the map column
*/
private ColumnState buildMap(ContainerState parent, ColumnMetadata columnSchema) {
// When dynamically adding columns, must add the (empty)
// map by itself, then add columns to the map via separate
// calls.
assert columnSchema.isMap();
assert columnSchema.tupleSchema().isEmpty();
// Create the vector, vector state and writer.
if (columnSchema.isArray()) {
return buildMapArray(parent, columnSchema);
} else {
return buildSingleMap(parent, columnSchema);
}
}
private ColumnState buildSingleMap(ContainerState parent, ColumnMetadata columnSchema) {
final ProjectionFilter projFilter = parent.projection();
final ProjResult projResult = projFilter.projection(columnSchema);
final MapVector vector;
final VectorState vectorState;
if (projResult.isProjected) {
// Don't get the map vector from the vector cache. Map vectors may
// have content that varies from batch to batch. Only the leaf
// vectors can be cached.
assert columnSchema.tupleSchema().isEmpty();
vector = new MapVector(columnSchema.schema(), parent.loader().allocator(), null);
vectorState = new MapVectorState(vector, new NullVectorState());
} else {
vector = null;
vectorState = new NullVectorState();
}
final TupleObjectWriter mapWriter = MapWriter.buildMap(columnSchema, vector, new ArrayList<>());
final SingleMapState mapState = new SingleMapState(parent.loader(),
parent.vectorCache().childCache(columnSchema.name()),
projResult.mapFilter);
return new MapColumnState(mapState, mapWriter, vectorState, parent.isVersioned());
}
private ColumnState buildMapArray(ContainerState parent, ColumnMetadata columnSchema) {
final ProjectionFilter projFilter = parent.projection();
final ProjResult projResult = projFilter.projection(columnSchema);
// Create the map's offset vector.
final RepeatedMapVector mapVector;
final UInt4Vector offsetVector;
if (projResult.isProjected) {
// Creating the map vector will create its contained vectors if we
// give it a materialized field with children. So, instead pass a clone
// without children so we can add them.
final ColumnMetadata mapColSchema = columnSchema.cloneEmpty();
// Don't get the map vector from the vector cache. Map vectors may
// have content that varies from batch to batch. Only the leaf
// vectors can be cached.
assert columnSchema.tupleSchema().isEmpty();
mapVector = new RepeatedMapVector(mapColSchema.schema(),
parent.loader().allocator(), null);
offsetVector = mapVector.getOffsetVector();
} else {
mapVector = null;
offsetVector = null;
}
// Create the writer using the offset vector
final AbstractObjectWriter writer = MapWriter.buildMapArray(
columnSchema, mapVector, new ArrayList<>());
// Wrap the offset vector in a vector state
VectorState offsetVectorState;
if (!projResult.isProjected) {
offsetVectorState = new NullVectorState();
} else {
offsetVectorState = new OffsetVectorState(
(((AbstractArrayWriter) writer.array()).offsetWriter()),
offsetVector,
writer.array().entry().events());
}
final VectorState mapVectorState = new MapVectorState(mapVector, offsetVectorState);
// Assemble it all into the column state.
final MapArrayState mapState = new MapArrayState(parent.loader(),
parent.vectorCache().childCache(columnSchema.name()),
projResult.mapFilter);
return new MapColumnState(mapState, writer, mapVectorState, parent.isVersioned());
}
private ColumnState buildVariant(ContainerState parent,
ColumnMetadata columnSchema) {
// Variant: UNION or (non-repeated) LIST
if (columnSchema.isArray()) {
// (non-repeated) LIST (somewhat like a repeated UNION)
return buildList(parent, columnSchema);
} else {
// (Non-repeated) UNION
return buildUnion(parent, columnSchema);
}
}
/**
* Builds a union column.
* <p>
* The union vector type is not well supported in Drill. The idea is that
* arbitrary operators can absorb schema changes by converting vectors to
* unions so that an operator can handle, say, a nullable int and a varchar.
* In practice, most operators don't support this feature. (Sort does -- but
* does not manage memory for the union case.) In principal, union can't solve
* the problem because ODBC and JDBC don't support unions, and it is easy
* to envision changes that unions won't solve (int and varchar types combining
* in a join column, say.) Still, Drill supports unions, so the code here
* does so. Unions are fully tested in the row set writer mechanism.
*
* @param parent container of vectors
* @param columnSchema implied projection type for the column
* @return column
*/
private ColumnState buildUnion(ContainerState parent, ColumnMetadata columnSchema) {
assert columnSchema.isVariant() && ! columnSchema.isArray();
// Create the union vector.
// Don't get the union vector from the vector cache. Union vectors may
// have content that varies from batch to batch. Only the leaf
// vectors can be cached.
assert columnSchema.variantSchema().size() == 0;
final UnionVector vector = new UnionVector(columnSchema.schema(), parent.loader().allocator(), null);
// Then the union writer.
final UnionWriterImpl unionWriter = new UnionWriterImpl(columnSchema, vector, null);
final VariantObjectWriter writer = new VariantObjectWriter(unionWriter);
// The union vector state which manages the types vector.
final UnionVectorState vectorState = new UnionVectorState(vector, unionWriter);
// Create the manager for the columns within the union.
final UnionState unionState = new UnionState(parent.loader(),
parent.vectorCache().childCache(columnSchema.name()));
// Bind the union state to the union writer to handle column additions.
unionWriter.bindListener(unionState);
// Assemble it all into a union column state.
return new UnionColumnState(parent.loader(), writer, vectorState, unionState);
}
private ColumnState buildList(ContainerState parent, ColumnMetadata columnSchema) {
// If the list has declared a single type, and has indicated that this
// is the only type expected, then build the list as a nullable array
// of that type. Else, build the list as array of (a possibly empty)
// union.
final VariantMetadata variant = columnSchema.variantSchema();
if (variant.isSimple()) {
if (variant.size() == 1) {
return buildSimpleList(parent, columnSchema);
} else if (variant.size() == 0) {
throw new IllegalArgumentException("Size of a non-expandable list can't be zero");
}
}
return buildUnionList(parent, columnSchema);
}
/**
* Create a list that is promised to only ever contain a single type (at least
* during this write session). The list acts as a repeated vector in which each
* element can be null. The writer is presented as an array of the single type.
* <p>
* List vectors (lists of optional values) are not supported in
* Drill. The code here works up through the scan operator. But, other operators do
* not support the {@code ListVector</tt> type.
*
* @param parent the parent (tuple, union or list) that holds this list
* @param columnSchema metadata description of the list which must contain
* exactly one subtype
* @return the column state for the list
*/
private ColumnState buildSimpleList(ContainerState parent, ColumnMetadata columnSchema) {
// The variant must have the one and only type.
assert columnSchema.variantSchema().size() == 1;
assert columnSchema.variantSchema().isSimple();
// Create the manager for the one and only column within the list.
final ListState listState = new ListState(parent.loader(),
parent.vectorCache().childCache(columnSchema.name()));
// Create the child vector, writer and state.
final ColumnMetadata memberSchema = columnSchema.variantSchema().listSubtype();
final ColumnState memberState = buildColumn(listState, memberSchema);
listState.setSubColumn(memberState);
// Create the list vector. Contains a single type.
final ListVector listVector = new ListVector(columnSchema.schema().cloneEmpty(),
parent.loader().allocator(), null);
listVector.setChildVector(memberState.vector());
// Create the list writer: an array of the one type.
final ListWriterImpl listWriter = new ListWriterImpl(columnSchema,
listVector, memberState.writer());
final AbstractObjectWriter listObjWriter = new ArrayObjectWriter(listWriter);
// Create the list vector state that tracks the list vector lifecycle.
final ListVectorState vectorState = new ListVectorState(listWriter,
memberState.writer().events(), listVector);
// Assemble it all into a union column state.
return new UnionColumnState(parent.loader(),
listObjWriter, vectorState, listState);
}
/**
* Create a list based on a (possible) union. The list starts empty here. The client
* can then add types as they are discovered. The list itself will transition from a
* list of nulls (no child type), to a list of a single type, to a list of unions.
* The writer interface will consistently present the list as a list of unions, even
* when the list itself has no subtype or a single subtype.
* <p>
* List vectors (lists of unions) are not supported in
* Drill. The code here works up through the scan operator. But, other operators do
* not support the {@code ListVector} type.
*
* @param parent the parent (tuple, union or list) that holds this list
* @param columnSchema description of the list (must be empty of
* subtypes)
* @return the column state for the list
*/
private ColumnState buildUnionList(ContainerState parent, ColumnMetadata columnSchema) {
// The variant must start out empty.
assert columnSchema.variantSchema().size() == 0;
// Create the union writer, bound to an empty list shim.
final UnionWriterImpl unionWriter = new UnionWriterImpl(columnSchema);
unionWriter.bindShim(new EmptyListShim());
final VariantObjectWriter unionObjWriter = new VariantObjectWriter(unionWriter);
// Create the list vector. Starts with the default (dummy) data
// vector which corresponds to the empty union shim above.
// Don't get the list vector from the vector cache. List vectors may
// have content that varies from batch to batch. Only the leaf
// vectors can be cached.
final ListVector listVector = new ListVector(columnSchema.schema(),
parent.loader().allocator(), null);
// Create the list vector state that tracks the list vector lifecycle.
final ListVectorState vectorState = new ListVectorState(unionWriter, listVector);
// Create the list writer: an array of unions.
final AbstractObjectWriter listWriter = new ArrayObjectWriter(
new ListWriterImpl(columnSchema, listVector, unionObjWriter));
// Create the manager for the columns within the list (which may or
// may not be grouped into a union.)
final ListState listState = new ListState(parent.loader(),
parent.vectorCache().childCache(columnSchema.name()));
// Bind the union state to the union writer to handle column additions.
unionWriter.bindListener(listState);
// Assemble it all into a union column state.
return new UnionColumnState(parent.loader(),
listWriter, vectorState, listState);
}
private ColumnState buildRepeatedList(ContainerState parent,
ColumnMetadata columnSchema) {
assert columnSchema.type() == MinorType.LIST;
assert columnSchema.mode() == DataMode.REPEATED;
// The schema provided must be empty. The caller must add
// the element type after creating the repeated writer itself.
assert columnSchema.childSchema() == null;
// Build the repeated vector.
final RepeatedListVector vector = new RepeatedListVector(
columnSchema.emptySchema(), parent.loader().allocator(), null);
// No inner type yet. The result set loader builds the subtype
// incrementally because it might be complex (a map or another
// repeated list.) To start, use a dummy to avoid need for if-statements
// everywhere.
final ColumnMetadata dummyElementSchema = new PrimitiveColumnMetadata(
MaterializedField.create(columnSchema.name(),
Types.repeated(MinorType.NULL)));
final AbstractObjectWriter dummyElement = ColumnWriterFactory.buildDummyColumnWriter(dummyElementSchema);
// Create the list writer: an array of arrays.
final AbstractObjectWriter arrayWriter = RepeatedListWriter.buildRepeatedList(
columnSchema, vector, dummyElement);
// Create the list vector state that tracks the list vector lifecycle.
final RepeatedListVectorState vectorState = new RepeatedListVectorState(
arrayWriter, vector);
// Build the container that tracks the array contents
final RepeatedListState listState = new RepeatedListState(
parent.loader(),
parent.vectorCache().childCache(columnSchema.name()));
// Bind the list state as the list event listener.
((RepeatedListWriter) arrayWriter.array()).bindListener(listState);
// Assemble it all into a column state. This state will
// propagate events down to the (one and only) child state.
return new RepeatedListColumnState(parent.loader(),
arrayWriter, vectorState, listState);
}
private ColumnState buildDict(ContainerState parent, ColumnMetadata columnSchema) {
// When dynamically adding columns, must add the (empty)
// dict by itself, then add columns to the dict via separate
// calls (the same way as is done for MAP).
assert columnSchema.isDict();
assert columnSchema.tupleSchema().isEmpty();
// Create the vector, vector state and writer.
if (columnSchema.isArray()) {
return buildDictArray(parent, columnSchema);
} else {
return buildSingleDict(parent, columnSchema);
}
}
private ColumnState buildDictArray(ContainerState parent, ColumnMetadata columnSchema) {
final ProjectionFilter projFilter = parent.projection();
final ProjResult projResult = projFilter.projection(columnSchema);
// Create the dict's offset vector.
final RepeatedDictVector repeatedDictVector;
final UInt4Vector offsetVector;
if (projResult.isProjected) {
// Creating the dict vector will create its contained vectors if we
// give it a materialized field with children. So, instead pass a clone
// without children so we can add them.
final ColumnMetadata dictColMetadata = columnSchema.cloneEmpty();
// Don't get the dict vector from the vector cache. Dict vectors may
// have content that varies from batch to batch. Only the leaf
// vectors can be cached.
assert columnSchema.tupleSchema().isEmpty();
repeatedDictVector = new RepeatedDictVector(dictColMetadata.schema(),
parent.loader().allocator(), null);
offsetVector = repeatedDictVector.getOffsetVector();
} else {
repeatedDictVector = null;
offsetVector = null;
}
// Create the writer using the offset vector
final AbstractObjectWriter writer = ObjectDictWriter.buildDictArray(
columnSchema, repeatedDictVector, new ArrayList<>());
// Wrap the offset vector in a vector state
VectorState offsetVectorState;
VectorState dictOffsetVectorState;
if (!projResult.isProjected) {
offsetVectorState = new NullVectorState();
dictOffsetVectorState = new NullVectorState();
} else {
AbstractArrayWriter arrayWriter = (AbstractArrayWriter) writer.array();
offsetVectorState = new OffsetVectorState(
arrayWriter.offsetWriter(),
offsetVector,
writer.array().entry().events());
dictOffsetVectorState = new OffsetVectorState(
((AbstractArrayWriter) arrayWriter.array()).offsetWriter(),
((DictVector) repeatedDictVector.getDataVector()).getOffsetVector(),
writer.array().entry().dict().entry().events());
}
final VectorState mapVectorState =
new TupleState.DictArrayVectorState(repeatedDictVector, offsetVectorState, dictOffsetVectorState);
// Assemble it all into the column state.
final TupleState.DictArrayState dictArrayState = new TupleState.DictArrayState(parent.loader(),
parent.vectorCache().childCache(columnSchema.name()),
projResult.mapFilter);
return new TupleState.DictColumnState(
dictArrayState, writer, mapVectorState, parent.isVersioned());
}
private ColumnState buildSingleDict(ContainerState parent, ColumnMetadata columnSchema) {
final ProjectionFilter projFilter = parent.projection();
final ProjResult projResult = projFilter.projection(columnSchema);
// Create the dict's offset vector.
final DictVector dictVector;
final UInt4Vector offsetVector;
if (projResult.isProjected) {
// Creating the dict vector will create its contained vectors if we
// give it a materialized field with children. So, instead pass a clone
// without children so we can add them.
final ColumnMetadata dictColMetadata = columnSchema.cloneEmpty();
// Don't get the dict vector from the vector cache. Dict vectors may
// have content that varies from batch to batch. Only the leaf
// vectors can be cached.
assert columnSchema.tupleSchema().isEmpty();
dictVector = new DictVector(dictColMetadata.schema(), parent.loader().allocator(), null);
offsetVector = dictVector.getOffsetVector();
} else {
dictVector = null;
offsetVector = null;
}
// Create the writer using the offset vector
final AbstractObjectWriter writer = ObjectDictWriter.buildDict(columnSchema, dictVector, new ArrayList<>());
// Wrap the offset vector in a vector state
final VectorState offsetVectorState;
if (!projResult.isProjected) {
offsetVectorState = new NullVectorState();
} else {
offsetVectorState = new OffsetVectorState(
(((AbstractArrayWriter) writer.dict()).offsetWriter()),
offsetVector,
writer.dict().entry().events());
}
final VectorState mapVectorState = new TupleState.SingleDictVectorState(dictVector, offsetVectorState);
// Assemble it all into the column state.
final SingleDictState dictArrayState = new SingleDictState(parent.loader(), parent.vectorCache().childCache(columnSchema.name()),
projResult.mapFilter);
return new TupleState.DictColumnState(
dictArrayState, writer, mapVectorState, parent.isVersioned());
}
}