/
VectorPTFOperator.java
820 lines (711 loc) · 31.6 KB
/
VectorPTFOperator.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
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
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
/*
* 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.hadoop.hive.ql.exec.vector.ptf;
import java.sql.Timestamp;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hive.common.type.DataTypePhysicalVariation;
import org.apache.hadoop.hive.common.type.HiveIntervalDayTime;
import org.apache.hadoop.hive.conf.HiveConf;
import org.apache.hadoop.hive.common.type.DataTypePhysicalVariation;
import org.apache.hadoop.hive.ql.CompilationOpContext;
import org.apache.hadoop.hive.ql.exec.Operator;
import org.apache.hadoop.hive.ql.exec.Utilities;
import org.apache.hadoop.hive.ql.exec.vector.BytesColumnVector;
import org.apache.hadoop.hive.ql.exec.vector.ColumnVector;
import org.apache.hadoop.hive.ql.exec.vector.ColumnVector.Type;
import org.apache.hadoop.hive.ql.exec.vector.DecimalColumnVector;
import org.apache.hadoop.hive.ql.exec.vector.DoubleColumnVector;
import org.apache.hadoop.hive.ql.exec.vector.IntervalDayTimeColumnVector;
import org.apache.hadoop.hive.ql.exec.vector.LongColumnVector;
import org.apache.hadoop.hive.ql.exec.vector.TimestampColumnVector;
import org.apache.hadoop.hive.ql.exec.vector.VectorizationContext;
import org.apache.hadoop.hive.ql.exec.vector.VectorizationContextRegion;
import org.apache.hadoop.hive.ql.exec.vector.VectorizationOperator;
import org.apache.hadoop.hive.ql.exec.vector.VectorizedBatchUtil;
import org.apache.hadoop.hive.ql.exec.vector.VectorizedRowBatch;
import org.apache.hadoop.hive.ql.exec.vector.expressions.StringExpr;
import org.apache.hadoop.hive.ql.exec.vector.expressions.VectorExpression;
import org.apache.hadoop.hive.ql.metadata.HiveException;
import org.apache.hadoop.hive.ql.plan.BaseWork;
import org.apache.hadoop.hive.ql.plan.OperatorDesc;
import org.apache.hadoop.hive.ql.plan.PTFDesc;
import org.apache.hadoop.hive.ql.plan.VectorDesc;
import org.apache.hadoop.hive.ql.plan.VectorPTFDesc;
import org.apache.hadoop.hive.ql.plan.VectorPTFInfo;
import org.apache.hadoop.hive.ql.plan.api.OperatorType;
import org.apache.hadoop.hive.serde.serdeConstants;
import org.apache.hadoop.hive.serde2.io.HiveDecimalWritable;
import org.apache.hadoop.hive.serde2.typeinfo.TypeInfo;
import org.apache.hadoop.hive.serde2.typeinfo.TypeInfoUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.primitives.Ints;
/**
* This class is native vectorized PTF operator class.
*/
public class VectorPTFOperator extends Operator<PTFDesc>
implements VectorizationOperator, VectorizationContextRegion {
private static final long serialVersionUID = 1L;
private static final String CLASS_NAME = VectorPTFOperator.class.getName();
private static final Logger LOG = LoggerFactory.getLogger(CLASS_NAME);
private int[] scratchColumnPositions;
private VectorizationContext vContext;
private VectorPTFDesc vectorDesc;
/**
* Information about our native vectorized PTF created by the Vectorizer class during
* it decision process and useful for execution.
*/
private VectorPTFInfo vectorPTFInfo;
// This is the vectorized row batch description of the output of the native vectorized PTF
// operator. It is based on the incoming vectorization context. Its projection may include
// a mixture of input columns and new scratch columns (for the aggregation output).
protected VectorizationContext vOutContext;
private boolean isPartitionOrderBy;
/**
* PTF vector expressions.
*/
private TypeInfo[] reducerBatchTypeInfos;
private int[] outputProjectionColumnMap;
private String[] outputColumnNames;
private TypeInfo[] outputTypeInfos;
private DataTypePhysicalVariation[] outputDataTypePhysicalVariations;
private int evaluatorCount;
private String[] evaluatorFunctionNames;
private int[] orderColumnMap;
private Type[] orderColumnVectorTypes;
private VectorExpression[] orderExpressions;
private int[] partitionColumnMap;
private Type[] partitionColumnVectorTypes;
private VectorExpression[] partitionExpressions;
private int[] keyInputColumnMap;
private int[] nonKeyInputColumnMap;
// The above members are initialized by the constructor and must not be
// transient.
//---------------------------------------------------------------------------
private transient boolean isLastGroupBatch;
private transient VectorizedRowBatch overflowBatch;
private transient VectorPTFGroupBatches groupBatches;
private transient VectorPTFEvaluatorBase[] evaluators;
private transient int[] streamingEvaluatorNums;
private transient boolean allEvaluatorsAreStreaming;
private transient boolean isFirstPartition;
private transient boolean[] currentPartitionIsNull;
private transient long[] currentPartitionLongs;
private transient double[] currentPartitionDoubles;
private transient byte[][] currentPartitionByteArrays;
private transient int[] currentPartitionByteLengths;
private transient HiveDecimalWritable[] currentPartitionDecimals;
private transient Timestamp[] currentPartitionTimestamps;
private transient HiveIntervalDayTime[] currentPartitionIntervalDayTimes;
private transient int[] bufferedColumnMap;
private transient TypeInfo[] bufferedTypeInfos;
// For debug tracing: the name of the map or reduce task.
private transient String taskName;
// Debug display.
private transient long batchCounter;
//---------------------------------------------------------------------------
/** Kryo ctor. */
protected VectorPTFOperator() {
super();
}
public VectorPTFOperator(CompilationOpContext ctx) {
super(ctx);
}
public VectorPTFOperator(CompilationOpContext ctx, OperatorDesc conf,
VectorizationContext vContext, VectorDesc vectorDesc) throws HiveException {
this(ctx);
LOG.info("VectorPTF constructor");
PTFDesc desc = (PTFDesc) conf;
this.conf = desc;
this.vectorDesc = (VectorPTFDesc) vectorDesc;
vectorPTFInfo = this.vectorDesc.getVectorPTFInfo();
this.vContext = vContext;
reducerBatchTypeInfos = this.vectorDesc.getReducerBatchTypeInfos();
isPartitionOrderBy = this.vectorDesc.getIsPartitionOrderBy();
outputColumnNames = this.vectorDesc.getOutputColumnNames();
outputTypeInfos = this.vectorDesc.getOutputTypeInfos();
outputDataTypePhysicalVariations = this.vectorDesc.getOutputDataTypePhysicalVariations();
outputProjectionColumnMap = vectorPTFInfo.getOutputColumnMap();
/*
* Create a new vectorization context to create a new projection, but keep
* same output column manager must be inherited to track the scratch the columns.
*/
vOutContext = new VectorizationContext(getName(), this.vContext);
setupVOutContext();
evaluatorFunctionNames = this.vectorDesc.getEvaluatorFunctionNames();
evaluatorCount = evaluatorFunctionNames.length;
orderColumnMap = vectorPTFInfo.getOrderColumnMap();
orderColumnVectorTypes = vectorPTFInfo.getOrderColumnVectorTypes();
orderExpressions = vectorPTFInfo.getOrderExpressions();
partitionColumnMap = vectorPTFInfo.getPartitionColumnMap();
partitionColumnVectorTypes = vectorPTFInfo.getPartitionColumnVectorTypes();
partitionExpressions = vectorPTFInfo.getPartitionExpressions();
keyInputColumnMap = vectorPTFInfo.getKeyInputColumnMap();
nonKeyInputColumnMap = vectorPTFInfo.getNonKeyInputColumnMap();
}
/**
* Setup the vectorized row batch description of the output of the native vectorized PTF
* operator. Use the output projection we previously built from a mixture of input
* columns and new scratch columns.
*/
protected void setupVOutContext() {
vOutContext.resetProjectionColumns();
final int count = outputColumnNames.length;
for (int i = 0; i < count; ++i) {
String columnName = outputColumnNames[i];
int outputColumn = outputProjectionColumnMap[i];
vOutContext.addProjectionColumn(columnName, outputColumn);
}
}
/*
* Allocate overflow batch columns by hand.
*/
private static void allocateOverflowBatchColumnVector(VectorizedRowBatch overflowBatch,
int outputColumn, String typeName, DataTypePhysicalVariation dataTypePhysicalVariation)
throws HiveException {
if (overflowBatch.cols[outputColumn] == null) {
typeName = VectorizationContext.mapTypeNameSynonyms(typeName);
TypeInfo typeInfo = TypeInfoUtils.getTypeInfoFromTypeString(typeName);
overflowBatch.cols[outputColumn] =
VectorizedBatchUtil.createColumnVector(typeInfo, dataTypePhysicalVariation);
}
}
/*
* Setup our 2nd batch with the same "column schema" as the output columns plus any scratch
* columns since the overflow batch will get forwarded to children operators.
*
* Considering this query below:
*
* select p_mfgr, p_name,
* count(*) over(partition by p_mfgr order by p_date range between 1 preceding and current row) as cs1,
* count(*) over(partition by p_mfgr order by p_date range between 3 preceding and current row) as cs2
* from vector_ptf_part_simple_orc;
*
* The overflow batch will have the following column structure:
* [0] BytesColumnVector -> p_mfgr (key)
* [1] DateColumnVector -> p_date (key, ordering col)
* [2] BytesColumnVector -> p_name (non-key)
* [3] LongColumnVector -> scratch
* [4] LongColumnVector -> scratch
*/
@VisibleForTesting
VectorizedRowBatch setupOverflowBatch(int firstOutputColumnIndex, String[] scratchColumnTypeNames,
int[] outputProjectionColumnMap, TypeInfo[] outputTypeInfos) throws HiveException {
int initialColumnCount = firstOutputColumnIndex;
VectorizedRowBatch overflowBatch;
int totalNumColumns = initialColumnCount + scratchColumnTypeNames.length;
overflowBatch = new VectorizedRowBatch(totalNumColumns);
// First, just allocate just the output columns we will be using.
for (int i = 0; i < outputProjectionColumnMap.length; i++) {
int outputColumn = outputProjectionColumnMap[i];
String typeName = outputTypeInfos[i].getTypeName();
allocateOverflowBatchColumnVector(overflowBatch, outputColumn, typeName,
vOutContext.getDataTypePhysicalVariation(outputColumn));
}
// Now, add any scratch columns needed for children operators.
int outputColumn = initialColumnCount;
int s = 0;
scratchColumnPositions = new int[scratchColumnTypeNames.length];
for (String typeName : scratchColumnTypeNames) {
allocateOverflowBatchColumnVector(overflowBatch, outputColumn, typeName,
vOutContext.getDataTypePhysicalVariation(outputColumn));
scratchColumnPositions[s] = outputColumn;
outputColumn += 1;
s += 1;
}
overflowBatch.projectedColumns = outputProjectionColumnMap;
overflowBatch.projectionSize = outputProjectionColumnMap.length;
overflowBatch.reset();
return overflowBatch;
}
@Override
protected void initializeOp(Configuration hconf) throws HiveException {
super.initializeOp(hconf);
VectorExpression.doTransientInit(partitionExpressions, hconf);
VectorExpression.doTransientInit(orderExpressions, hconf);
if (LOG.isDebugEnabled()) {
// Determine the name of our map or reduce task for debug tracing.
BaseWork work = Utilities.getMapWork(hconf);
if (work == null) {
work = Utilities.getReduceWork(hconf);
}
taskName = work.getName();
}
final int partitionKeyCount = vectorDesc.getPartitionExprNodeDescs().length;
currentPartitionIsNull = new boolean[partitionKeyCount];
currentPartitionLongs = new long[partitionKeyCount];
currentPartitionDoubles = new double[partitionKeyCount];
currentPartitionByteArrays = new byte[partitionKeyCount][];
currentPartitionByteLengths = new int[partitionKeyCount];
currentPartitionDecimals = new HiveDecimalWritable[partitionKeyCount];
currentPartitionTimestamps = new Timestamp[partitionKeyCount];
currentPartitionIntervalDayTimes = new HiveIntervalDayTime[partitionKeyCount];
/*
* Setup the overflow batch.
*/
overflowBatch = setupOverflowBatch(vContext.firstOutputColumnIndex(),
vOutContext.getScratchColumnTypeNames(), outputProjectionColumnMap, outputTypeInfos);
evaluators = VectorPTFDesc.getEvaluators(vectorDesc, vectorPTFInfo);
for (VectorPTFEvaluatorBase evaluator : evaluators) {
evaluator.setNullsLast(HiveConf.getBoolVar(hconf, HiveConf.ConfVars.HIVE_DEFAULT_NULLS_LAST));
}
streamingEvaluatorNums = VectorPTFDesc.getStreamingEvaluatorNums(evaluators);
allEvaluatorsAreStreaming = (streamingEvaluatorNums.length == evaluatorCount);
groupBatches = new VectorPTFGroupBatches(
hconf, vectorDesc.getVectorizedPTFMaxMemoryBufferingBatchCount());
initBufferedColumns();
initExpressionColumns();
int [] keyWithoutOrderColumnMap = determineKeyColumnsWithoutOrderColumns(keyInputColumnMap, orderColumnMap);
groupBatches.init(
evaluators,
outputProjectionColumnMap,
bufferedColumnMap,
bufferedTypeInfos,
orderColumnMap,
keyWithoutOrderColumnMap,
overflowBatch);
isFirstPartition = true;
batchCounter = 0;
}
@Override
public void setNextVectorBatchGroupStatus(boolean isLastGroupBatch) throws HiveException {
this.isLastGroupBatch = isLastGroupBatch;
}
/**
* We are processing a batch from reduce processor that is only for one reducer key or PTF group.
*
* For a simple OVER (PARTITION BY column) or OVER (ORDER BY column), the reduce processor's
* group key is the partition or order by key.
*
* For an OVER (PARTITION BY column1, ORDER BY column2), the reduce-shuffle group key is
* the combination of the partition column1 and the order by column2. In this case, this method
* has to watch for changes in the partition and reset the group aggregations.
*
* The reduce processor calls setNextVectorBatchGroupStatus beforehand to tell us whether the
* batch supplied to our process method is the last batch for the group key, or not. This helps
* us intelligently process the batch.
*/
@Override
public void process(Object row, int tag) throws HiveException {
VectorizedRowBatch batch = (VectorizedRowBatch) row;
for (VectorExpression orderExpression : orderExpressions) {
orderExpression.evaluate(batch);
}
if (partitionExpressions != null) {
for (VectorExpression partitionExpression : partitionExpressions) {
partitionExpression.evaluate(batch);
}
}
if (isFirstPartition) {
isFirstPartition = false;
setCurrentPartition(batch);
} else if (isPartitionChanged(batch)) {
/*
* We should take care of evaluating the previous partition, but only if not all the
* evaluators are streaming. If they are all streaming, are supposed to put their results into
* the column vectors on the fly (in a streaming manner), this is handled later on by calling
* groupBatches.evaluateStreamingGroupBatch for every single batch.
*/
if (!allEvaluatorsAreStreaming){
finishPartition(getPartitionKey());
}
setCurrentPartition(batch);
groupBatches.resetEvaluators();
}
if (allEvaluatorsAreStreaming) {
// We can process this batch immediately.
groupBatches.evaluateStreamingGroupBatch(batch, isLastGroupBatch);
forward(batch, null);
} else {
// only collecting the batch for later evaluation
groupBatches.bufferGroupBatch(batch, isLastGroupBatch);
}
// If we are only processing a PARTITION BY and isLastGroupBatch, reset our evaluators.
if (!isPartitionOrderBy && isLastGroupBatch) {
groupBatches.resetEvaluators();
}
}
private void initBufferedColumns() {
/*
* If we have more than one group key batch, we will buffer their contents.
* We don't buffer the partitioning column since it's a constant for the whole partition.
* We buffer:
* 1. order columns
* 2. the non-key input columns
* 3. any streaming columns that will already have their output values
* 4. scratch columns (as they're used by input expressions of evaluators)
*/
int bufferedColumnCount = orderColumnMap.length + nonKeyInputColumnMap.length
+ streamingEvaluatorNums.length + scratchColumnPositions.length;
this.bufferedColumnMap = new int[bufferedColumnCount];
this.bufferedTypeInfos = new TypeInfo[bufferedColumnCount];
int orderColumnCount = orderColumnMap.length;
int nonKeyInputColumnCount = nonKeyInputColumnMap.length;
int streamingEvaluatorCount = streamingEvaluatorNums.length;
/*
* In scenario 4. order column is not in keyInputColumnMap and is not referred in
* reducerBatchTypeInfos, so we need to figure the TypeInfo out in an alternative way
* (columnVectorTypeToTypeInfo). This is a workaround, maybe a compile-time solution would be
* better.
*/
for (int i = 0; i < orderColumnMap.length; i++) {
final int columnNum = orderColumnMap[i];
bufferedColumnMap[i] = columnNum;
bufferedTypeInfos[i] = reducerBatchTypeInfos.length <= columnNum
? columnVectorTypeToTypeInfo(orderColumnVectorTypes[i]) : reducerBatchTypeInfos[columnNum];
}
for (int i = 0; i < nonKeyInputColumnMap.length; i++) {
final int columnNum = nonKeyInputColumnMap[i];
// ensure offsets in buffered map: [[order cols] [non-key input cols]]
final int bufferedMapIndex = orderColumnCount + i;
bufferedColumnMap[bufferedMapIndex] = columnNum;
bufferedTypeInfos[bufferedMapIndex] = reducerBatchTypeInfos[columnNum];
}
for (int i = 0; i < streamingEvaluatorCount; i++) {
final int streamingEvaluatorNum = streamingEvaluatorNums[i];
// ensure offsets in buffered map: [[order cols] [non-key input cols] [streaming cols]]
final int bufferedMapIndex = orderColumnCount + nonKeyInputColumnCount + i;
bufferedColumnMap[bufferedMapIndex] = outputProjectionColumnMap[streamingEvaluatorNum];
bufferedTypeInfos[bufferedMapIndex] = outputTypeInfos[streamingEvaluatorNum];
}
for (int i = 0; i < scratchColumnPositions.length; i++) {
final int bufferedMapIndex = orderColumnCount + nonKeyInputColumnCount + streamingEvaluatorCount + i;
bufferedColumnMap[bufferedMapIndex] = scratchColumnPositions[i];
bufferedTypeInfos[bufferedMapIndex] = TypeInfoUtils.getTypeInfoFromTypeString(vOutContext.getScratchColumnTypeNames()[i]);
}
}
private void initExpressionColumns() {
for (int i = 0; i < evaluators.length; i++) {
VectorPTFEvaluatorBase evaluator = evaluators[i];
/*
* Non-streaming evaluators work on buffered batches, we need to adapt them. Before PTF
* bounded start vectorization (HIVE-24761), VectorExpression.outputColumnNum was closed and
* VectorExpression.inputColumnNum didn't even exist (even though the vast majority of
* VectorExpression subclasses use at least 1 input column). Since VectorPTFOperator and
* VectorPTFGroupBatches work on modified batches (by not storing all the columns, and having
* ordering columns first), the expressions planned in compile-time won't work with the
* original config (column layout). It would make sense to move this logic to compile time,
* because here in runtime, a very simple mapping (bufferedColumnMap) is used, so it might be
* used. However, vectorized expression compilation affects many layers of code (having
* VectorizationContext as the common scope), and moving the calculation of bufferedColumnMap
* and this override logic to compile-time would create much more complicated behavior there
* (probably involving hacking most of the time, or maybe a great re-design) just because of
* the optimized column layout of the PTF vectorization.
*/
if (!evaluator.streamsResult()) {
evaluator.inputColumnNum = IntStream.range(0, bufferedColumnMap.length)
.filter(j -> bufferedColumnMap[j] == evaluator.inputColumnNum).findFirst()
.orElseGet(() -> evaluator.inputColumnNum);
if (evaluator.inputVecExpr != null) {
for (int j = 0; j < evaluator.inputVecExpr.inputColumnNum.length; j++){
final int jj = j; // need a final in stream filter
evaluator.inputVecExpr.inputColumnNum[jj] = IntStream.range(0, bufferedColumnMap.length)
.filter(k -> bufferedColumnMap[k] == evaluator.inputVecExpr.inputColumnNum[jj])
.findFirst().orElseGet(() -> evaluator.inputVecExpr.inputColumnNum[jj]);
}
evaluator.inputVecExpr.outputColumnNum = IntStream.range(0, bufferedColumnMap.length)
.filter(j -> bufferedColumnMap[j] == evaluator.inputVecExpr.outputColumnNum)
.findFirst().orElseGet(() -> evaluator.inputVecExpr.outputColumnNum);
}
evaluator.mapCustomColumns(bufferedColumnMap);
}
}
}
/**
* Let's say we have a typical ordering scenario where is 1 order column:
* keyInputColumnMap: [0, 1] (-> key cols contain order cols)
* orderColumnMap: [1]
* The method returns the non-ordering key columns: [0]
*
* This is typically needed when while forwarding batches and want to fill
* only partitioning but non-ordering columns from buffered batches to the overflow batch,
* as the buffered batches already contain ordering column which is used for range calculation.
*
* Possible scenarios:
* 1. explicit partitioning and ordering column
* select p_mfgr, p_name, p_retailprice, count(*) over(partition by p_mfgr order by p_date)
* keyInputColumnMap: [0, 1]
* orderColumnMap: [1]
*
* 2a. no explicit ordering col
* select p_mfgr, p_name, p_retailprice, count(*) over(partition by p_mfgr)
* keyInputColumnMap: [0]
* orderColumnMap: [0]
*
* 2b. no explicit ordering col, another column (keyInput is 0th again)
* select p_mfgr, p_name, p_retailprice, count(*) over(partition by p_name)
* keyInputColumnMap: [0]
* orderColumnMap: [0]
*
* 3. no explicit partitioning col (= constant partition expression)
* select p_mfgr, p_name, p_retailprice, count(*) over(order by p_date)
* keyInputColumnMap: [1]
* orderColumnMap: [1]
*
* 4. constant present on the partitioning column
* select p_mfgr, p_name, p_retailprice, count(*) over(partition by p_mfgr)
* from vector_ptf_part_simple_orc where p_mfgr = "Manufacturer#1";
* partitionExpressions: [ConstantVectorExpression(val Manufacturer#1) -> 6:string]
* orderExpressions: [ConstantVectorExpression(val Manufacturer#1) -> 7:string]
* keyInputColumnMap: []
* orderColumnMap: [7]
* @param keyInputColumnMap
* @param orderColumnMap
* @return
*/
private int[] determineKeyColumnsWithoutOrderColumns(int[] keyInputColumnMap,
int[] orderColumnMap) {
List<Integer> keyColumnsWithoutOrderColumns =
IntStream.of(keyInputColumnMap).boxed().collect(Collectors.toList());
List<Integer> orderColumns = IntStream.of(orderColumnMap).boxed().collect(Collectors.toList());
keyColumnsWithoutOrderColumns.removeAll(orderColumns);
return Ints.toArray(keyColumnsWithoutOrderColumns);
}
private static TypeInfo columnVectorTypeToTypeInfo(Type type) {
switch (type) {
case DOUBLE:
return TypeInfoUtils.getTypeInfoFromTypeString(serdeConstants.DOUBLE_TYPE_NAME);
case BYTES:
return TypeInfoUtils.getTypeInfoFromTypeString(serdeConstants.STRING_TYPE_NAME);
case DECIMAL:
return TypeInfoUtils.getTypeInfoFromTypeString(serdeConstants.DECIMAL_TYPE_NAME);
case TIMESTAMP:
return TypeInfoUtils.getTypeInfoFromTypeString(serdeConstants.TIMESTAMP_TYPE_NAME);
case LONG:
return TypeInfoUtils.getTypeInfoFromTypeString(serdeConstants.INT_TYPE_NAME);
default:
throw new RuntimeException("Cannot convert column vector type: '" + type + "' to TypeInfo");
}
}
private Object[] getPartitionKey() {
final int count = partitionColumnMap.length;
Object[] key = new Object[count];
for (int i = 0; i < count; i++) {
if (currentPartitionIsNull[i]) {
key[i] = null;
continue;
}
switch (partitionColumnVectorTypes[i]) {
case LONG:
key[i] = currentPartitionLongs[i];
break;
case DOUBLE:
key[i] = currentPartitionDoubles[i];
break;
case BYTES:
key[i] = new byte[currentPartitionByteLengths[i]];
System.arraycopy(currentPartitionByteArrays[i], 0, key[i], 0,
currentPartitionByteLengths[i]);
break;
case DECIMAL:
key[i] = currentPartitionDecimals[i];
break;
case TIMESTAMP:
key[i] = currentPartitionTimestamps[i];
break;
case INTERVAL_DAY_TIME:
key[i] = currentPartitionIntervalDayTimes[i];
break;
default:
throw new RuntimeException(
"Unexpected column vector type " + partitionColumnVectorTypes[i]);
}
}
return key;
}
private void finishPartition(Object[] partitionKey) throws HiveException {
/*
* Last group batch.
*
* Take the (non-streaming) group aggregation values and write output columns for all
* rows of every batch of the group. As each group batch is finished being written, they are
* forwarded to the next operator.
*/
groupBatches.finishPartition();
groupBatches.fillGroupResultsAndForward(this, partitionKey);
groupBatches.cleanupPartition();
}
private boolean isPartitionChanged(VectorizedRowBatch batch) {
final int count = partitionColumnMap.length;
for (int i = 0; i < count; i++) {
ColumnVector colVector = batch.cols[partitionColumnMap[i]];
// Vector reduce key (i.e. partition) columns are repeated -- so we test element 0.
final boolean isNull = !colVector.noNulls && colVector.isNull[0];
final boolean currentIsNull = currentPartitionIsNull[i];
if (isNull != currentIsNull) {
return true;
}
if (isNull) {
// NULL does equal NULL here.
continue;
}
switch (partitionColumnVectorTypes[i]) {
case LONG:
if (currentPartitionLongs[i] != ((LongColumnVector) colVector).vector[0]) {
return true;
}
break;
case DOUBLE:
if (currentPartitionDoubles[i] != ((DoubleColumnVector) colVector).vector[0]) {
return true;
}
break;
case BYTES:
{
BytesColumnVector byteColVector = (BytesColumnVector) colVector;
byte[] bytes = byteColVector.vector[0];
final int start = byteColVector.start[0];
final int length = byteColVector.length[0];
if (!StringExpr.equal(
bytes, start, length,
currentPartitionByteArrays[i], 0, currentPartitionByteLengths[i])) {
return true;
}
}
break;
case DECIMAL:
if (!currentPartitionDecimals[i].equals(((DecimalColumnVector) colVector).vector[0])) {
return true;
}
break;
case TIMESTAMP:
if (((TimestampColumnVector) colVector).compareTo(0, currentPartitionTimestamps[i]) != 0) {
return true;
}
break;
case INTERVAL_DAY_TIME:
if (((IntervalDayTimeColumnVector) colVector).compareTo(0, currentPartitionIntervalDayTimes[i]) != 0) {
return true;
}
break;
default:
throw new RuntimeException("Unexpected column vector type " + partitionColumnVectorTypes[i]);
}
}
return false;
}
private void setCurrentPartition(VectorizedRowBatch batch) {
final int count = partitionColumnMap.length;
for (int i = 0; i < count; i++) {
ColumnVector colVector = batch.cols[partitionColumnMap[i]];
// Partition columns are repeated -- so we test element 0.
final boolean isNull = !colVector.noNulls && colVector.isNull[0];
currentPartitionIsNull[i] = isNull;
if (isNull) {
continue;
}
switch (partitionColumnVectorTypes[i]) {
case LONG:
currentPartitionLongs[i] = ((LongColumnVector) colVector).vector[0];
break;
case DOUBLE:
currentPartitionDoubles[i] = ((DoubleColumnVector) colVector).vector[0];
break;
case BYTES:
{
BytesColumnVector byteColVector = (BytesColumnVector) colVector;
byte[] bytes = byteColVector.vector[0];
final int start = byteColVector.start[0];
final int length = byteColVector.length[0];
if (currentPartitionByteArrays[i] == null || currentPartitionByteLengths[i] < length) {
currentPartitionByteArrays[i] = Arrays.copyOfRange(bytes, start, start + length);
} else {
System.arraycopy(bytes, start, currentPartitionByteArrays[i], 0, length);
}
currentPartitionByteLengths[i] = length;
}
break;
case DECIMAL:
if (currentPartitionDecimals[i] == null) {
currentPartitionDecimals[i] = new HiveDecimalWritable();
}
currentPartitionDecimals[i].set(((DecimalColumnVector) colVector).vector[0]);
break;
case TIMESTAMP:
if (currentPartitionTimestamps[i] == null) {
currentPartitionTimestamps[i] = new Timestamp(0);
}
((TimestampColumnVector) colVector).timestampUpdate(currentPartitionTimestamps[i], 0);
break;
case INTERVAL_DAY_TIME:
if (currentPartitionIntervalDayTimes[i] == null) {
currentPartitionIntervalDayTimes[i] = new HiveIntervalDayTime();
}
((IntervalDayTimeColumnVector) colVector).intervalDayTimeUpdate(currentPartitionIntervalDayTimes[i], 0);
break;
default:
throw new RuntimeException("Unexpected column vector type " + partitionColumnVectorTypes[i]);
}
}
}
/**
* This package visible method can be called from VectorPTFGroupBatches. The usage of
* vectorForward instead of forward is important in order to count the runtime rows (EXPLAIN
* ANALYZE) correctly.
*
* @param batch
* @throws HiveException
*/
void forwardBatch(VectorizedRowBatch batch) throws HiveException {
super.vectorForward(batch);
}
@Override
protected void closeOp(boolean abort) throws HiveException {
/*
* Why would finishPartition be skipped here?
* 1. abort: obviously
* 2. allEvaluatorsAreStreaming: if all evaluators are streaming, we already evaluated
* 3. isFirstPartition: if it's true, we haven't seen any records/batches in the operator
*/
if (!abort && !allEvaluatorsAreStreaming && !isFirstPartition){
finishPartition(getPartitionKey());
}
super.closeOp(abort);
}
/**
* @return the name of the operator
*/
@Override
public String getName() {
return getOperatorName();
}
static public String getOperatorName() {
return "PTF";
}
@Override
public OperatorType getType() {
return OperatorType.PTF;
}
@Override
public VectorizationContext getOutputVectorizationContext() {
return vOutContext;
}
@Override
public VectorizationContext getInputVectorizationContext() {
return vContext;
}
@Override
public VectorDesc getVectorDesc() {
return vectorDesc;
}
}