DanielZhu58 commented on code in PR #6438:
URL: https://github.com/apache/hive/pull/6438#discussion_r3224308614


##########
standalone-metastore/metastore-server/src/test/java/org/apache/hadoop/hive/metastore/TestStatisticsManagement.java:
##########
@@ -0,0 +1,497 @@
+/*
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.metastore;
+
+import static org.apache.hadoop.hive.metastore.Warehouse.DEFAULT_CATALOG_NAME;
+import static org.apache.hadoop.hive.metastore.Warehouse.DEFAULT_DATABASE_NAME;
+import static org.junit.Assert.assertTrue;
+
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.hive.metastore.annotation.MetastoreUnitTest;
+import org.apache.hadoop.hive.metastore.api.ColumnStatistics;
+import org.apache.hadoop.hive.metastore.api.ColumnStatisticsData;
+import org.apache.hadoop.hive.metastore.api.ColumnStatisticsDesc;
+import org.apache.hadoop.hive.metastore.api.ColumnStatisticsObj;
+import org.apache.hadoop.hive.metastore.api.Database;
+import org.apache.hadoop.hive.metastore.api.DoubleColumnStatsData;
+import org.apache.hadoop.hive.metastore.api.NoSuchObjectException;
+import org.apache.hadoop.hive.metastore.api.Partition;
+import org.apache.hadoop.hive.metastore.api.Table;
+import org.apache.hadoop.hive.metastore.client.builder.DatabaseBuilder;
+import org.apache.hadoop.hive.metastore.client.builder.PartitionBuilder;
+import org.apache.hadoop.hive.metastore.client.builder.TableBuilder;
+import org.apache.hadoop.hive.metastore.conf.MetastoreConf;
+import org.apache.hadoop.hive.metastore.conf.MetastoreConf.ConfVars;
+import org.apache.hadoop.hive.metastore.model.MPartitionColumnStatistics;
+import org.apache.hadoop.hive.metastore.model.MTableColumnStatistics;
+import org.apache.hadoop.hive.metastore.security.HadoopThriftAuthBridge;
+import org.apache.hadoop.hive.metastore.utils.TestTxnDbUtil;
+import org.apache.thrift.TException;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+import javax.jdo.PersistenceManager;
+import javax.jdo.Query;
+
+/**
+ * Unit tests for {@link StatisticsManagementTask}, verifying that expired 
table-level and
+ * partition-level column statistics are deleted on schedule, and that tables 
marked with the
+ * exclude property are left untouched.
+ */
+@Category(MetastoreUnitTest.class)
+public class TestStatisticsManagement {
+
+  private IMetaStoreClient client;
+  private Configuration conf;
+
+  @Before
+  public void setUp() throws Exception {
+    conf = MetastoreConf.newMetastoreConf();
+    MetastoreConf.setVar(conf, ConfVars.METASTORE_METADATA_TRANSFORMER_CLASS, 
" ");
+    MetaStoreTestUtils.setConfForStandloneMode(conf);
+    conf.setBoolean(ConfVars.MULTITHREADED.getVarname(), false);
+    conf.setBoolean(ConfVars.HIVE_IN_TEST.getVarname(), true);
+
+    // Enable stats auto deletion with a short retention so the threshold 
check triggers easily.
+    MetastoreConf.setBoolVar(conf, ConfVars.COLUMN_STATISTICS_AUTO_DELETION, 
true);
+    MetastoreConf.setTimeVar(conf, 
ConfVars.COLUMN_STATISTICS_RETENTION_PERIOD, 1, TimeUnit.DAYS);
+
+    
MetaStoreTestUtils.startMetaStoreWithRetry(HadoopThriftAuthBridge.getBridge(), 
conf);
+    TestTxnDbUtil.setConfValues(conf);
+    TestTxnDbUtil.prepDb(conf);
+
+    client = new HiveMetaStoreClient(conf);
+  }
+
+  @After
+  public void tearDown() throws Exception {
+    if (client != null) {
+      // Drop any leftover databases, similar to TestPartitionManagement.java.
+      List<String> dbs = client.getAllDatabases(DEFAULT_CATALOG_NAME);
+      for (String db : dbs) {
+        if (!db.equalsIgnoreCase(DEFAULT_DATABASE_NAME)) {
+          client.dropDatabase(DEFAULT_CATALOG_NAME, db, true, false, true);
+        }
+      }
+    }
+    try {
+      if (client != null) {
+        client.close();
+      }
+    } finally {
+      client = null;
+    }
+  }
+
+  @Test
+  public void testExpiredTableColStatsAreDeleted() throws Exception {
+    String dbName = "stats_db1";
+    String tableName = "tbl1";
+    createDbAndTable(dbName, tableName, false);
+    writeTableLevelColStats(dbName, tableName, "c1");
+    assertHasTableColStats(dbName, tableName, "c1");
+    makeAllTableColStatsOlderThanRetention(dbName, tableName);
+
+    runStatisticsManagementTask(conf);
+
+    assertNoTableColStats(dbName, tableName, "c1");
+  }
+
+  @Test
+  public void testExcludedTableStatsAreNotDeleted() throws Exception {
+    String dbName = "stats_db2";
+    String tableName = "tbl2";
+    createDbAndTable(dbName, tableName, true);
+    writeTableLevelColStats(dbName, tableName, "c1");
+    assertHasTableColStats(dbName, tableName, "c1");
+    makeAllTableColStatsOlderThanRetention(dbName, tableName);
+
+    runStatisticsManagementTask(conf);
+
+    // Stats must still be present because the table is marked as excluded.
+    assertHasTableColStats(dbName, tableName, "c1");
+  }
+
+  /**
+   * Verifies that setting {@value 
StatisticsManagementTask#STATISTICS_AUTO_DELETION_EXCLUDE_TBLPROPERTY}
+   * to {@code "false"} does not prevent deletion. Only an explicit {@code 
"true"} value excludes
+   * a table; any other value (including {@code "false"}) is treated as not 
excluded.
+   */
+  @Test
+  public void testExcludePropertySetToFalseDoesNotPreventDeletion() throws 
Exception {
+    String dbName = "stats_db3";
+    String tableName = "tbl3";
+    createDbAndTable(dbName, tableName, false);
+
+    // Explicitly set the exclude property to "false" — the task must still 
delete these stats.
+    Table t = client.getTable(dbName, tableName);
+    t.getParameters().put(
+        StatisticsManagementTask.STATISTICS_AUTO_DELETION_EXCLUDE_TBLPROPERTY, 
"false");
+    client.alter_table(dbName, tableName, t);
+
+    writeTableLevelColStats(dbName, tableName, "c1");
+    assertHasTableColStats(dbName, tableName, "c1");
+    makeAllTableColStatsOlderThanRetention(dbName, tableName);
+
+    runStatisticsManagementTask(conf);
+
+    // "false" is not a valid exclude signal; stats should be deleted.
+    assertNoTableColStats(dbName, tableName, "c1");
+  }
+
+  /**
+   * Verifies that expired partition-level column statistics are deleted when 
the task runs.
+   */
+  @Test
+  public void testExpiredPartitionColStatsAreDeleted() throws Exception {
+    String dbName = "stats_db4";
+    String tableName = "tbl4";
+    String partVal = "p1";
+    createDbAndPartitionedTable(dbName, tableName, false);
+    createPartition(dbName, tableName, partVal);
+
+    String partName = "part_col=" + partVal;
+    writePartitionLevelColStats(dbName, tableName, partName, "c1");
+    assertHasPartitionColStats(dbName, tableName, partName, "c1");
+    makeAllPartitionColStatsOlderThanRetention(dbName, tableName);
+
+    runStatisticsManagementTask(conf);
+
+    assertNoPartitionColStats(dbName, tableName, partName, "c1");
+  }
+
+  /**
+   * Verifies that partition-level column statistics are not deleted when the 
table has the
+   * exclude property set to {@code "true"}.
+   */
+  @Test
+  public void testExcludedTablePartitionStatsAreNotDeleted() throws Exception {
+    String dbName = "stats_db5";
+    String tableName = "tbl5";
+    String partVal = "p1";
+    createDbAndPartitionedTable(dbName, tableName, true);
+    createPartition(dbName, tableName, partVal);
+
+    String partName = "part_col=" + partVal;
+    writePartitionLevelColStats(dbName, tableName, partName, "c1");
+    assertHasPartitionColStats(dbName, tableName, partName, "c1");
+    makeAllPartitionColStatsOlderThanRetention(dbName, tableName);
+
+    runStatisticsManagementTask(conf);
+
+    // Stats must still be present because the table is marked as excluded.
+    assertHasPartitionColStats(dbName, tableName, partName, "c1");
+  }
+
+  /**
+   * Creates a database (unless it is the default database) and a simple 
two-column test table.
+   *
+   * @param dbName    name of the database to create
+   * @param tableName name of the table to create
+   * @param exclude   if {@code true}, sets the auto-deletion exclude property 
on the table
+   */
+  private void createDbAndTable(String dbName, String tableName, boolean 
exclude) throws Exception {
+    Database db;
+    if (!DEFAULT_DATABASE_NAME.equals(dbName)) {
+      db = new DatabaseBuilder()
+          .setName(dbName)
+          .setCatalogName(DEFAULT_CATALOG_NAME)
+          .create(client, conf);
+    } else {
+      db = client.getDatabase(DEFAULT_CATALOG_NAME, DEFAULT_DATABASE_NAME);
+    }
+
+    TableBuilder tb = new TableBuilder()
+        .inDb(db)
+        .setTableName(tableName)
+        .addCol("c1", "double")
+        .addCol("c2", "string")
+        .setInputFormat("org.apache.hadoop.hive.ql.io.orc.OrcInputFormat")
+        .setOutputFormat("org.apache.hadoop.hive.ql.io.orc.OrcOutputFormat");
+
+    Table t = tb.build(conf);
+    if (exclude) {
+      t.getParameters().put(
+          
StatisticsManagementTask.STATISTICS_AUTO_DELETION_EXCLUDE_TBLPROPERTY, "true");
+    }
+    client.createTable(t);
+    client.flushCache();
+  }
+
+  /**
+   * Creates a database and a partitioned test table with one partition key 
{@code part_col}.
+   *
+   * @param dbName    name of the database to create
+   * @param tableName name of the table to create
+   * @param exclude   if {@code true}, sets the auto-deletion exclude property 
on the table
+   */
+  private void createDbAndPartitionedTable(String dbName, String tableName,
+                                           boolean exclude) throws Exception {
+    Database db = new DatabaseBuilder()
+        .setName(dbName)
+        .setCatalogName(DEFAULT_CATALOG_NAME)
+        .create(client, conf);
+
+    TableBuilder tb = new TableBuilder()
+        .inDb(db)
+        .setTableName(tableName)
+        .addCol("c1", "double")
+        .addCol("c2", "string")
+        .addPartCol("part_col", "string")
+        .setInputFormat("org.apache.hadoop.hive.ql.io.orc.OrcInputFormat")
+        .setOutputFormat("org.apache.hadoop.hive.ql.io.orc.OrcOutputFormat");
+
+    Table t = tb.build(conf);
+    if (exclude) {
+      t.getParameters().put(
+          
StatisticsManagementTask.STATISTICS_AUTO_DELETION_EXCLUDE_TBLPROPERTY, "true");
+    }
+    client.createTable(t);
+    client.flushCache();
+  }
+
+  /**
+   * Adds a single partition with the given value for {@code part_col} to the 
specified table.
+   *
+   * @param dbName    database name
+   * @param tableName table name
+   * @param partVal   value for the {@code part_col} partition key
+   */
+  private void createPartition(String dbName, String tableName, String 
partVal) throws Exception {
+    Table t = client.getTable(dbName, tableName);
+    Partition partition = new PartitionBuilder()
+        .inTable(t)
+        .addValue(partVal)
+        .build(conf);
+    client.add_partition(partition);
+  }
+
+  /**
+   * Writes minimal table-level column statistics for the given column via the 
metastore client.
+   *
+   * @param db  database name
+   * @param tbl table name
+   * @param col column name
+   */
+  private void writeTableLevelColStats(String db, String tbl, String col) 
throws TException {
+    ColumnStatisticsObj obj = buildDoubleColStatsObj(col);
+
+    ColumnStatisticsDesc desc = new ColumnStatisticsDesc(true, db, tbl);
+    desc.setCatName("hive");

Review Comment:
   Acknowledged and changed. Will leave the engine as "hive" for now.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to