Skip to content

Commit 70415af

Browse files
authored
[Feat] (Nereids)support showTrash Command (#44684)
Issue Number: close #42763
1 parent 4bd1cc1 commit 70415af

6 files changed

Lines changed: 151 additions & 1 deletion

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ supportedShowStatement
246246
| SHOW TABLET DIAGNOSIS tabletId=INTEGER_VALUE #showDiagnoseTablet
247247
| SHOW FRONTENDS name=identifier? #showFrontends
248248
| SHOW TABLE tableId=INTEGER_VALUE #showTableId
249+
| SHOW TRASH (ON backend=STRING_LITERAL)? #showTrash
249250
| SHOW WHITELIST #showWhitelist
250251
| SHOW TABLETS BELONG
251252
tabletIds+=INTEGER_VALUE (COMMA tabletIds+=INTEGER_VALUE)* #showTabletsBelong
@@ -332,7 +333,6 @@ unsupportedShowStatement
332333
| SHOW BRIEF? RESTORE ((FROM | IN) database=multipartIdentifier)? wildWhere? #showRestore
333334
| SHOW RESOURCES wildWhere? sortClause? limitClause? #showResources
334335
| SHOW WORKLOAD GROUPS wildWhere? #showWorkloadGroups
335-
| SHOW TRASH (ON backend=STRING_LITERAL)? #showTrash
336336
| SHOW SNAPSHOT ON repo=identifier wildWhere? #showSnapshot
337337
| SHOW FULL? BUILTIN? FUNCTIONS
338338
((FROM | IN) database=multipartIdentifier)? wildWhere? #showFunctions

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@
255255
import org.apache.doris.nereids.DorisParser.ShowStorageEnginesContext;
256256
import org.apache.doris.nereids.DorisParser.ShowTableIdContext;
257257
import org.apache.doris.nereids.DorisParser.ShowTabletsBelongContext;
258+
import org.apache.doris.nereids.DorisParser.ShowTrashContext;
258259
import org.apache.doris.nereids.DorisParser.ShowTriggersContext;
259260
import org.apache.doris.nereids.DorisParser.ShowVariablesContext;
260261
import org.apache.doris.nereids.DorisParser.ShowViewContext;
@@ -541,6 +542,7 @@
541542
import org.apache.doris.nereids.trees.plans.commands.ShowStorageEnginesCommand;
542543
import org.apache.doris.nereids.trees.plans.commands.ShowTableIdCommand;
543544
import org.apache.doris.nereids.trees.plans.commands.ShowTabletsBelongCommand;
545+
import org.apache.doris.nereids.trees.plans.commands.ShowTrashCommand;
544546
import org.apache.doris.nereids.trees.plans.commands.ShowTriggersCommand;
545547
import org.apache.doris.nereids.trees.plans.commands.ShowVariablesCommand;
546548
import org.apache.doris.nereids.trees.plans.commands.ShowViewCommand;
@@ -4462,6 +4464,17 @@ public LogicalPlan visitShowTriggers(ShowTriggersContext ctx) {
44624464
return new ShowTriggersCommand();
44634465
}
44644466

4467+
@Override
4468+
public LogicalPlan visitShowTrash(ShowTrashContext ctx) {
4469+
if (ctx.ON() != null) {
4470+
String backend = stripQuotes(ctx.STRING_LITERAL().getText());
4471+
new ShowTrashCommand(backend);
4472+
} else {
4473+
return new ShowTrashCommand();
4474+
}
4475+
return new ShowTrashCommand();
4476+
}
4477+
44654478
@Override
44664479
public LogicalPlan visitShowRepositories(ShowRepositoriesContext ctx) {
44674480
return new ShowRepositoriesCommand();

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ public enum PlanType {
220220
SHOW_SMALL_FILES_COMMAND,
221221
SHOW_STORAGE_ENGINES_COMMAND,
222222
SHOW_TABLE_ID_COMMAND,
223+
SHOW_TRASH_COMMAND,
223224
SHOW_TRIGGERS_COMMAND,
224225
SHOW_VARIABLES_COMMAND,
225226
SHOW_AUTHORS_COMMAND,
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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.catalog.Column;
21+
import org.apache.doris.catalog.Env;
22+
import org.apache.doris.catalog.ScalarType;
23+
import org.apache.doris.common.ErrorCode;
24+
import org.apache.doris.common.ErrorReport;
25+
import org.apache.doris.common.proc.TrashProcDir;
26+
import org.apache.doris.common.util.NetUtils;
27+
import org.apache.doris.mysql.privilege.PrivPredicate;
28+
import org.apache.doris.nereids.trees.plans.PlanType;
29+
import org.apache.doris.nereids.trees.plans.visitor.PlanVisitor;
30+
import org.apache.doris.qe.ConnectContext;
31+
import org.apache.doris.qe.ShowResultSet;
32+
import org.apache.doris.qe.ShowResultSetMetaData;
33+
import org.apache.doris.qe.StmtExecutor;
34+
import org.apache.doris.system.Backend;
35+
36+
import com.google.common.collect.ImmutableMap;
37+
import com.google.common.collect.Lists;
38+
39+
import java.util.List;
40+
41+
/**
42+
* show trash command
43+
*/
44+
public class ShowTrashCommand extends ShowCommand {
45+
private List<Backend> backends = Lists.newArrayList();
46+
private String backendQuery;
47+
48+
public ShowTrashCommand() {
49+
super(PlanType.SHOW_TRASH_COMMAND);
50+
}
51+
52+
public ShowTrashCommand(String backendQuery) {
53+
super(PlanType.SHOW_TRASH_COMMAND);
54+
this.backendQuery = backendQuery;
55+
}
56+
57+
public List<Backend> getBackends() {
58+
return backends;
59+
}
60+
61+
public String getBackend() {
62+
return backendQuery;
63+
}
64+
65+
public ShowResultSetMetaData getMetaData() {
66+
ShowResultSetMetaData.Builder builder = ShowResultSetMetaData.builder();
67+
for (String title : TrashProcDir.TITLE_NAMES) {
68+
builder.addColumn(new Column(title, ScalarType.createVarchar(30)));
69+
}
70+
return builder.build();
71+
}
72+
73+
private ShowResultSet handleShowTrash(String backendQuery) throws Exception {
74+
if (!Env.getCurrentEnv().getAccessManager().checkGlobalPriv(ConnectContext.get(), PrivPredicate.ADMIN)
75+
&& !Env.getCurrentEnv().getAccessManager().checkGlobalPriv(ConnectContext.get(),
76+
PrivPredicate.OPERATOR)) {
77+
ErrorReport.reportAnalysisException(ErrorCode.ERR_SPECIFIC_ACCESS_DENIED_ERROR, "ADMIN/OPERATOR");
78+
}
79+
ImmutableMap<Long, Backend> backendsInfo = Env.getCurrentSystemInfo().getAllBackendsByAllCluster();
80+
if (backendQuery == null || backendQuery.isEmpty()) {
81+
for (Backend backend : backendsInfo.values()) {
82+
this.backends.add(backend);
83+
}
84+
} else {
85+
for (Backend backend : backendsInfo.values()) {
86+
String backendStr = NetUtils.getHostPortInAccessibleFormat(backend.getHost(),
87+
backend.getHeartbeatPort());
88+
if (backendQuery.equals(backendStr)) {
89+
this.backends.add(backend);
90+
break;
91+
}
92+
}
93+
}
94+
List<List<String>> infos = Lists.newArrayList();
95+
TrashProcDir.getTrashInfo(backends, infos);
96+
return new ShowResultSet(getMetaData(), infos);
97+
}
98+
99+
@Override
100+
public <R, C> R accept(PlanVisitor<R, C> visitor, C context) {
101+
return visitor.visitShowTrashCommand(this, context);
102+
}
103+
104+
@Override
105+
public ShowResultSet doRun(ConnectContext ctx, StmtExecutor executor) throws Exception {
106+
return handleShowTrash(backendQuery);
107+
}
108+
}
109+

‎fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/visitor/CommandVisitor.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@
102102
import org.apache.doris.nereids.trees.plans.commands.ShowStorageEnginesCommand;
103103
import org.apache.doris.nereids.trees.plans.commands.ShowTableIdCommand;
104104
import org.apache.doris.nereids.trees.plans.commands.ShowTabletsBelongCommand;
105+
import org.apache.doris.nereids.trees.plans.commands.ShowTrashCommand;
105106
import org.apache.doris.nereids.trees.plans.commands.ShowTriggersCommand;
106107
import org.apache.doris.nereids.trees.plans.commands.ShowVariablesCommand;
107108
import org.apache.doris.nereids.trees.plans.commands.ShowViewCommand;
@@ -370,6 +371,10 @@ default R visitShowPluginsCommand(ShowPluginsCommand showPluginsCommand, C conte
370371
return visitCommand(showPluginsCommand, context);
371372
}
372373

374+
default R visitShowTrashCommand(ShowTrashCommand showTrashCommand, C context) {
375+
return visitCommand(showTrashCommand, context);
376+
}
377+
373378
default R visitShowTriggersCommand(ShowTriggersCommand showTriggersCommand, C context) {
374379
return visitCommand(showTriggersCommand, context);
375380
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
suite("show_trash_nereids") {
19+
// can not use qt command since the output change based on cluster and backend ip
20+
checkNereidsExecute("""show trash;""")
21+
checkNereidsExecute("""show trash on "127.0.0.1:9050";""")
22+
}

0 commit comments

Comments
 (0)