You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@doris.apache.org by GitBox <gi...@apache.org> on 2022/10/12 06:51:48 UTC

[GitHub] [doris] Kikyou1997 commented on a diff in pull request #13303: [enhancement](statistics) Support to drop table or partition statistics

Kikyou1997 commented on code in PR #13303:
URL: https://github.com/apache/doris/pull/13303#discussion_r993050520


##########
fe/fe-core/src/main/java/org/apache/doris/analysis/DropTableStatsStmt.java:
##########
@@ -0,0 +1,140 @@
+// 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.doris.analysis;
+
+import org.apache.doris.catalog.Database;
+import org.apache.doris.catalog.Env;
+import org.apache.doris.catalog.Table;
+import org.apache.doris.common.AnalysisException;
+import org.apache.doris.common.ErrorCode;
+import org.apache.doris.common.ErrorReport;
+import org.apache.doris.common.UserException;
+import org.apache.doris.common.util.Util;
+import org.apache.doris.mysql.privilege.PaloAuth;
+import org.apache.doris.mysql.privilege.PrivPredicate;
+import org.apache.doris.qe.ConnectContext;
+
+import com.google.common.base.Preconditions;
+import com.google.common.base.Strings;
+import com.google.common.collect.Maps;
+import com.google.common.collect.Sets;
+
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * Manually drop statistics for tables or partitions.
+ * Table or partition can be specified, if neither is specified,
+ * all statistics under the current database will be deleted.
+ *
+ * syntax:
+ *     DROP STATS [TableName [PARTITIONS(partitionNames)]];
+ */
+public class DropTableStatsStmt extends DdlStmt {
+    private final TableName tableName;
+    private final PartitionNames optPartitionNames;
+
+    // after analyzed
+    private final Map<Long, Set<String>> tblIdToPartition = Maps.newHashMap();
+
+    public DropTableStatsStmt(TableName tableName, PartitionNames optPartitionNames) {
+        this.tableName = tableName;
+        this.optPartitionNames = optPartitionNames;
+    }
+
+    public Map<Long, Set<String>> getTblIdToPartition() {
+        Preconditions.checkArgument(isAnalyzed(),
+                "The partition name must be obtained after the parsing is complete");
+        return tblIdToPartition;
+    }
+
+    @Override
+    public void analyze(Analyzer analyzer) throws UserException {
+        super.analyze(analyzer);
+
+        if (tableName != null) {
+            if (Strings.isNullOrEmpty(tableName.getDb())) {
+                tableName.setDb(analyzer.getDefaultDb());
+            }
+
+            tableName.analyze(analyzer);
+
+            // check whether the deletion permission is granted
+            checkAnalyzePriv(tableName.getDb(), tableName.getTbl());
+
+            // disallow external catalog
+            Util.prohibitExternalCatalog(tableName.getCtl(), this.getClass().getSimpleName());
+
+            Database db = analyzer.getEnv().getInternalCatalog()
+                    .getDbOrAnalysisException(tableName.getDb());
+            long tableId = db.getTableOrAnalysisException(tableName.getTbl()).getId();
+
+            if (optPartitionNames == null) {
+                tblIdToPartition.put(tableId, null);
+            } else {
+                optPartitionNames.analyze(analyzer);
+                List<String> pNames = optPartitionNames.getPartitionNames();
+                HashSet<String> partitionNames = Sets.newHashSet(pNames);
+                tblIdToPartition.put(tableId, partitionNames);
+            }
+        } else {
+            Database db = analyzer.getEnv().getInternalCatalog()
+                    .getDbOrAnalysisException(analyzer.getDefaultDb());
+            for (Table table : db.getTables()) {

Review Comment:
   Why put partitions of every tables in db into the map



##########
fe/fe-core/src/main/java/org/apache/doris/statistics/Statistics.java:
##########
@@ -159,6 +164,26 @@ public void updateColumnStats(long tableId, String partitionName, String columnN
         }
     }
 
+    public void dropTableStats(long tableId) {
+        dropPartitionStats(tableId, null);
+    }
+
+    public void dropPartitionStats(long tableId, String partitionName) {

Review Comment:
   you can simply add synchronized modifier to this method, since whole code in it is in the synced block



##########
fe/fe-core/src/main/java/org/apache/doris/statistics/Statistics.java:
##########
@@ -159,6 +164,26 @@ public void updateColumnStats(long tableId, String partitionName, String columnN
         }
     }
 
+    public void dropTableStats(long tableId) {
+        dropPartitionStats(tableId, null);
+    }
+
+    public void dropPartitionStats(long tableId, String partitionName) {

Review Comment:
   Besides, I think the ` if (idToTableStats.containsKey(tableId))` is a bit redundant, since you will use the value anyway in the inner block



##########
fe/fe-core/src/main/java/org/apache/doris/statistics/TableStats.java:
##########
@@ -78,7 +78,7 @@ public TableStats(double rowCount, long dataSize) {
     }
 
     public double getRowCount() {
-        if  (rowCount == -1) {
+        if  (rowCount == -1 && !nameToPartitionStats.isEmpty()) {

Review Comment:
   Is latter condition necessary?



##########
fe/fe-core/src/main/java/org/apache/doris/statistics/StatisticsManager.java:
##########
@@ -58,6 +60,27 @@ public Statistics getStatistics() {
         return statistics;
     }
 
+    /**
+     * Support for deleting table or partition statistics.
+     *
+     * @param stmt get table name and partition name from it.
+     */
+    public void dropStats(DropTableStatsStmt stmt) {

Review Comment:
   seems that the grammer doesn't allow to drop multiple table in single statement though



-- 
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: commits-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org