/
RelOptHiveTable.java
775 lines (707 loc) · 31 KB
/
RelOptHiveTable.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
/*
* 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.optimizer.calcite;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import org.apache.calcite.linq4j.tree.Expression;
import org.apache.calcite.plan.RelOptSchema;
import org.apache.calcite.plan.RelOptTable;
import org.apache.calcite.plan.RelOptUtil.InputFinder;
import org.apache.calcite.prepare.RelOptTableImpl;
import org.apache.calcite.rel.RelCollation;
import org.apache.calcite.rel.RelCollationTraitDef;
import org.apache.calcite.rel.RelDistribution;
import org.apache.calcite.rel.RelFieldCollation;
import org.apache.calcite.rel.RelFieldCollation.Direction;
import org.apache.calcite.rel.RelFieldCollation.NullDirection;
import org.apache.calcite.rel.RelNode;
import org.apache.calcite.rel.RelReferentialConstraint;
import org.apache.calcite.rel.RelReferentialConstraintImpl;
import org.apache.calcite.rel.logical.LogicalTableScan;
import org.apache.calcite.rel.type.RelDataType;
import org.apache.calcite.rel.type.RelDataTypeFactory;
import org.apache.calcite.rel.type.RelDataTypeField;
import org.apache.calcite.rex.RexNode;
import org.apache.calcite.schema.ColumnStrategy;
import org.apache.calcite.util.ImmutableBitSet;
import org.apache.calcite.util.Pair;
import org.apache.calcite.util.mapping.IntPair;
import org.apache.hadoop.hive.conf.HiveConf;
import org.apache.hadoop.hive.metastore.api.FieldSchema;
import org.apache.hadoop.hive.metastore.api.Order;
import org.apache.hadoop.hive.metastore.Warehouse;
import org.apache.hadoop.hive.ql.exec.ColumnInfo;
import org.apache.hadoop.hive.ql.metadata.ForeignKeyInfo;
import org.apache.hadoop.hive.ql.metadata.ForeignKeyInfo.ForeignKeyCol;
import org.apache.hadoop.hive.ql.metadata.Hive;
import org.apache.hadoop.hive.ql.metadata.HiveException;
import org.apache.hadoop.hive.ql.metadata.PartitionIterable;
import org.apache.hadoop.hive.ql.metadata.PrimaryKeyInfo;
import org.apache.hadoop.hive.ql.metadata.Table;
import org.apache.hadoop.hive.ql.metadata.UniqueConstraint;
import org.apache.hadoop.hive.ql.metadata.UniqueConstraint.UniqueConstraintCol;
import org.apache.hadoop.hive.ql.metadata.VirtualColumn;
import org.apache.hadoop.hive.ql.optimizer.calcite.translator.ExprNodeConverter;
import org.apache.hadoop.hive.ql.optimizer.ppr.PartitionPruner;
import org.apache.hadoop.hive.ql.parse.ColumnStatsList;
import org.apache.hadoop.hive.ql.parse.PrunedPartitionList;
import org.apache.hadoop.hive.ql.plan.ColStatistics;
import org.apache.hadoop.hive.ql.plan.ExprNodeDesc;
import org.apache.hadoop.hive.ql.plan.Statistics;
import org.apache.hadoop.hive.ql.plan.Statistics.State;
import org.apache.hadoop.hive.ql.session.SessionState;
import org.apache.hadoop.hive.ql.session.SessionState.LogHelper;
import org.apache.hadoop.hive.ql.stats.StatsUtils;
import org.apache.hadoop.hive.ql.util.DirectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
public class RelOptHiveTable implements RelOptTable {
//~ Instance fields --------------------------------------------------------
private final RelOptSchema schema;
private final RelDataTypeFactory typeFactory;
private final RelDataType rowType;
private final List<String> qualifiedTblName;
private final String name;
private final Table hiveTblMetadata;
private final ImmutableList<ColumnInfo> hiveNonPartitionCols;
private final ImmutableList<ColumnInfo> hivePartitionCols;
private final Map<Integer, ColStatistics> hiveColStatsMap;
private final ImmutableMap<Integer, ColumnInfo> hiveNonPartitionColsMap;
private final ImmutableMap<Integer, ColumnInfo> hivePartitionColsMap;
private final ImmutableList<VirtualColumn> hiveVirtualCols;
private final int noOfNonVirtualCols;
private final List<ImmutableBitSet> keys;
private final List<ImmutableBitSet> nonNullablekeys;
private final List<RelReferentialConstraint> referentialConstraints;
private final HiveConf hiveConf;
private final Hive db;
private final Map<String, Table> tablesCache;
private final Map<String, PrunedPartitionList> partitionCache;
private final Map<String, ColumnStatsList> colStatsCache;
private final AtomicInteger noColsMissingStats;
private double rowCount = -1;
PrunedPartitionList partitionList;
protected static final Logger LOG = LoggerFactory.getLogger(RelOptHiveTable.class.getName());
public RelOptHiveTable(RelOptSchema calciteSchema, RelDataTypeFactory typeFactory, List<String> qualifiedTblName,
RelDataType rowType, Table hiveTblMetadata, List<ColumnInfo> hiveNonPartitionCols, List<ColumnInfo> hivePartitionCols,
List<VirtualColumn> hiveVirtualCols, HiveConf hconf, Hive db, Map<String, Table> tabNameToTabObject,
Map<String, PrunedPartitionList> partitionCache, Map<String, ColumnStatsList> colStatsCache,
AtomicInteger noColsMissingStats) {
this.schema = calciteSchema;
this.typeFactory = typeFactory;
this.qualifiedTblName = ImmutableList.copyOf(qualifiedTblName);
this.name = this.qualifiedTblName.stream().collect(Collectors.joining("."));
this.rowType = rowType;
this.hiveTblMetadata = hiveTblMetadata;
this.hiveColStatsMap = new HashMap<>();
this.hiveNonPartitionCols = ImmutableList.copyOf(hiveNonPartitionCols);
this.hiveNonPartitionColsMap = HiveCalciteUtil.getColInfoMap(hiveNonPartitionCols, 0);
this.hivePartitionCols = ImmutableList.copyOf(hivePartitionCols);
this.hivePartitionColsMap = HiveCalciteUtil.getColInfoMap(hivePartitionCols, hiveNonPartitionColsMap.size());
this.noOfNonVirtualCols = hiveNonPartitionCols.size() + hivePartitionCols.size();
this.hiveVirtualCols = ImmutableList.copyOf(hiveVirtualCols);
this.hiveConf = hconf;
this.db = db;
this.tablesCache = tabNameToTabObject;
this.partitionCache = partitionCache;
this.colStatsCache = colStatsCache;
this.noColsMissingStats = noColsMissingStats;
Pair<List<ImmutableBitSet>, List<ImmutableBitSet>> constraintKeys = generateKeys();
this.keys = constraintKeys.left;
this.nonNullablekeys = constraintKeys.right;
this.referentialConstraints = generateReferentialConstraints();
}
//~ Methods ----------------------------------------------------------------
public String getName() {
return name;
}
@Override
public List<String> getQualifiedName() {
return qualifiedTblName;
}
@Override
public RelDataType getRowType() {
return rowType;
}
@Override
public RelOptSchema getRelOptSchema() {
return schema;
}
public RelDataTypeFactory getTypeFactory() {
return typeFactory;
}
@Override
public Expression getExpression(Class clazz) {
throw new UnsupportedOperationException();
}
public List<ImmutableBitSet> getNonNullableKeys() {
return nonNullablekeys;
}
@Override
public RelOptTable extend(List<RelDataTypeField> extendedFields) {
throw new UnsupportedOperationException();
}
@Override
public List<ColumnStrategy> getColumnStrategies() {
return RelOptTableImpl.columnStrategies(this);
}
public RelOptHiveTable copy(RelDataType newRowType) {
// 1. Build map of column name to col index of original schema
// Assumption: Hive Table can not contain duplicate column names
Map<String, Integer> nameToColIndxMap = new HashMap<String, Integer>();
for (RelDataTypeField f : this.rowType.getFieldList()) {
nameToColIndxMap.put(f.getName(), f.getIndex());
}
// 2. Build nonPart/Part/Virtual column info for new RowSchema
List<ColumnInfo> newHiveNonPartitionCols = new ArrayList<ColumnInfo>();
List<ColumnInfo> newHivePartitionCols = new ArrayList<ColumnInfo>();
List<VirtualColumn> newHiveVirtualCols = new ArrayList<VirtualColumn>();
Map<Integer, VirtualColumn> virtualColInfoMap = HiveCalciteUtil.getVColsMap(this.hiveVirtualCols,
this.noOfNonVirtualCols);
Integer originalColIndx;
ColumnInfo cInfo;
VirtualColumn vc;
for (RelDataTypeField f : newRowType.getFieldList()) {
originalColIndx = nameToColIndxMap.get(f.getName());
if ((cInfo = hiveNonPartitionColsMap.get(originalColIndx)) != null) {
newHiveNonPartitionCols.add(new ColumnInfo(cInfo));
} else if ((cInfo = hivePartitionColsMap.get(originalColIndx)) != null) {
newHivePartitionCols.add(new ColumnInfo(cInfo));
} else if ((vc = virtualColInfoMap.get(originalColIndx)) != null) {
newHiveVirtualCols.add(vc);
} else {
throw new RuntimeException("Copy encountered a column not seen in original TS");
}
}
// 3. Build new Table
return new RelOptHiveTable(this.schema, this.typeFactory, this.qualifiedTblName, newRowType,
this.hiveTblMetadata, newHiveNonPartitionCols, newHivePartitionCols, newHiveVirtualCols,
this.hiveConf, this.db, this.tablesCache, this.partitionCache, this.colStatsCache,
this.noColsMissingStats);
}
// Given a key this method returns true if all of the columns in the key are not nullable
public boolean isNonNullableKey(ImmutableBitSet columns) {
for (ImmutableBitSet key : nonNullablekeys) {
if (columns.contains(key)) {
return true;
}
}
return false;
}
@Override
public boolean isKey(ImmutableBitSet columns) {
for (ImmutableBitSet key : keys) {
if (columns.contains(key)) {
return true;
}
}
return false;
}
@Override
public List<RelReferentialConstraint> getReferentialConstraints() {
return referentialConstraints;
}
private Pair<List<ImmutableBitSet>, List<ImmutableBitSet>> generateKeys() {
final PrimaryKeyInfo primaryKeyInfo = hiveTblMetadata.getPrimaryKeyInfo();
final UniqueConstraint uniqueKeyInfo = hiveTblMetadata.getUniqueKeyInfo();
ImmutableList.Builder<ImmutableBitSet> builder = ImmutableList.builder();
ImmutableList.Builder<ImmutableBitSet> nonNullbuilder = ImmutableList.builder();
// First PK
if (primaryKeyInfo != null && !primaryKeyInfo.getColNames().isEmpty()) {
ImmutableBitSet.Builder keys = ImmutableBitSet.builder();
for (String pkColName : primaryKeyInfo.getColNames().values()) {
int pkPos;
for (pkPos = 0; pkPos < rowType.getFieldNames().size(); pkPos++) {
String colName = rowType.getFieldNames().get(pkPos);
if (pkColName.equals(colName)) {
break;
}
}
if (pkPos == rowType.getFieldNames().size()) {
LOG.error("Column for primary key definition " + pkColName + " not found");
}
keys.set(pkPos);
}
ImmutableBitSet key = keys.build();
builder.add(key);
nonNullbuilder.add(key);
}
// Then UKs
if (uniqueKeyInfo != null && !uniqueKeyInfo.getUniqueConstraints().isEmpty()) {
for (List<UniqueConstraintCol> ukCols : uniqueKeyInfo.getUniqueConstraints().values()) {
ImmutableBitSet.Builder keys = ImmutableBitSet.builder();
boolean isNonNullable = true;
for (UniqueConstraintCol ukCol : ukCols) {
int ukPos;
for (ukPos = 0; ukPos < rowType.getFieldNames().size(); ukPos++) {
String colName = rowType.getFieldNames().get(ukPos);
if (ukCol.colName.equals(colName)) {
if (rowType.getFieldList().get(ukPos).getType().isNullable()) {
// they should all be nullable
isNonNullable = false;
}
break;
}
}
if (ukPos == rowType.getFieldNames().size()) {
LOG.error("Column for unique constraint definition " + ukCol.colName + " not found");
}
keys.set(ukPos);
}
ImmutableBitSet key = keys.build();
builder.add(key);
if (isNonNullable) {
nonNullbuilder.add(key);
}
}
}
return new Pair<>(builder.build(), nonNullbuilder.build());
}
private List<RelReferentialConstraint> generateReferentialConstraints() {
final ForeignKeyInfo foreignKeyInfo = hiveTblMetadata.getForeignKeyInfo();
ImmutableList.Builder<RelReferentialConstraint> builder = ImmutableList.builder();
if (foreignKeyInfo != null && !foreignKeyInfo.getForeignKeys().isEmpty()) {
for (List<ForeignKeyCol> fkCols : foreignKeyInfo.getForeignKeys().values()) {
String parentDatabaseName = fkCols.get(0).parentDatabaseName;
String parentTableName = fkCols.get(0).parentTableName;
String qualifiedName;
List<String> parentTableQualifiedName = new ArrayList<>();
if (parentDatabaseName != null && !parentDatabaseName.isEmpty()) {
parentTableQualifiedName.add(parentDatabaseName);
parentTableQualifiedName.add(parentTableName);
qualifiedName = Warehouse.getQualifiedName(
parentDatabaseName, parentTableName);
} else {
parentTableQualifiedName.add(parentTableName);
qualifiedName = parentTableName;
}
Table parentTab = getTable(qualifiedName);
if (parentTab == null) {
LOG.error("Table for primary key not found: "
+ "databaseName: " + parentDatabaseName + ", "
+ "tableName: " + parentTableName);
return ImmutableList.of();
}
ImmutableList.Builder<IntPair> keys = ImmutableList.builder();
for (ForeignKeyCol fkCol : fkCols) {
int fkPos;
for (fkPos = 0; fkPos < rowType.getFieldNames().size(); fkPos++) {
String fkColName = rowType.getFieldNames().get(fkPos);
if (fkColName.equals(fkCol.childColName)) {
break;
}
}
int pkPos;
for (pkPos = 0; pkPos < parentTab.getAllCols().size(); pkPos++) {
String pkColName = parentTab.getAllCols().get(pkPos).getName();
if (pkColName.equals(fkCol.parentColName)) {
break;
}
}
if (fkPos == rowType.getFieldNames().size()
|| pkPos == parentTab.getAllCols().size()) {
LOG.error("Column for foreign key definition " + fkCol + " not found");
return ImmutableList.of();
}
keys.add(IntPair.of(fkPos, pkPos));
}
builder.add(RelReferentialConstraintImpl.of(qualifiedTblName,
parentTableQualifiedName, keys.build()));
}
}
return builder.build();
}
private Table getTable(String tableName) {
if (!tablesCache.containsKey(tableName)) {
try {
Table table = db.getTable(tableName);
if (table != null) {
tablesCache.put(tableName, table);
}
return table;
} catch (HiveException e) {
throw new RuntimeException(e);
}
}
return tablesCache.get(tableName);
}
@Override
public RelNode toRel(ToRelContext context) {
return new LogicalTableScan(context.getCluster(), this);
}
@Override
public <T> T unwrap(Class<T> arg0) {
return arg0.isInstance(this) ? arg0.cast(this) : null;
}
@Override
public List<RelCollation> getCollationList() {
ImmutableList.Builder<RelFieldCollation> collationList = new ImmutableList.Builder<RelFieldCollation>();
for (Order sortColumn : this.hiveTblMetadata.getSortCols()) {
for (int i=0; i<this.hiveTblMetadata.getSd().getCols().size(); i++) {
FieldSchema field = this.hiveTblMetadata.getSd().getCols().get(i);
if (field.getName().equals(sortColumn.getCol())) {
Direction direction = DirectionUtils.codeToDirection(sortColumn.getOrder());
NullDirection nullDirection = sortColumn.getOrder() == DirectionUtils.ASCENDING_CODE ?
NullDirection.FIRST : NullDirection.LAST;
collationList.add(new RelFieldCollation(i, direction, nullDirection));
break;
}
}
}
return new ImmutableList.Builder<RelCollation>()
.add(RelCollationTraitDef.INSTANCE.canonize(
new HiveRelCollation(collationList.build())))
.build();
}
@Override
public RelDistribution getDistribution() {
ImmutableList.Builder<Integer> columnPositions = new ImmutableList.Builder<Integer>();
for (String bucketColumn : this.hiveTblMetadata.getBucketCols()) {
for (int i=0; i<this.hiveTblMetadata.getSd().getCols().size(); i++) {
FieldSchema field = this.hiveTblMetadata.getSd().getCols().get(i);
if (field.getName().equals(bucketColumn)) {
columnPositions.add(i);
break;
}
}
}
return new HiveRelDistribution(RelDistribution.Type.HASH_DISTRIBUTED,
columnPositions.build());
}
@Override
public double getRowCount() {
if (rowCount == -1) {
if (null == partitionList) {
// we are here either unpartitioned table or partitioned table with no
// predicates
computePartitionList(hiveConf, null, new HashSet<Integer>());
}
rowCount = StatsUtils.getNumRows(hiveConf, getNonPartColumns(), hiveTblMetadata,
partitionList, noColsMissingStats);
}
return rowCount;
}
public Table getHiveTableMD() {
return hiveTblMetadata;
}
private String getColNamesForLogging(Set<String> colLst) {
StringBuilder sb = new StringBuilder();
boolean firstEntry = true;
for (String colName : colLst) {
if (firstEntry) {
sb.append(colName);
firstEntry = false;
} else {
sb.append(", " + colName);
}
}
return sb.toString();
}
public void computePartitionList(HiveConf conf, RexNode pruneNode, Set<Integer> partOrVirtualCols) {
try {
if (!hiveTblMetadata.isPartitioned() || pruneNode == null
|| InputFinder.bits(pruneNode).length() == 0) {
// there is no predicate on partitioning column, we need all partitions
// in this case.
partitionList = PartitionPruner.prune(hiveTblMetadata, null, conf, getName(),
partitionCache);
return;
}
// We have valid pruning expressions, only retrieve qualifying partitions
ExprNodeDesc pruneExpr = pruneNode.accept(new ExprNodeConverter(getName(), getRowType(),
partOrVirtualCols, getTypeFactory()));
partitionList = PartitionPruner.prune(hiveTblMetadata, pruneExpr, conf, getName(),
partitionCache);
} catch (HiveException he) {
throw new RuntimeException(he);
}
}
private void updateColStats(Set<Integer> projIndxLst, boolean allowMissingStats) {
List<String> nonPartColNamesThatRqrStats = new ArrayList<String>();
List<Integer> nonPartColIndxsThatRqrStats = new ArrayList<Integer>();
List<String> partColNamesThatRqrStats = new ArrayList<String>();
List<Integer> partColIndxsThatRqrStats = new ArrayList<Integer>();
Set<String> colNamesFailedStats = new HashSet<String>();
// 1. Separate required columns to Non Partition and Partition Cols
ColumnInfo tmp;
for (Integer pi : projIndxLst) {
if (hiveColStatsMap.get(pi) == null) {
if ((tmp = hiveNonPartitionColsMap.get(pi)) != null) {
nonPartColNamesThatRqrStats.add(tmp.getInternalName());
nonPartColIndxsThatRqrStats.add(pi);
} else if ((tmp = hivePartitionColsMap.get(pi)) != null) {
partColNamesThatRqrStats.add(tmp.getInternalName());
partColIndxsThatRqrStats.add(pi);
} else {
noColsMissingStats.getAndIncrement();
String logMsg = "Unable to find Column Index: " + pi + ", in "
+ hiveTblMetadata.getCompleteName();
LOG.error(logMsg);
throw new RuntimeException(logMsg);
}
}
}
if (null == partitionList) {
// We could be here either because its an unpartitioned table or because
// there are no pruning predicates on a partitioned table.
computePartitionList(hiveConf, null, new HashSet<Integer>());
}
ColumnStatsList colStatsCached = colStatsCache.get(partitionList.getKey());
if (colStatsCached == null) {
colStatsCached = new ColumnStatsList();
colStatsCache.put(partitionList.getKey(), colStatsCached);
}
// 2. Obtain Col Stats for Non Partition Cols
if (nonPartColNamesThatRqrStats.size() > 0) {
List<ColStatistics> hiveColStats = new ArrayList<ColStatistics>();
if (!hiveTblMetadata.isPartitioned()) {
// 2.1 Handle the case for unpartitioned table.
try {
Statistics stats = StatsUtils.collectStatistics(hiveConf, null,
hiveTblMetadata, hiveNonPartitionCols, nonPartColNamesThatRqrStats,
colStatsCached, nonPartColNamesThatRqrStats, true);
rowCount = stats.getNumRows();
for (String c : nonPartColNamesThatRqrStats) {
ColStatistics cs = stats.getColumnStatisticsFromColName(c);
if (cs != null) {
hiveColStats.add(cs);
}
}
colStatsCached.updateState(stats.getColumnStatsState());
// 2.1.1 Record Column Names that we needed stats for but couldn't
if (hiveColStats.isEmpty()) {
colNamesFailedStats.addAll(nonPartColNamesThatRqrStats);
} else if (hiveColStats.size() != nonPartColNamesThatRqrStats.size()) {
Set<String> setOfFiledCols = new HashSet<String>(nonPartColNamesThatRqrStats);
Set<String> setOfObtainedColStats = new HashSet<String>();
for (ColStatistics cs : hiveColStats) {
setOfObtainedColStats.add(cs.getColumnName());
}
setOfFiledCols.removeAll(setOfObtainedColStats);
colNamesFailedStats.addAll(setOfFiledCols);
} else {
// Column stats in hiveColStats might not be in the same order as the columns in
// nonPartColNamesThatRqrStats. reorder hiveColStats so we can build hiveColStatsMap
// using nonPartColIndxsThatRqrStats as below
Map<String, ColStatistics> columnStatsMap =
new HashMap<String, ColStatistics>(hiveColStats.size());
for (ColStatistics cs : hiveColStats) {
columnStatsMap.put(cs.getColumnName(), cs);
// even though the stats were estimated we need to warn user that
// stats are not available
if(cs.isEstimated()) {
colNamesFailedStats.add(cs.getColumnName());
}
}
hiveColStats.clear();
for (String colName : nonPartColNamesThatRqrStats) {
hiveColStats.add(columnStatsMap.get(colName));
}
}
} catch (HiveException e) {
String logMsg = "Collecting stats for table: " + hiveTblMetadata.getTableName() + " failed.";
LOG.error(logMsg, e);
throw new RuntimeException(logMsg, e);
}
} else {
// 2.2 Obtain col stats for partitioned table.
try {
if (partitionList.getNotDeniedPartns().isEmpty()) {
// no need to make a metastore call
rowCount = 0;
hiveColStats = new ArrayList<ColStatistics>();
for (int i = 0; i < nonPartColNamesThatRqrStats.size(); i++) {
// add empty stats object for each column
hiveColStats.add(
new ColStatistics(
nonPartColNamesThatRqrStats.get(i),
hiveNonPartitionColsMap.get(nonPartColIndxsThatRqrStats.get(i)).getTypeName()));
}
colNamesFailedStats.clear();
colStatsCached.updateState(State.COMPLETE);
} else {
Statistics stats = StatsUtils.collectStatistics(hiveConf, partitionList,
hiveTblMetadata, hiveNonPartitionCols, nonPartColNamesThatRqrStats, colStatsCached,
nonPartColNamesThatRqrStats, true);
rowCount = stats.getNumRows();
hiveColStats = new ArrayList<ColStatistics>();
for (String c : nonPartColNamesThatRqrStats) {
ColStatistics cs = stats.getColumnStatisticsFromColName(c);
if (cs != null) {
hiveColStats.add(cs);
if(cs.isEstimated()) {
colNamesFailedStats.add(c);
}
} else {
colNamesFailedStats.add(c);
}
}
colStatsCached.updateState(stats.getColumnStatsState());
}
} catch (HiveException e) {
String logMsg = "Collecting stats failed.";
LOG.error(logMsg, e);
throw new RuntimeException(logMsg, e);
}
}
if (hiveColStats != null && hiveColStats.size() == nonPartColNamesThatRqrStats.size()) {
for (int i = 0; i < hiveColStats.size(); i++) {
// the columns in nonPartColIndxsThatRqrStats/nonPartColNamesThatRqrStats/hiveColStats
// are in same order
hiveColStatsMap.put(nonPartColIndxsThatRqrStats.get(i), hiveColStats.get(i));
colStatsCached.put(hiveColStats.get(i).getColumnName(), hiveColStats.get(i));
if (LOG.isDebugEnabled()) {
LOG.debug("Stats for column " + hiveColStats.get(i).getColumnName() +
" in table " + hiveTblMetadata.getTableName() + " stored in cache");
LOG.debug(hiveColStats.get(i).toString());
}
}
}
}
// 3. Obtain Stats for Partition Cols
if (colNamesFailedStats.isEmpty() && !partColNamesThatRqrStats.isEmpty()) {
ColStatistics cStats = null;
for (int i = 0; i < partColNamesThatRqrStats.size(); i++) {
cStats = StatsUtils.getColStatsForPartCol(hivePartitionColsMap.get(partColIndxsThatRqrStats.get(i)),
new PartitionIterable(partitionList.getNotDeniedPartns()), hiveConf);
hiveColStatsMap.put(partColIndxsThatRqrStats.get(i), cStats);
colStatsCached.put(cStats.getColumnName(), cStats);
if (LOG.isDebugEnabled()) {
LOG.debug("Stats for column " + cStats.getColumnName() +
" in table " + hiveTblMetadata.getTableName() + " stored in cache");
LOG.debug(cStats.toString());
}
}
}
// 4. Warn user if we could get stats for required columns
if (!colNamesFailedStats.isEmpty()) {
String logMsg = "No Stats for " + hiveTblMetadata.getCompleteName() + ", Columns: "
+ getColNamesForLogging(colNamesFailedStats);
noColsMissingStats.getAndAdd(colNamesFailedStats.size());
if (allowMissingStats) {
LOG.warn(logMsg);
HiveConf conf = SessionState.getSessionConf();
if (HiveConf.getBoolVar(conf, HiveConf.ConfVars.HIVE_CBO_SHOW_WARNINGS)) {
LogHelper console = SessionState.getConsole();
console.printInfo(logMsg);
}
} else {
LOG.error(logMsg);
throw new RuntimeException(logMsg);
}
}
}
public List<ColStatistics> getColStat(List<Integer> projIndxLst) {
// If we allow estimated stats for the columns, then we shall set the boolean to true,
// since otherwise we will throw an exception because columns with estimated stats are
// actually added to the list of columns that do not contain stats.
return getColStat(projIndxLst, HiveConf.getBoolVar(hiveConf, HiveConf.ConfVars.HIVE_STATS_ESTIMATE_STATS));
}
/** Note: DOES NOT CHECK txn stats. */
public List<ColStatistics> getColStat(List<Integer> projIndxLst, boolean allowMissingStats) {
List<ColStatistics> colStatsBldr = Lists.newArrayList();
Set<Integer> projIndxSet = new HashSet<Integer>(projIndxLst);
if (projIndxLst != null) {
for (Integer i : projIndxLst) {
if (i >= noOfNonVirtualCols) {
projIndxSet.remove(i);
} else if (hiveColStatsMap.get(i) != null) {
colStatsBldr.add(hiveColStatsMap.get(i));
projIndxSet.remove(i);
}
}
if (!projIndxSet.isEmpty()) {
LOG.info("Calculating column statistics for {}, projIndxSet was not empty: {}, allowMissingStats: {}", name,
projIndxLst, allowMissingStats);
updateColStats(projIndxSet, allowMissingStats);
for (Integer i : projIndxSet) {
colStatsBldr.add(hiveColStatsMap.get(i));
}
}
} else {
List<Integer> pILst = new ArrayList<Integer>();
for (Integer i = 0; i < noOfNonVirtualCols; i++) {
if (hiveColStatsMap.get(i) == null) {
pILst.add(i);
}
}
if (!pILst.isEmpty()) {
LOG.info("Calculating column statistics for {}, projIndxSet was empty, calculated: {}, allowMissingStats: {}",
name, pILst, allowMissingStats);
updateColStats(new HashSet<Integer>(pILst), allowMissingStats);
for (Integer pi : pILst) {
colStatsBldr.add(hiveColStatsMap.get(pi));
}
}
}
return colStatsBldr;
}
/*
* use to check if a set of columns are all partition columns. true only if: -
* all columns in BitSet are partition columns.
*/
public boolean containsPartitionColumnsOnly(ImmutableBitSet cols) {
for (int i = cols.nextSetBit(0); i >= 0; i++, i = cols.nextSetBit(i + 1)) {
if (!hivePartitionColsMap.containsKey(i)) {
return false;
}
}
return true;
}
public List<VirtualColumn> getVirtualCols() {
return this.hiveVirtualCols;
}
public List<ColumnInfo> getPartColumns() {
return this.hivePartitionCols;
}
public List<ColumnInfo> getNonPartColumns() {
return this.hiveNonPartitionCols;
}
public int getNoOfNonVirtualCols() {
return noOfNonVirtualCols;
}
public Map<Integer, ColumnInfo> getPartColInfoMap() {
return hivePartitionColsMap;
}
public Map<Integer, ColumnInfo> getNonPartColInfoMap() {
return hiveNonPartitionColsMap;
}
@Override
public boolean equals(Object obj) {
return obj instanceof RelOptHiveTable
&& this.rowType.equals(((RelOptHiveTable) obj).getRowType())
&& this.getHiveTableMD().equals(((RelOptHiveTable) obj).getHiveTableMD());
}
@Override
public int hashCode() {
return (this.getHiveTableMD() == null)
? super.hashCode() : this.getHiveTableMD().hashCode();
}
public String getPartitionListKey() {
return partitionList != null ? partitionList.getKey() : null;
}
}