80
80
* the underlying database. It should use ANSI SQL and be compatible with common databases
81
81
* such as MySQL (note that MySQL doesn't use full ANSI mode by default), Postgres, etc.
82
82
*
83
- * This class separates out the statistics update part from MetaStoreDirectSql class.
83
+ * This class separates out the update part from MetaStoreDirectSql class.
84
84
*/
85
- class DirectSqlUpdate {
86
- private static final Logger LOG = LoggerFactory .getLogger (DirectSqlUpdate .class .getName ());
87
- PersistenceManager pm ;
88
- Configuration conf ;
89
- DatabaseProduct dbType ;
90
- int maxBatchSize ;
91
- SQLGenerator sqlGenerator ;
85
+ class DirectSqlUpdatePart {
86
+ private static final Logger LOG = LoggerFactory .getLogger (DirectSqlUpdatePart .class .getName ());
87
+
88
+ private final PersistenceManager pm ;
89
+ private final Configuration conf ;
90
+ private final DatabaseProduct dbType ;
91
+ private final int maxBatchSize ;
92
+ private final SQLGenerator sqlGenerator ;
93
+
92
94
private static final ReentrantLock derbyLock = new ReentrantLock (true );
93
-
94
- public DirectSqlUpdate (PersistenceManager pm , Configuration conf ,
95
- DatabaseProduct dbType , int batchSize ) {
95
+
96
+ public DirectSqlUpdatePart (PersistenceManager pm , Configuration conf ,
97
+ DatabaseProduct dbType , int batchSize ) {
96
98
this .pm = pm ;
97
99
this .conf = conf ;
98
100
this .dbType = dbType ;
@@ -362,9 +364,6 @@ private Map<String, Map<String, String>> updatePartitionParamTable(Connection db
362
364
throws SQLException , MetaException {
363
365
Map <String , Map <String , String >> result = new HashMap <>();
364
366
boolean areTxnStatsSupported = MetastoreConf .getBoolVar (conf , ConfVars .HIVE_TXN_STATS_ENABLED );
365
- PreparedStatement statementInsert = null ;
366
- PreparedStatement statementDelete = null ;
367
- PreparedStatement statementUpdate = null ;
368
367
String insert = "INSERT INTO \" PARTITION_PARAMS\" (\" PART_ID\" , \" PARAM_KEY\" , \" PARAM_VALUE\" ) "
369
368
+ "VALUES( ? , 'COLUMN_STATS_ACCURATE' , ? )" ;
370
369
String delete = "DELETE from \" PARTITION_PARAMS\" "
@@ -384,10 +383,9 @@ private Map<String, Map<String, String>> updatePartitionParamTable(Connection db
384
383
// get the old parameters from PARTITION_PARAMS table.
385
384
Map <Long , String > partIdToParaMap = getParamValues (dbConn , partIdList );
386
385
387
- try {
388
- statementInsert = dbConn .prepareStatement (insert );
389
- statementDelete = dbConn .prepareStatement (delete );
390
- statementUpdate = dbConn .prepareStatement (update );
386
+ try (PreparedStatement statementInsert = dbConn .prepareStatement (insert );
387
+ PreparedStatement statementDelete = dbConn .prepareStatement (delete );
388
+ PreparedStatement statementUpdate = dbConn .prepareStatement (update )) {
391
389
for (Map .Entry entry : partitionInfoMap .entrySet ()) {
392
390
PartitionInfo partitionInfo = (PartitionInfo ) entry .getKey ();
393
391
ColumnStatistics colStats = (ColumnStatistics ) entry .getValue ();
@@ -472,83 +470,9 @@ private Map<String, Map<String, String>> updatePartitionParamTable(Connection db
472
470
updateWriteIdForPartitions (dbConn , writeId , partIdList );
473
471
}
474
472
return result ;
475
- } finally {
476
- closeStmt (statementInsert );
477
- closeStmt (statementUpdate );
478
- closeStmt (statementDelete );
479
473
}
480
474
}
481
475
482
- private static class PartitionInfo {
483
- long partitionId ;
484
- long writeId ;
485
- String partitionName ;
486
- public PartitionInfo (long partitionId , long writeId , String partitionName ) {
487
- this .partitionId = partitionId ;
488
- this .writeId = writeId ;
489
- this .partitionName = partitionName ;
490
- }
491
-
492
- @ Override
493
- public int hashCode () {
494
- return (int )partitionId ;
495
- }
496
-
497
- @ Override
498
- public boolean equals (Object o )
499
- {
500
- if (this == o ) {
501
- return true ;
502
- }
503
- if (o == null ) {
504
- return false ;
505
- }
506
- if (!(o instanceof PartitionInfo )) {
507
- return false ;
508
- }
509
- PartitionInfo other = (PartitionInfo )o ;
510
- if (this .partitionId != other .partitionId ) {
511
- return false ;
512
- }
513
- return true ;
514
- }
515
- }
516
-
517
- private static class PartColNameInfo {
518
- long partitionId ;
519
- String colName ;
520
- public PartColNameInfo (long partitionId , String colName ) {
521
- this .partitionId = partitionId ;
522
- this .colName = colName ;
523
- }
524
-
525
- @ Override
526
- public int hashCode () {
527
- return (int )partitionId ;
528
- }
529
-
530
- @ Override
531
- public boolean equals (Object o )
532
- {
533
- if (this == o ) {
534
- return true ;
535
- }
536
- if (o == null ) {
537
- return false ;
538
- }
539
- if (!(o instanceof PartColNameInfo )) {
540
- return false ;
541
- }
542
- PartColNameInfo other = (PartColNameInfo )o ;
543
- if (this .partitionId != other .partitionId ) {
544
- return false ;
545
- }
546
- if (this .colName .equalsIgnoreCase (other .colName )) {
547
- return true ;
548
- }
549
- return false ;
550
- }
551
- }
552
476
553
477
private Map <PartitionInfo , ColumnStatistics > getPartitionInfo (Connection dbConn , long tblId ,
554
478
Map <String , ColumnStatistics > partColStatsMap )
@@ -792,7 +716,7 @@ private void updatePartitionsInBatch(Map<List<String>, Long> partValuesToId,
792
716
List <Partition > newParts ) throws MetaException {
793
717
List <String > columns = Arrays .asList ("\" CREATE_TIME\" " , "\" LAST_ACCESS_TIME\" " , "\" WRITE_ID\" " );
794
718
List <String > conditionKeys = Arrays .asList ("\" PART_ID\" " );
795
- String stmt = dbType .createUpdatePreparedStmt ("\" PARTITIONS\" " , columns , conditionKeys );
719
+ String stmt = TxnUtils .createUpdatePreparedStmt ("\" PARTITIONS\" " , columns , conditionKeys );
796
720
int maxRows = dbType .getMaxRows (maxBatchSize , 4 );
797
721
updateWithStatement (statement -> Batchable .runBatched (maxRows , newParts , new Batchable <Partition , Void >() {
798
722
@ Override
@@ -916,7 +840,7 @@ private void updateParams(String paramTable, String idColumn,
916
840
List <Pair <Long , Pair <String , String >>> updateIdAndParams ) throws MetaException {
917
841
List <String > columns = Arrays .asList ("\" PARAM_VALUE\" " );
918
842
List <String > conditionKeys = Arrays .asList (idColumn , "\" PARAM_KEY\" " );
919
- String stmt = dbType .createUpdatePreparedStmt (paramTable , columns , conditionKeys );
843
+ String stmt = TxnUtils .createUpdatePreparedStmt (paramTable , columns , conditionKeys );
920
844
int maxRows = dbType .getMaxRows (maxBatchSize , 3 );
921
845
updateWithStatement (statement -> Batchable .runBatched (maxRows , updateIdAndParams ,
922
846
new Batchable <Pair <Long , Pair <String , String >>, Object >() {
@@ -938,7 +862,7 @@ public List<Object> run(List<Pair<Long, Pair<String, String>>> input) throws SQL
938
862
private void insertParams (String paramTable , String idColumn ,
939
863
List <Pair <Long , Pair <String , String >>> addIdAndParams ) throws MetaException {
940
864
List <String > columns = Arrays .asList (idColumn , "\" PARAM_KEY\" " , "\" PARAM_VALUE\" " );
941
- String query = dbType .createInsertPreparedStmt (paramTable , columns );
865
+ String query = TxnUtils .createInsertPreparedStmt (paramTable , columns );
942
866
int maxRows = dbType .getMaxRows (maxBatchSize , 3 );
943
867
updateWithStatement (statement -> Batchable .runBatched (maxRows , addIdAndParams ,
944
868
new Batchable <Pair <Long , Pair <String , String >>, Void >() {
@@ -1046,7 +970,7 @@ private void updateSDInBatch(List<Long> ids, Map<Long, StorageDescriptor> idToSd
1046
970
List <String > columns = Arrays .asList ("\" CD_ID\" " , "\" INPUT_FORMAT\" " , "\" IS_COMPRESSED\" " ,
1047
971
"\" IS_STOREDASSUBDIRECTORIES\" " , "\" LOCATION\" " , "\" NUM_BUCKETS\" " , "\" OUTPUT_FORMAT\" " );
1048
972
List <String > conditionKeys = Arrays .asList ("\" SD_ID\" " );
1049
- String stmt = dbType .createUpdatePreparedStmt ("\" SDS\" " , columns , conditionKeys );
973
+ String stmt = TxnUtils .createUpdatePreparedStmt ("\" SDS\" " , columns , conditionKeys );
1050
974
int maxRows = dbType .getMaxRows (maxBatchSize , 8 );
1051
975
updateWithStatement (statement -> Batchable .runBatched (maxRows , ids ,
1052
976
new Batchable <Long , Void >() {
@@ -1083,7 +1007,7 @@ public List<Void> run(List<Long> input) throws MetaException {
1083
1007
}
1084
1008
});
1085
1009
List <String > columns = Arrays .asList ("\" SD_ID\" " , "\" INTEGER_IDX\" " , "\" BUCKET_COL_NAME\" " );
1086
- String stmt = dbType .createInsertPreparedStmt ("\" BUCKETING_COLS\" " , columns );
1010
+ String stmt = TxnUtils .createInsertPreparedStmt ("\" BUCKETING_COLS\" " , columns );
1087
1011
List <Long > idWithBucketCols = filterIdsByNonNullValue (sdIds , sdIdToBucketCols );
1088
1012
int maxRows = dbType .getMaxRows (maxBatchSize , 3 );
1089
1013
updateWithStatement (statement -> Batchable .runBatched (maxRows , idWithBucketCols , new Batchable <Long , Object >() {
@@ -1117,7 +1041,7 @@ public List<Void> run(List<Long> input) throws MetaException {
1117
1041
});
1118
1042
1119
1043
List <String > columns = Arrays .asList ("\" SD_ID\" " , "\" INTEGER_IDX\" " , "\" COLUMN_NAME\" " , "\" ORDER\" " );
1120
- String stmt = dbType .createInsertPreparedStmt ("\" SORT_COLS\" " , columns );
1044
+ String stmt = TxnUtils .createInsertPreparedStmt ("\" SORT_COLS\" " , columns );
1121
1045
List <Long > idWithSortCols = filterIdsByNonNullValue (sdIds , sdIdToSortCols );
1122
1046
int maxRows = dbType .getMaxRows (maxBatchSize , 4 );
1123
1047
updateWithStatement (statement -> Batchable .runBatched (maxRows , idWithSortCols , new Batchable <Long , Object >() {
@@ -1229,7 +1153,7 @@ private Long getDataStoreId(Class<?> modelClass) throws MetaException {
1229
1153
private void insertSkewedColNamesInBatch (Map <Long , List <String >> sdIdToSkewedColNames ,
1230
1154
List <Long > sdIds ) throws MetaException {
1231
1155
List <String > columns = Arrays .asList ("\" SD_ID\" " , "\" INTEGER_IDX\" " , "\" SKEWED_COL_NAME\" " );
1232
- String stmt = dbType .createInsertPreparedStmt ("\" SKEWED_COL_NAMES\" " , columns );
1156
+ String stmt = TxnUtils .createInsertPreparedStmt ("\" SKEWED_COL_NAMES\" " , columns );
1233
1157
List <Long > idWithSkewedCols = filterIdsByNonNullValue (sdIds , sdIdToSkewedColNames );
1234
1158
int maxRows = dbType .getMaxRows (maxBatchSize , 3 );
1235
1159
updateWithStatement (statement -> Batchable .runBatched (maxRows , idWithSkewedCols , new Batchable <Long , Object >() {
@@ -1252,7 +1176,7 @@ public List<Object> run(List<Long> input) throws SQLException {
1252
1176
1253
1177
private void insertStringListInBatch (List <Long > stringListIds ) throws MetaException {
1254
1178
List <String > columns = Arrays .asList ("\" STRING_LIST_ID\" " );
1255
- String insertQuery = dbType .createInsertPreparedStmt ("\" SKEWED_STRING_LIST\" " , columns );
1179
+ String insertQuery = TxnUtils .createInsertPreparedStmt ("\" SKEWED_STRING_LIST\" " , columns );
1256
1180
int maxRows = dbType .getMaxRows (maxBatchSize , 1 );
1257
1181
updateWithStatement (statement -> Batchable .runBatched (maxRows , stringListIds ,
1258
1182
new Batchable <Long , Void >() {
@@ -1272,7 +1196,7 @@ public List<Void> run(List<Long> input) throws SQLException {
1272
1196
private void insertStringListValuesInBatch (Map <Long , List <String >> stringListIdToValues ,
1273
1197
List <Long > stringListIds ) throws MetaException {
1274
1198
List <String > columns = Arrays .asList ("\" STRING_LIST_ID\" " , "\" INTEGER_IDX\" " , "\" STRING_LIST_VALUE\" " );
1275
- String insertQuery = dbType .createInsertPreparedStmt ("\" SKEWED_STRING_LIST_VALUES\" " , columns );
1199
+ String insertQuery = TxnUtils .createInsertPreparedStmt ("\" SKEWED_STRING_LIST_VALUES\" " , columns );
1276
1200
List <Long > idWithStringList = filterIdsByNonNullValue (stringListIds , stringListIdToValues );
1277
1201
int maxRows = dbType .getMaxRows (maxBatchSize , 3 );
1278
1202
updateWithStatement (statement -> Batchable .runBatched (maxRows , idWithStringList ,
@@ -1298,7 +1222,7 @@ public List<Void> run(List<Long> input) throws SQLException {
1298
1222
private void insertSkewedValuesInBatch (Map <Long , List <Long >> sdIdToStringListId ,
1299
1223
List <Long > sdIds ) throws MetaException {
1300
1224
List <String > columns = Arrays .asList ("\" SD_ID_OID\" " , "\" INTEGER_IDX\" " , "\" STRING_LIST_ID_EID\" " );
1301
- String insertQuery = dbType .createInsertPreparedStmt ("\" SKEWED_VALUES\" " , columns );
1225
+ String insertQuery = TxnUtils .createInsertPreparedStmt ("\" SKEWED_VALUES\" " , columns );
1302
1226
List <Long > idWithSkewedValues = filterIdsByNonNullValue (sdIds , sdIdToStringListId );
1303
1227
int maxRows = dbType .getMaxRows (maxBatchSize , 3 );
1304
1228
updateWithStatement (statement -> Batchable .runBatched (maxRows , idWithSkewedValues ,
@@ -1324,7 +1248,7 @@ public List<Void> run(List<Long> input) throws Exception {
1324
1248
private void insertSkewColValueLocInBatch (Map <Long , List <Pair <Long , String >>> sdIdToColValueLoc ,
1325
1249
List <Long > sdIds ) throws MetaException {
1326
1250
List <String > columns = Arrays .asList ("\" SD_ID\" " , "\" STRING_LIST_ID_KID\" " , "\" LOCATION\" " );
1327
- String insertQuery = dbType .createInsertPreparedStmt ("\" SKEWED_COL_VALUE_LOC_MAP\" " , columns );
1251
+ String insertQuery = TxnUtils .createInsertPreparedStmt ("\" SKEWED_COL_VALUE_LOC_MAP\" " , columns );
1328
1252
List <Long > idWithColValueLoc = filterIdsByNonNullValue (sdIds , sdIdToColValueLoc );
1329
1253
int maxRows = dbType .getMaxRows (maxBatchSize , 3 );
1330
1254
updateWithStatement (statement -> Batchable .runBatched (maxRows , idWithColValueLoc ,
@@ -1415,7 +1339,7 @@ public List<Void> run(List<Long> input) throws Exception {
1415
1339
1416
1340
private void insertCDInBatch (List <Long > ids , Map <Long , List <FieldSchema >> idToCols )
1417
1341
throws MetaException {
1418
- String insertCds = dbType .createInsertPreparedStmt ("\" CDS\" " , Arrays .asList ("\" CD_ID\" " ));
1342
+ String insertCds = TxnUtils .createInsertPreparedStmt ("\" CDS\" " , Arrays .asList ("\" CD_ID\" " ));
1419
1343
int maxRows = dbType .getMaxRows (maxBatchSize , 1 );
1420
1344
updateWithStatement (statement -> Batchable .runBatched (maxRows , ids ,
1421
1345
new Batchable <Long , Void >() {
@@ -1432,7 +1356,7 @@ public List<Void> run(List<Long> input) throws SQLException {
1432
1356
1433
1357
List <String > columns = Arrays .asList ("\" CD_ID\" " ,
1434
1358
"\" COMMENT\" " , "\" COLUMN_NAME\" " , "\" TYPE_NAME\" " , "\" INTEGER_IDX\" " );
1435
- String insertColumns = dbType .createInsertPreparedStmt ("\" COLUMNS_V2\" " , columns );
1359
+ String insertColumns = TxnUtils .createInsertPreparedStmt ("\" COLUMNS_V2\" " , columns );
1436
1360
int maxRowsForCDs = dbType .getMaxRows (maxBatchSize , 5 );
1437
1361
updateWithStatement (statement -> Batchable .runBatched (maxRowsForCDs , ids ,
1438
1362
new Batchable <Long , Void >() {
@@ -1463,8 +1387,8 @@ private void updateKeyConstraintsInBatch(Map<Long, Long> oldCdIdToNewCdId,
1463
1387
List <String > parentColumns = Arrays .asList ("\" PARENT_CD_ID\" " , "\" PARENT_INTEGER_IDX\" " );
1464
1388
List <String > childColumns = Arrays .asList ("\" CHILD_CD_ID\" " , "\" CHILD_INTEGER_IDX\" " );
1465
1389
1466
- String updateParent = dbType .createUpdatePreparedStmt (tableName , parentColumns , parentColumns );
1467
- String updateChild = dbType .createUpdatePreparedStmt (tableName , childColumns , childColumns );
1390
+ String updateParent = TxnUtils .createUpdatePreparedStmt (tableName , parentColumns , parentColumns );
1391
+ String updateChild = TxnUtils .createUpdatePreparedStmt (tableName , childColumns , childColumns );
1468
1392
for (String updateStmt : new String []{updateParent , updateChild }) {
1469
1393
int maxRows = dbType .getMaxRows (maxBatchSize , 4 );
1470
1394
updateWithStatement (statement -> Batchable .runBatched (maxRows , oldCdIds ,
@@ -1519,7 +1443,7 @@ private void updateSerdeInBatch(List<Long> ids, Map<Long, SerDeInfo> idToSerde)
1519
1443
// Followed the jdo implement to update only NAME and SLIB of SERDES.
1520
1444
List <String > columns = Arrays .asList ("\" NAME\" " , "\" SLIB\" " );
1521
1445
List <String > condKeys = Arrays .asList ("\" SERDE_ID\" " );
1522
- String updateStmt = dbType .createUpdatePreparedStmt ("\" SERDES\" " , columns , condKeys );
1446
+ String updateStmt = TxnUtils .createUpdatePreparedStmt ("\" SERDES\" " , columns , condKeys );
1523
1447
List <Long > idWithSerde = filterIdsByNonNullValue (ids , idToSerde );
1524
1448
int maxRows = dbType .getMaxRows (maxBatchSize , 3 );
1525
1449
updateWithStatement (statement -> Batchable .runBatched (maxRows , idWithSerde ,
@@ -1538,4 +1462,75 @@ public List<Void> run(List<Long> input) throws SQLException {
1538
1462
}
1539
1463
}), updateStmt );
1540
1464
}
1465
+
1466
+ private static final class PartitionInfo {
1467
+ long partitionId ;
1468
+ long writeId ;
1469
+ String partitionName ;
1470
+ public PartitionInfo (long partitionId , long writeId , String partitionName ) {
1471
+ this .partitionId = partitionId ;
1472
+ this .writeId = writeId ;
1473
+ this .partitionName = partitionName ;
1474
+ }
1475
+
1476
+ @ Override
1477
+ public int hashCode () {
1478
+ return (int )partitionId ;
1479
+ }
1480
+
1481
+ @ Override
1482
+ public boolean equals (Object o )
1483
+ {
1484
+ if (this == o ) {
1485
+ return true ;
1486
+ }
1487
+ if (o == null ) {
1488
+ return false ;
1489
+ }
1490
+ if (!(o instanceof PartitionInfo )) {
1491
+ return false ;
1492
+ }
1493
+ PartitionInfo other = (PartitionInfo )o ;
1494
+ if (this .partitionId != other .partitionId ) {
1495
+ return false ;
1496
+ }
1497
+ return true ;
1498
+ }
1499
+ }
1500
+
1501
+ private static final class PartColNameInfo {
1502
+ long partitionId ;
1503
+ String colName ;
1504
+ public PartColNameInfo (long partitionId , String colName ) {
1505
+ this .partitionId = partitionId ;
1506
+ this .colName = colName ;
1507
+ }
1508
+
1509
+ @ Override
1510
+ public int hashCode () {
1511
+ return (int )partitionId ;
1512
+ }
1513
+
1514
+ @ Override
1515
+ public boolean equals (Object o )
1516
+ {
1517
+ if (this == o ) {
1518
+ return true ;
1519
+ }
1520
+ if (o == null ) {
1521
+ return false ;
1522
+ }
1523
+ if (!(o instanceof PartColNameInfo )) {
1524
+ return false ;
1525
+ }
1526
+ PartColNameInfo other = (PartColNameInfo )o ;
1527
+ if (this .partitionId != other .partitionId ) {
1528
+ return false ;
1529
+ }
1530
+ if (this .colName .equalsIgnoreCase (other .colName )) {
1531
+ return true ;
1532
+ }
1533
+ return false ;
1534
+ }
1535
+ }
1541
1536
}
0 commit comments