Skip to content

Commit e6c3068

Browse files
authored
[Enhancement] (nereids)implement dropCached/Expired/StatsCommand in nereids (#48763)
Issue Number: close #42709,#42710,#42711
1 parent 8df0b80 commit e6c3068

File tree

9 files changed

+401
-7
lines changed

9 files changed

+401
-7
lines changed

fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisParser.g4

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -743,14 +743,14 @@ supportedStatsStatement
743743
MODIFY COLUMN columnName=identifier
744744
SET STATS LEFT_PAREN propertyItemList RIGHT_PAREN partitionSpec? #alterColumnStats
745745
| SHOW INDEX STATS tableName=multipartIdentifier indexId=identifier #showIndexStats
746-
;
747-
748-
unsupportedStatsStatement
749-
: DROP STATS tableName=multipartIdentifier
746+
| DROP STATS tableName=multipartIdentifier
750747
columns=identifierList? partitionSpec? #dropStats
751748
| DROP CACHED STATS tableName=multipartIdentifier #dropCachedStats
752749
| DROP EXPIRED STATS #dropExpiredStats
753-
| DROP ANALYZE JOB INTEGER_VALUE #dropAanalyzeJob
750+
;
751+
752+
unsupportedStatsStatement
753+
: DROP ANALYZE JOB INTEGER_VALUE #dropAanalyzeJob
754754
| KILL ANALYZE jobId=INTEGER_VALUE #killAnalyzeJob
755755
| SHOW TABLE STATS tableName=multipartIdentifier
756756
partitionSpec? columnList=identifierList? #showTableStats

fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.apache.doris.analysis.DbName;
2727
import org.apache.doris.analysis.EncryptKeyName;
2828
import org.apache.doris.analysis.FunctionName;
29+
import org.apache.doris.analysis.PartitionNames;
2930
import org.apache.doris.analysis.PassVar;
3031
import org.apache.doris.analysis.SetType;
3132
import org.apache.doris.analysis.StorageBackend;
@@ -563,12 +564,14 @@
563564
import org.apache.doris.nereids.trees.plans.commands.DeleteFromCommand;
564565
import org.apache.doris.nereids.trees.plans.commands.DeleteFromUsingCommand;
565566
import org.apache.doris.nereids.trees.plans.commands.DescribeCommand;
567+
import org.apache.doris.nereids.trees.plans.commands.DropCachedStatsCommand;
566568
import org.apache.doris.nereids.trees.plans.commands.DropCatalogCommand;
567569
import org.apache.doris.nereids.trees.plans.commands.DropCatalogRecycleBinCommand;
568570
import org.apache.doris.nereids.trees.plans.commands.DropCatalogRecycleBinCommand.IdType;
569571
import org.apache.doris.nereids.trees.plans.commands.DropConstraintCommand;
570572
import org.apache.doris.nereids.trees.plans.commands.DropDatabaseCommand;
571573
import org.apache.doris.nereids.trees.plans.commands.DropEncryptkeyCommand;
574+
import org.apache.doris.nereids.trees.plans.commands.DropExpiredStatsCommand;
572575
import org.apache.doris.nereids.trees.plans.commands.DropFileCommand;
573576
import org.apache.doris.nereids.trees.plans.commands.DropFunctionCommand;
574577
import org.apache.doris.nereids.trees.plans.commands.DropJobCommand;
@@ -577,6 +580,7 @@
577580
import org.apache.doris.nereids.trees.plans.commands.DropRepositoryCommand;
578581
import org.apache.doris.nereids.trees.plans.commands.DropRoleCommand;
579582
import org.apache.doris.nereids.trees.plans.commands.DropSqlBlockRuleCommand;
583+
import org.apache.doris.nereids.trees.plans.commands.DropStatsCommand;
580584
import org.apache.doris.nereids.trees.plans.commands.DropStoragePolicyCommand;
581585
import org.apache.doris.nereids.trees.plans.commands.DropTableCommand;
582586
import org.apache.doris.nereids.trees.plans.commands.DropUserCommand;
@@ -847,6 +851,7 @@
847851
import java.util.ArrayList;
848852
import java.util.Collections;
849853
import java.util.HashMap;
854+
import java.util.HashSet;
850855
import java.util.List;
851856
import java.util.Locale;
852857
import java.util.Map;
@@ -6189,6 +6194,34 @@ public LogicalPlan visitDescribeTableValuedFunction(DorisParser.DescribeTableVal
61896194
return new DescribeCommand(tableValuedFunctionRef);
61906195
}
61916196

6197+
@Override
6198+
public LogicalPlan visitDropStats(DorisParser.DropStatsContext ctx) {
6199+
TableNameInfo tableNameInfo = new TableNameInfo(visitMultipartIdentifier(ctx.tableName));
6200+
6201+
Set<String> columnNames = new HashSet<>();
6202+
if (ctx.identifierList() != null) {
6203+
columnNames.addAll(visitIdentifierList(ctx.identifierList()));
6204+
}
6205+
6206+
PartitionNames partitionNames = null;
6207+
if (ctx.partitionSpec() != null) {
6208+
Pair<Boolean, List<String>> partitionSpec = visitPartitionSpec(ctx.partitionSpec());
6209+
partitionNames = new PartitionNames(partitionSpec.first, partitionSpec.second);
6210+
}
6211+
return new DropStatsCommand(tableNameInfo, columnNames, partitionNames);
6212+
}
6213+
6214+
@Override
6215+
public LogicalPlan visitDropCachedStats(DorisParser.DropCachedStatsContext ctx) {
6216+
TableNameInfo tableNameInfo = new TableNameInfo(visitMultipartIdentifier(ctx.tableName));
6217+
return new DropCachedStatsCommand(tableNameInfo);
6218+
}
6219+
6220+
@Override
6221+
public LogicalPlan visitDropExpiredStats(DorisParser.DropExpiredStatsContext ctx) {
6222+
return new DropExpiredStatsCommand();
6223+
}
6224+
61926225
@Override
61936226
public LogicalPlan visitAlterTableStats(DorisParser.AlterTableStatsContext ctx) {
61946227
TableNameInfo tableNameInfo = new TableNameInfo(visitMultipartIdentifier(ctx.name));

fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/PlanType.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,9 @@ public enum PlanType {
316316
ALTER_SYSTEM_MODIFY_BACKEND,
317317
ALTER_SYSTEM_MODIFY_FRONTEND_OR_BACKEND_HOSTNAME,
318318
ALTER_SYSTEM_RENAME_COMPUTE_GROUP,
319+
DROP_STATS_COMMAND,
320+
DROP_CACHED_STATS_COMMAND,
321+
DROP_EXPIRED_STATS_COMMAND,
319322
ALTER_TABLE_STATS_COMMAND,
320323
ALTER_COLUMN_STATS_COMMAND
321324
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.apache.doris.nereids.trees.plans.commands;
19+
20+
import org.apache.doris.analysis.StmtType;
21+
import org.apache.doris.catalog.DatabaseIf;
22+
import org.apache.doris.catalog.Env;
23+
import org.apache.doris.catalog.TableIf;
24+
import org.apache.doris.common.AnalysisException;
25+
import org.apache.doris.common.ErrorCode;
26+
import org.apache.doris.common.ErrorReport;
27+
import org.apache.doris.common.UserException;
28+
import org.apache.doris.datasource.CatalogIf;
29+
import org.apache.doris.mysql.privilege.PrivPredicate;
30+
import org.apache.doris.nereids.trees.plans.PlanType;
31+
import org.apache.doris.nereids.trees.plans.commands.info.TableNameInfo;
32+
import org.apache.doris.nereids.trees.plans.visitor.PlanVisitor;
33+
import org.apache.doris.qe.ConnectContext;
34+
import org.apache.doris.qe.StmtExecutor;
35+
36+
/**
37+
* Manually drop cached statistics for table and its mv.
38+
* <p>
39+
* syntax:
40+
* DROP CACHED STATS TableName;
41+
*/
42+
public class DropCachedStatsCommand extends DropCommand {
43+
private final TableNameInfo tableNameInfo;
44+
private long catalogId;
45+
private long dbId;
46+
private long tblId;
47+
48+
public DropCachedStatsCommand(TableNameInfo tableNameInfo) {
49+
super(PlanType.DROP_CACHED_STATS_COMMAND);
50+
this.tableNameInfo = tableNameInfo;
51+
}
52+
53+
@Override
54+
public void doRun(ConnectContext ctx, StmtExecutor executor) throws Exception {
55+
validate(ctx);
56+
ctx.getEnv().getAnalysisManager().dropCachedStats(catalogId, dbId, tblId);
57+
}
58+
59+
private void validate(ConnectContext ctx) throws UserException {
60+
if (tableNameInfo == null) {
61+
throw new UserException("Should specify a valid table name.");
62+
}
63+
tableNameInfo.analyze(ctx);
64+
String catalogName = tableNameInfo.getCtl();
65+
String dbName = tableNameInfo.getDb();
66+
String tblName = tableNameInfo.getTbl();
67+
CatalogIf catalog = ctx.getEnv().getCatalogMgr().getCatalogOrAnalysisException(tableNameInfo.getCtl());
68+
DatabaseIf db = catalog.getDbOrAnalysisException(dbName);
69+
TableIf table = db.getTableOrAnalysisException(tblName);
70+
tblId = table.getId();
71+
dbId = db.getId();
72+
catalogId = catalog.getId();
73+
// check permission
74+
checkAnalyzePriv(catalogName, db.getFullName(), table.getName());
75+
}
76+
77+
private void checkAnalyzePriv(String catalogName, String dbName, String tblName) throws AnalysisException {
78+
if (!Env.getCurrentEnv().getAccessManager()
79+
.checkTblPriv(ConnectContext.get(), catalogName, dbName, tblName,
80+
PrivPredicate.DROP)) {
81+
ErrorReport.reportAnalysisException(
82+
ErrorCode.ERR_TABLEACCESS_DENIED_ERROR,
83+
"DROP",
84+
ConnectContext.get().getQualifiedUser(),
85+
ConnectContext.get().getRemoteIP(),
86+
dbName + "." + tblName);
87+
}
88+
}
89+
90+
@Override
91+
public <R, C> R accept(PlanVisitor<R, C> visitor, C context) {
92+
return visitor.visitDropCachedStatsCommand(this, context);
93+
}
94+
95+
@Override
96+
public StmtType stmtType() {
97+
return StmtType.DROP;
98+
}
99+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.apache.doris.nereids.trees.plans.commands;
19+
20+
import org.apache.doris.analysis.StmtType;
21+
import org.apache.doris.nereids.trees.plans.PlanType;
22+
import org.apache.doris.nereids.trees.plans.visitor.PlanVisitor;
23+
import org.apache.doris.qe.ConnectContext;
24+
import org.apache.doris.qe.StmtExecutor;
25+
26+
/**
27+
* DropExpiredStatsCommand
28+
*/
29+
30+
public class DropExpiredStatsCommand extends DropCommand {
31+
32+
public DropExpiredStatsCommand() {
33+
super(PlanType.DROP_EXPIRED_STATS_COMMAND);
34+
}
35+
36+
@Override
37+
public void doRun(ConnectContext ctx, StmtExecutor executor) throws Exception {
38+
ctx.getEnv().getAnalysisManager().dropExpiredStats();
39+
}
40+
41+
@Override
42+
public <R, C> R accept(PlanVisitor<R, C> visitor, C context) {
43+
return visitor.visitDropExpiredStatsCommand(this, context);
44+
}
45+
46+
@Override
47+
public StmtType stmtType() {
48+
return StmtType.DROP;
49+
}
50+
}

0 commit comments

Comments
 (0)