Skip to content

Commit b8068f3

Browse files
author
wecharyu
committed
HIVE-28677: Implement direct sql for delete table/partition column stats
1 parent 20d26ad commit b8068f3

File tree

3 files changed

+128
-25
lines changed

3 files changed

+128
-25
lines changed

standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/MetaStoreDirectSql.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3241,6 +3241,46 @@ public void deleteColumnStatsState(long tbl_id) throws MetaException {
32413241
}
32423242
}
32433243

3244+
public boolean deleteTableColumnStatistics(long tableId, String colName, String engine) {
3245+
String deleteSql = "delete from " + TAB_COL_STATS + " where \"TBL_ID\" = " + tableId;
3246+
if (colName != null) {
3247+
deleteSql += " and \"COLUMN_NAME\" = '" + colName + "'";
3248+
}
3249+
if (engine != null) {
3250+
deleteSql += " and \"ENGINE\" = '" + engine + "'";
3251+
}
3252+
try {
3253+
executeNoResult(deleteSql);
3254+
} catch (SQLException e) {
3255+
LOG.warn("Error removing table column stats. ", e);
3256+
return false;
3257+
}
3258+
return true;
3259+
}
3260+
3261+
public boolean deletePartitionColumnStats(String catName, String dbName, String tblName,
3262+
String partName, String colName, String engine) throws MetaException {
3263+
String sqlFilter = "" + PARTITIONS + ".\"PART_NAME\" = ? ";
3264+
List<Long> partitionIds = getPartitionIdsViaSqlFilter(catName, dbName, tblName, sqlFilter,
3265+
Arrays.asList(partName), Collections.emptyList(), -1);
3266+
assert(partitionIds.size() == 1);
3267+
3268+
String deleteSql = "delete from " + PART_COL_STATS + " where \"PART_ID\" = " + partitionIds.get(0);
3269+
if (colName != null) {
3270+
deleteSql += " and \"COLUMN_NAME\" = '" + colName + "'";
3271+
}
3272+
if (engine != null) {
3273+
deleteSql += " and \"ENGINE\" = '" + engine + "'";
3274+
}
3275+
try {
3276+
executeNoResult(deleteSql);
3277+
} catch (SQLException e) {
3278+
LOG.warn("Error removing partition column stats. ", e);
3279+
return false;
3280+
}
3281+
return true;
3282+
}
3283+
32443284
public Map<String, Map<String, String>> updatePartitionColumnStatisticsBatch(
32453285
Map<String, ColumnStatistics> partColStatsMap,
32463286
Table tbl,

standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/ObjectStore.java

Lines changed: 58 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10234,30 +10234,47 @@ protected Integer getJdoResult(GetHelper<Integer> ctx) throws MetaException, NoS
1023410234
public boolean deletePartitionColumnStatistics(String catName, String dbName, String tableName,
1023510235
String partName, List<String> partVals, String colName, String engine)
1023610236
throws NoSuchObjectException, MetaException, InvalidObjectException, InvalidInputException {
10237-
boolean ret = false;
10238-
Query query = null;
1023910237
dbName = org.apache.commons.lang3.StringUtils.defaultString(dbName,
1024010238
Warehouse.DEFAULT_DATABASE_NAME);
1024110239
catName = normalizeIdentifier(catName);
1024210240
if (tableName == null) {
1024310241
throw new InvalidInputException("Table name is null.");
1024410242
}
10243+
// Note: this does not verify ACID state; called internally when removing cols/etc.
10244+
// Also called via an unused metastore API that checks for ACID tables.
10245+
MPartition mPartition = getMPartition(catName, dbName, tableName, partName);
10246+
if (mPartition == null) {
10247+
throw new NoSuchObjectException("Partition " + partName
10248+
+ " for which stats deletion is requested doesn't exist");
10249+
}
10250+
10251+
return new GetHelper<Boolean>(catName, dbName, tableName, true, true) {
10252+
@Override
10253+
protected String describeResult() {
10254+
return "delete prtition column stats";
10255+
}
10256+
10257+
@Override
10258+
protected Boolean getSqlResult(GetHelper<Boolean> ctx) throws MetaException {
10259+
return directSql.deletePartitionColumnStats(catName, dbName, tableName, partName, colName, engine);
10260+
}
10261+
10262+
@Override
10263+
protected Boolean getJdoResult(GetHelper<Boolean> ctx)
10264+
throws MetaException, NoSuchObjectException, InvalidObjectException {
10265+
return deletePartitionColumnStatisticsVisJDO(catName, dbName, tableName, partName, colName, engine);
10266+
}
10267+
}.run(false);
10268+
}
10269+
10270+
private boolean deletePartitionColumnStatisticsVisJDO(String catName, String dbName, String tableName,
10271+
String partName, String colName, String engine) throws NoSuchObjectException {
10272+
boolean ret = false;
10273+
Query query = null;
1024510274
try {
1024610275
openTransaction();
10247-
MTable mTable = getMTable(catName, dbName, tableName);
1024810276
MPartitionColumnStatistics mStatsObj;
1024910277
List<MPartitionColumnStatistics> mStatsObjColl;
10250-
if (mTable == null) {
10251-
throw new NoSuchObjectException("Table " + tableName
10252-
+ " for which stats deletion is requested doesn't exist");
10253-
}
10254-
// Note: this does not verify ACID state; called internally when removing cols/etc.
10255-
// Also called via an unused metastore API that checks for ACID tables.
10256-
MPartition mPartition = getMPartition(catName, dbName, tableName, partVals, mTable);
10257-
if (mPartition == null) {
10258-
throw new NoSuchObjectException("Partition " + partName
10259-
+ " for which stats deletion is requested doesn't exist");
10260-
}
1026110278
query = pm.newQuery(MPartitionColumnStatistics.class);
1026210279
String filter;
1026310280
String parameters;
@@ -10334,25 +10351,42 @@ public boolean deletePartitionColumnStatistics(String catName, String dbName, St
1033410351
public boolean deleteTableColumnStatistics(String catName, String dbName, String tableName,
1033510352
String colName, String engine)
1033610353
throws NoSuchObjectException, MetaException, InvalidObjectException, InvalidInputException {
10337-
boolean ret = false;
10338-
Query query = null;
1033910354
dbName = org.apache.commons.lang3.StringUtils.defaultString(dbName,
1034010355
Warehouse.DEFAULT_DATABASE_NAME);
1034110356
if (tableName == null) {
1034210357
throw new InvalidInputException("Table name is null.");
1034310358
}
10359+
10360+
// Note: this does not verify ACID state; called internally when removing cols/etc.
10361+
// Also called via an unused metastore API that checks for ACID tables.
10362+
return new GetHelper<Boolean>(catName, dbName, tableName, true, true) {
10363+
10364+
@Override
10365+
protected String describeResult() {
10366+
return "delete table column stats";
10367+
}
10368+
10369+
@Override
10370+
protected Boolean getSqlResult(GetHelper<Boolean> ctx) throws MetaException {
10371+
return directSql.deleteTableColumnStatistics(getTable().getId(), colName, engine);
10372+
}
10373+
10374+
@Override
10375+
protected Boolean getJdoResult(GetHelper<Boolean> ctx)
10376+
throws MetaException, NoSuchObjectException, InvalidObjectException {
10377+
return deleteTableColumnStatisticsViaJdo(catName, dbName, tableName, colName, engine);
10378+
}
10379+
}.run(true);
10380+
}
10381+
10382+
private boolean deleteTableColumnStatisticsViaJdo(String catName, String dbName, String tableName,
10383+
String colName, String engine) throws NoSuchObjectException {
10384+
boolean ret = false;
10385+
Query query = null;
1034410386
try {
1034510387
openTransaction();
10346-
MTable mTable = getMTable(catName, dbName, tableName);
1034710388
MTableColumnStatistics mStatsObj;
1034810389
List<MTableColumnStatistics> mStatsObjColl;
10349-
if (mTable == null) {
10350-
throw new NoSuchObjectException("Table " +
10351-
TableName.getQualified(catName, dbName, tableName)
10352-
+ " for which stats deletion is requested doesn't exist");
10353-
}
10354-
// Note: this does not verify ACID state; called internally when removing cols/etc.
10355-
// Also called via an unused metastore API that checks for ACID tables.
1035610390
query = pm.newQuery(MTableColumnStatistics.class);
1035710391
String filter;
1035810392
String parameters;

standalone-metastore/metastore-server/src/test/java/org/apache/hadoop/hive/metastore/TestObjectStore.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -893,7 +893,7 @@ public void testTableStatisticsOps() throws Exception {
893893
}
894894

895895
@Test
896-
public void testGetPartitionStatistics() throws Exception {
896+
public void testPartitionStatisticsOps() throws Exception {
897897
createPartitionedTable(true, true);
898898

899899
List<List<ColumnStatistics>> stat;
@@ -912,6 +912,35 @@ public void testGetPartitionStatistics() throws Exception {
912912
ColumnStatisticsData expectedStats = new ColStatsBuilder<>(long.class).numNulls(1).numDVs(2)
913913
.low(3L).high(4L).hll(3, 4).kll(3, 4).build();
914914
assertEqualStatistics(expectedStats, computedStats);
915+
916+
objectStore.deletePartitionColumnStatistics(DEFAULT_CATALOG_NAME, DB1, TABLE1,
917+
"test_part_col=a0", Arrays.asList("a0"), null, ENGINE);
918+
try (AutoCloseable c = deadline()) {
919+
stat = objectStore.getPartitionColumnStatistics(DEFAULT_CATALOG_NAME, DB1, TABLE1,
920+
Arrays.asList("test_part_col=a0", "test_part_col=a1", "test_part_col=a2"),
921+
Collections.singletonList("test_part_col"));
922+
}
923+
Assert.assertEquals(1, stat.size());
924+
Assert.assertEquals(2, stat.get(0).size());
925+
926+
objectStore.deletePartitionColumnStatistics(DEFAULT_CATALOG_NAME, DB1, TABLE1,
927+
"test_part_col=a1", Arrays.asList("a1"), "test_part_col", null);
928+
try (AutoCloseable c = deadline()) {
929+
stat = objectStore.getPartitionColumnStatistics(DEFAULT_CATALOG_NAME, DB1, TABLE1,
930+
Arrays.asList("test_part_col=a0", "test_part_col=a1", "test_part_col=a2"),
931+
Collections.singletonList("test_part_col"));
932+
}
933+
Assert.assertEquals(1, stat.size());
934+
Assert.assertEquals(1, stat.get(0).size());
935+
936+
objectStore.deletePartitionColumnStatistics(DEFAULT_CATALOG_NAME, DB1, TABLE1,
937+
"test_part_col=a2", Arrays.asList("a2"), null, null);
938+
try (AutoCloseable c = deadline()) {
939+
stat = objectStore.getPartitionColumnStatistics(DEFAULT_CATALOG_NAME, DB1, TABLE1,
940+
Arrays.asList("test_part_col=a0", "test_part_col=a1", "test_part_col=a2"),
941+
Collections.singletonList("test_part_col"));
942+
}
943+
Assert.assertEquals(0, stat.size());
915944
}
916945

917946
/**

0 commit comments

Comments
 (0)