Skip to content

Commit d63cab3

Browse files
rootheguanhui
authored andcommitted
[fix](fe) Fix SHOW TRASH ON clause to support multiple backends
1 parent f7671a0 commit d63cab3

6 files changed

Lines changed: 261 additions & 26 deletions

File tree

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6678,10 +6678,9 @@ public LogicalPlan visitShowTypeCast(DorisParser.ShowTypeCastContext ctx) {
66786678
@Override
66796679
public LogicalPlan visitShowTrash(ShowTrashContext ctx) {
66806680
if (ctx.ON() != null) {
6681-
String backend = stripQuotes(ctx.STRING_LITERAL().getText());
6682-
new ShowTrashCommand(backend);
6683-
} else {
6684-
return new ShowTrashCommand();
6681+
List<String> backendsQuery = Lists.newArrayList();
6682+
ctx.backends.forEach(backend -> backendsQuery.add(stripQuotes(backend.getText())));
6683+
return new ShowTrashCommand(backendsQuery);
66856684
}
66866685
return new ShowTrashCommand();
66876686
}

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

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,30 +36,32 @@
3636
import com.google.common.collect.ImmutableMap;
3737
import com.google.common.collect.Lists;
3838

39+
import java.util.HashMap;
3940
import java.util.List;
41+
import java.util.Map;
4042

4143
/**
4244
* show trash command
4345
*/
4446
public class ShowTrashCommand extends ShowCommand {
4547
private List<Backend> backends = Lists.newArrayList();
46-
private String backendQuery;
48+
private List<String> backendsQuery;
4749

4850
public ShowTrashCommand() {
4951
super(PlanType.SHOW_TRASH_COMMAND);
5052
}
5153

52-
public ShowTrashCommand(String backendQuery) {
54+
public ShowTrashCommand(List<String> backendsQuery) {
5355
super(PlanType.SHOW_TRASH_COMMAND);
54-
this.backendQuery = backendQuery;
56+
this.backendsQuery = backendsQuery;
5557
}
5658

5759
public List<Backend> getBackends() {
5860
return backends;
5961
}
6062

61-
public String getBackend() {
62-
return backendQuery;
63+
public List<String> getBackendsQuery() {
64+
return backendsQuery;
6365
}
6466

6567
public ShowResultSetMetaData getMetaData() {
@@ -70,24 +72,28 @@ public ShowResultSetMetaData getMetaData() {
7072
return builder.build();
7173
}
7274

73-
private ShowResultSet handleShowTrash(String backendQuery) throws Exception {
75+
private ShowResultSet handleShowTrash(List<String> backendsQuery) throws Exception {
7476
if (!Env.getCurrentEnv().getAccessManager().checkGlobalPriv(ConnectContext.get(), PrivPredicate.ADMIN)
7577
&& !Env.getCurrentEnv().getAccessManager().checkGlobalPriv(ConnectContext.get(),
7678
PrivPredicate.OPERATOR)) {
7779
ErrorReport.reportAnalysisException(ErrorCode.ERR_SPECIFIC_ACCESS_DENIED_ERROR, "ADMIN/OPERATOR");
7880
}
7981
ImmutableMap<Long, Backend> backendsInfo = Env.getCurrentSystemInfo().getAllBackendsByAllCluster();
80-
if (backendQuery == null || backendQuery.isEmpty()) {
82+
if (backendsQuery == null) {
8183
for (Backend backend : backendsInfo.values()) {
8284
this.backends.add(backend);
8385
}
8486
} else {
87+
Map<String, Long> backendsID = new HashMap<>();
8588
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;
89+
backendsID.put(
90+
NetUtils.getHostPortInAccessibleFormat(backend.getHost(), backend.getHeartbeatPort()),
91+
backend.getId());
92+
}
93+
for (String backendQuery : backendsQuery) {
94+
if (backendsID.containsKey(backendQuery)) {
95+
this.backends.add(backendsInfo.get(backendsID.get(backendQuery)));
96+
backendsID.remove(backendQuery);
9197
}
9298
}
9399
}
@@ -103,7 +109,7 @@ public <R, C> R accept(PlanVisitor<R, C> visitor, C context) {
103109

104110
@Override
105111
public ShowResultSet doRun(ConnectContext ctx, StmtExecutor executor) throws Exception {
106-
return handleShowTrash(backendQuery);
112+
return handleShowTrash(backendsQuery);
107113
}
108114
}
109115

‎fe/fe-core/src/test/java/org/apache/doris/nereids/parser/NereidsParserTest.java‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
import org.apache.doris.nereids.trees.plans.commands.ExplainCommand;
5858
import org.apache.doris.nereids.trees.plans.commands.ExplainCommand.ExplainLevel;
5959
import org.apache.doris.nereids.trees.plans.commands.ReplayCommand;
60+
import org.apache.doris.nereids.trees.plans.commands.ShowTrashCommand;
6061
import org.apache.doris.nereids.trees.plans.commands.UpdateCommand;
6162
import org.apache.doris.nereids.trees.plans.commands.merge.MergeIntoCommand;
6263
import org.apache.doris.nereids.trees.plans.logical.LogicalAggregate;
@@ -1769,4 +1770,41 @@ public void testIsTrueAndIsFalseExpression() {
17691770
Assertions.assertInstanceOf(Not.class, expression);
17701771
Assertions.assertInstanceOf(IsFalse.class, expression.child(0));
17711772
}
1773+
1774+
@Test
1775+
public void testShowTrashCommand() {
1776+
NereidsParser nereidsParser = new NereidsParser();
1777+
1778+
// Case 1: SHOW TRASH without ON clause (backendsQuery should be null)
1779+
String sql1 = "SHOW TRASH";
1780+
Plan plan1 = nereidsParser.parseSingle(sql1);
1781+
Assertions.assertInstanceOf(ShowTrashCommand.class, plan1);
1782+
ShowTrashCommand cmd1 = (ShowTrashCommand) plan1;
1783+
Assertions.assertNull(cmd1.getBackendsQuery());
1784+
1785+
// Case 2: SHOW TRASH ON with single backend (new parenthesized syntax)
1786+
String sql2 = "SHOW TRASH ON (\"be1:9050\")";
1787+
Plan plan2 = nereidsParser.parseSingle(sql2);
1788+
Assertions.assertInstanceOf(ShowTrashCommand.class, plan2);
1789+
ShowTrashCommand cmd2 = (ShowTrashCommand) plan2;
1790+
1791+
Assertions.assertNotNull(cmd2.getBackendsQuery());
1792+
Assertions.assertEquals(1, cmd2.getBackendsQuery().size());
1793+
Assertions.assertEquals("be1:9050", cmd2.getBackendsQuery().get(0));
1794+
1795+
// Case 3: SHOW TRASH ON with multiple backends
1796+
String sql3 = "SHOW TRASH ON (\"be1:9050\", \"be2:9050\", \"be3:9050\")";
1797+
Plan plan3 = nereidsParser.parseSingle(sql3);
1798+
Assertions.assertInstanceOf(ShowTrashCommand.class, plan3);
1799+
ShowTrashCommand cmd3 = (ShowTrashCommand) plan3;
1800+
1801+
Assertions.assertNotNull(cmd3.getBackendsQuery());
1802+
Assertions.assertEquals(3, cmd3.getBackendsQuery().size());
1803+
Assertions.assertEquals(Lists.newArrayList("be1:9050", "be2:9050", "be3:9050"), cmd3.getBackendsQuery());
1804+
1805+
// Case 4: Verify old syntax (without parentheses) is rejected
1806+
String sql4 = "SHOW TRASH ON \"be1:9050\"";
1807+
Assertions.assertThrows(ParseException.class, () -> nereidsParser.parseSingle(sql4));
1808+
}
1809+
17721810
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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.Env;
21+
import org.apache.doris.common.proc.TrashProcDir;
22+
import org.apache.doris.mysql.privilege.AccessControllerManager;
23+
import org.apache.doris.qe.ConnectContext;
24+
import org.apache.doris.qe.StmtExecutor;
25+
import org.apache.doris.system.Backend;
26+
import org.apache.doris.system.SystemInfoService;
27+
28+
import com.google.common.collect.ImmutableMap;
29+
import com.google.common.collect.Lists;
30+
import org.junit.jupiter.api.AfterEach;
31+
import org.junit.jupiter.api.Assertions;
32+
import org.junit.jupiter.api.BeforeEach;
33+
import org.junit.jupiter.api.Test;
34+
import org.mockito.MockedStatic;
35+
import org.mockito.Mockito;
36+
37+
import java.util.List;
38+
39+
public class ShowTrashCommandTest {
40+
41+
private MockedStatic<Env> mockedEnvStatic;
42+
private MockedStatic<ConnectContext> mockedConnectContextStatic;
43+
private MockedStatic<TrashProcDir> mockedTrashProcDirStatic;
44+
45+
private SystemInfoService systemInfoService;
46+
private ConnectContext ctx;
47+
private StmtExecutor executor;
48+
private AccessControllerManager accessManager;
49+
50+
@BeforeEach
51+
public void setUp() {
52+
systemInfoService = Mockito.mock(SystemInfoService.class);
53+
ctx = Mockito.mock(ConnectContext.class);
54+
executor = Mockito.mock(StmtExecutor.class);
55+
accessManager = Mockito.mock(AccessControllerManager.class);
56+
mockedConnectContextStatic = Mockito.mockStatic(ConnectContext.class);
57+
mockedConnectContextStatic.when(ConnectContext::get).thenReturn(ctx);
58+
59+
Env fakeEnv = Mockito.mock(Env.class);
60+
Mockito.when(fakeEnv.getAccessManager()).thenReturn(accessManager);
61+
62+
Mockito.when(accessManager.checkGlobalPriv(Mockito.eq(ctx), Mockito.any())).thenReturn(true);
63+
64+
mockedEnvStatic = Mockito.mockStatic(Env.class, Mockito.CALLS_REAL_METHODS);
65+
mockedEnvStatic.when(Env::getCurrentEnv).thenReturn(fakeEnv);
66+
mockedEnvStatic.when(Env::getCurrentSystemInfo).thenReturn(systemInfoService);
67+
68+
mockedTrashProcDirStatic = Mockito.mockStatic(TrashProcDir.class);
69+
mockedTrashProcDirStatic.when(() -> TrashProcDir.getTrashInfo(
70+
Mockito.anyList(),
71+
Mockito.anyList()
72+
)).thenAnswer(invocation -> null);
73+
}
74+
75+
@AfterEach
76+
public void tearDown() {
77+
if (mockedEnvStatic != null) {
78+
mockedEnvStatic.close();
79+
}
80+
if (mockedConnectContextStatic != null) {
81+
mockedConnectContextStatic.close();
82+
}
83+
if (mockedTrashProcDirStatic != null) {
84+
mockedTrashProcDirStatic.close();
85+
}
86+
}
87+
88+
@Test
89+
public void testHandleShowTrashWithFilter() throws Exception {
90+
Backend be1 = new Backend(1001L, "192.168.1.1", 9050);
91+
Backend be2 = new Backend(1002L, "192.168.1.2", 9050);
92+
Backend be3 = new Backend(1003L, "192.168.1.3", 9050);
93+
ImmutableMap<Long, Backend> allBackendsMap = ImmutableMap.of(
94+
1001L, be1,
95+
1002L, be2,
96+
1003L, be3
97+
);
98+
Mockito.when(systemInfoService.getAllBackendsByAllCluster()).thenReturn(allBackendsMap);
99+
ShowTrashCommand command = new ShowTrashCommand(
100+
Lists.newArrayList("192.168.1.1:9050", "192.168.1.3:9050")
101+
);
102+
command.doRun(ctx, executor);
103+
List<Backend> resultBackends = command.getBackends();
104+
Assertions.assertNotNull(resultBackends);
105+
Assertions.assertEquals(2, resultBackends.size());
106+
List<Long> resultIds = Lists.newArrayList(resultBackends.get(0).getId(), resultBackends.get(1).getId());
107+
Assertions.assertTrue(resultIds.contains(1001L));
108+
Assertions.assertTrue(resultIds.contains(1003L));
109+
Assertions.assertFalse(resultIds.contains(1002L));
110+
}
111+
112+
@Test
113+
public void testHandleShowTrashWithNonExistentBackend() throws Exception {
114+
Backend be1 = new Backend(1001L, "192.168.1.1", 9050);
115+
ImmutableMap<Long, Backend> allBackendsMap = ImmutableMap.of(1001L, be1);
116+
Mockito.when(systemInfoService.getAllBackendsByAllCluster()).thenReturn(allBackendsMap);
117+
ShowTrashCommand command = new ShowTrashCommand(
118+
Lists.newArrayList("192.168.1.1:9050", "192.168.99.99:9999")
119+
);
120+
command.doRun(ctx, executor);
121+
List<Backend> resultBackends = command.getBackends();
122+
Assertions.assertNotNull(resultBackends);
123+
Assertions.assertEquals(1, resultBackends.size());
124+
Assertions.assertEquals(1001L, resultBackends.get(0).getId());
125+
}
126+
127+
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,8 @@ supportedShowStatement
464464
| SHOW FULL? (COLUMNS | FIELDS) (FROM | IN) tableName=multipartIdentifier
465465
((FROM | IN) database=multipartIdentifier)? wildWhere? #showColumns
466466
| SHOW TABLE tableId=INTEGER_VALUE #showTableId
467-
| SHOW TRASH (ON backend=STRING_LITERAL)? #showTrash
467+
| SHOW TRASH (ON LEFT_PAREN backends+=STRING_LITERAL
468+
(COMMA backends+=STRING_LITERAL)* RIGHT_PAREN)? #showTrash
468469
| SHOW TYPECAST ((FROM | IN) database=identifier)? #showTypeCast
469470
| SHOW (CLUSTERS | (COMPUTE GROUPS)) #showClusters
470471
| SHOW statementScope? STATUS #showStatus

‎regression-test/suites/query_p0/ddl/show_trash/test_nereids_trash.groovy‎

Lines changed: 72 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,79 @@
1616
// under the License.
1717

1818
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";""")
19+
// Retrieve backend info dynamically to adapt to any environment (1 node or N nodes)
20+
def allBackends = sql """SHOW BACKENDS"""
21+
assertTrue(allBackends.size() > 0, "Test requires at least 1 backend in the cluster")
2222

23-
//cloud-mode
23+
// Extract HostPorts dynamically.
24+
// Based on actual SHOW BACKENDS output:
25+
// Index 1: Host (e.g., 172.17.0.1)
26+
// Index 2: HeartbeatPort (e.g., 9050) <--- This is the port used by SHOW TRASH!
27+
String firstBackendHost = allBackends[0][1].toString()
28+
String firstBackendPort = allBackends[0][2].toString()
29+
String firstBackendHostPort = "${firstBackendHost}:${firstBackendPort}"
30+
31+
String nonExistentBackend = "1.2.3.4:9999" // A non-existent backend address
32+
33+
// ========================================
34+
// Test 1: Verify backward compatibility (No filter)
35+
// ========================================
36+
def allTrashResults = sql """SHOW TRASH"""
37+
assertTrue(allTrashResults.size() == allBackends.size(),
38+
"Without filter, should return trash info for all backends. Expected: ${allBackends.size()}, Got: ${allTrashResults.size()}")
39+
40+
// ========================================
41+
// Test 2: Verify single backend filter
42+
// ========================================
43+
def singleResult = sql """SHOW TRASH ON ("${firstBackendHostPort}")"""
44+
assertTrue(singleResult.size() == 1,
45+
"With single backend filter, should return exactly 1 row. Expected: 1, Got: ${singleResult.size()}")
46+
47+
// Verify the returned backend matches the requested one
48+
// FIX: Column 1 is the 'Backend' string (IP:Port), not Column 0 (BackendId)
49+
assertTrue(singleResult[0][1].toString() == firstBackendHostPort,
50+
"The returned backend should match the requested one. Expected: ${firstBackendHostPort}, Got: ${singleResult[0][1]}")
51+
52+
// ========================================
53+
// Test 3: Verify filter with non-existent backend
54+
// ========================================
55+
def mixedResults = sql """SHOW TRASH ON ("${firstBackendHostPort}", "${nonExistentBackend}")"""
56+
assertTrue(mixedResults.size() == 1,
57+
"With one existent and one non-existent backend, should return only 1 row. Expected: 1, Got: ${mixedResults.size()}")
58+
59+
// ========================================
60+
// Test 4: Verify result with all non-existent backends
61+
// ========================================
62+
def emptyResults = sql """SHOW TRASH ON ("${nonExistentBackend}", "5.6.7.8:9999")"""
63+
assertTrue(emptyResults.size() == 0,
64+
"With all non-existent backends, should return empty result. Expected: 0, Got: ${emptyResults.size()}")
65+
66+
// ========================================
67+
// Test 5: Verify filter for multiple backends (Only if >= 2 backends exist)
68+
// ========================================
69+
if (allBackends.size() >= 2) {
70+
// FIX: Use the same indices as Test 2 for consistency
71+
String secondBackendHost = allBackends[1][1].toString()
72+
String secondBackendPort = allBackends[1][2].toString()
73+
String secondBackendHostPort = "${secondBackendHost}:${secondBackendPort}"
74+
75+
def multiResults = sql """SHOW TRASH ON ("${firstBackendHostPort}", "${secondBackendHostPort}")"""
76+
assertTrue(multiResults.size() == 2,
77+
"With filter for 2 backends, should return exactly 2 rows. Expected: 2, Got: ${multiResults.size()}")
78+
79+
// Verify content
80+
// FIX: Column 1 is the 'Backend' string, not Column 0
81+
def r1 = multiResults[0][1].toString()
82+
def r2 = multiResults[1][1].toString()
83+
assertTrue(
84+
(r1 == firstBackendHostPort && r2 == secondBackendHostPort) ||
85+
(r1 == secondBackendHostPort && r2 == firstBackendHostPort),
86+
"Returned backends should match the requested ones. Expected: [${firstBackendHostPort}, ${secondBackendHostPort}] in any order, Got: [${r1}, ${r2}]"
87+
)
88+
}
89+
90+
// cloud-mode checks
2491
if (isCloudMode()) {
25-
return
92+
// Add cloud mode specific logic here
2693
}
27-
checkNereidsExecute("""ADMIN CLEAN TRASH;""")
28-
checkNereidsExecute("""ADMIN CLEAN TRASH ON ("127.0.0.1:9050");""")
29-
checkNereidsExecute("""ADMIN CLEAN TRASH ON ("192.168.0.1:9050", "192.168.0.2:9050", "192.168.0.3:9050");""")
3094
}

0 commit comments

Comments
 (0)