Skip to content

Commit cad2f74

Browse files
committed
[improvement](catalog) add call-count tests for SHOW TABLES external fast path
What problem does this PR solve? Issue Number: close apache#66079 Problem Summary: Add unit tests for the non-verbose SHOW TABLES fast path on external catalogs, asserting the call counts of the key DatabaseIf interfaces so the branch routing cannot silently regress: - external catalog, non-verbose SHOW TABLES: getTableNamesOrEmptyWithLock() is called once and getTables() is never called; the listed names come back sorted and a table the user lacks SHOW privilege on is excluded. - external + verbose, external + SHOW VIEWS, and internal catalog: still take the original getTables() loop (getTables() once, getTableNamesOrEmptyWithLock() never). Release note: None
1 parent 6f15009 commit cad2f74

1 file changed

Lines changed: 130 additions & 0 deletions

File tree

‎fe/fe-core/src/test/java/org/apache/doris/nereids/trees/plans/commands/ShowTableCommandTest.java‎

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,35 @@
1818
package org.apache.doris.nereids.trees.plans.commands;
1919

2020
import org.apache.doris.backup.CatalogMocker;
21+
import org.apache.doris.catalog.DatabaseIf;
22+
import org.apache.doris.catalog.Env;
23+
import org.apache.doris.catalog.TableIf;
2124
import org.apache.doris.common.AnalysisException;
25+
import org.apache.doris.datasource.CatalogIf;
26+
import org.apache.doris.datasource.CatalogMgr;
2227
import org.apache.doris.datasource.InternalCatalog;
28+
import org.apache.doris.mysql.privilege.AccessControllerManager;
29+
import org.apache.doris.mysql.privilege.PrivPredicate;
2330
import org.apache.doris.nereids.trees.plans.PlanType;
2431
import org.apache.doris.qe.ConnectContext;
32+
import org.apache.doris.qe.ShowResultSet;
33+
import org.apache.doris.qe.StmtExecutor;
2534
import org.apache.doris.utframe.TestWithFeService;
2635

36+
import com.google.common.collect.ImmutableSet;
37+
import com.google.common.collect.Lists;
2738
import org.junit.jupiter.api.Assertions;
2839
import org.junit.jupiter.api.Test;
40+
import org.mockito.MockedStatic;
41+
import org.mockito.Mockito;
2942

3043
import java.io.IOException;
44+
import java.util.List;
3145

3246
public class ShowTableCommandTest extends TestWithFeService {
47+
private static final String CATALOG_NAME = "hive_catalog";
48+
private static final String DB_NAME = "hive_db";
49+
3350
private ConnectContext ctx;
3451

3552
private void runBefore() throws IOException {
@@ -63,4 +80,117 @@ void testInvalidate() throws Exception {
6380
"", false, PlanType.SHOW_TABLES);
6481
Assertions.assertThrows(AnalysisException.class, () -> command2.validate(ctx));
6582
}
83+
84+
/**
85+
* Bundle of mocks needed to drive {@link ShowTableCommand#doRun}: a mocked {@link ConnectContext}
86+
* whose {@code getEnv().getCatalogMgr().getCatalogOrAnalysisException(...).getDbOrAnalysisException(...)}
87+
* chain resolves to {@code dbIf}, wired to the given catalog (which decides whether
88+
* {@code dbIf.getCatalog() instanceof InternalCatalog} holds).
89+
*/
90+
private static final class ShowTableCommandMocks {
91+
private final ConnectContext ctx = Mockito.mock(ConnectContext.class);
92+
private final StmtExecutor executor = Mockito.mock(StmtExecutor.class);
93+
private final Env env = Mockito.mock(Env.class);
94+
private final AccessControllerManager accessControllerManager = Mockito.mock(AccessControllerManager.class);
95+
@SuppressWarnings("unchecked")
96+
private final DatabaseIf<TableIf> dbIf = Mockito.mock(DatabaseIf.class);
97+
98+
@SuppressWarnings("unchecked")
99+
ShowTableCommandMocks(CatalogIf<?> catalogIf) throws AnalysisException {
100+
CatalogMgr catalogMgr = Mockito.mock(CatalogMgr.class);
101+
Mockito.when(ctx.getEnv()).thenReturn(env);
102+
Mockito.when(env.getCatalogMgr()).thenReturn(catalogMgr);
103+
Mockito.when(env.getAccessManager()).thenReturn(accessControllerManager);
104+
Mockito.when(catalogMgr.getCatalogOrAnalysisException(Mockito.anyString())).thenReturn(catalogIf);
105+
Mockito.when(catalogIf.getDbOrAnalysisException(Mockito.anyString())).thenReturn(dbIf);
106+
Mockito.when(dbIf.getCatalog()).thenReturn(catalogIf);
107+
Mockito.when(dbIf.getFullName()).thenReturn(DB_NAME);
108+
}
109+
}
110+
111+
private static ShowResultSet runDoRun(ShowTableCommandMocks mocks, ShowTableCommand command) throws Exception {
112+
try (MockedStatic<Env> mockedEnv = Mockito.mockStatic(Env.class);
113+
MockedStatic<ConnectContext> mockedConnectContext = Mockito.mockStatic(ConnectContext.class)) {
114+
mockedEnv.when(Env::getCurrentEnv).thenReturn(mocks.env);
115+
mockedConnectContext.when(ConnectContext::get).thenReturn(mocks.ctx);
116+
return command.doRun(mocks.ctx, mocks.executor);
117+
}
118+
}
119+
120+
@Test
121+
public void testExternalCatalogNonVerboseShowTablesUsesFastPath() throws Exception {
122+
CatalogIf<?> catalogIf = Mockito.mock(CatalogIf.class);
123+
ShowTableCommandMocks mocks = new ShowTableCommandMocks(catalogIf);
124+
Mockito.when(mocks.dbIf.getTableNamesWithLock())
125+
.thenReturn(ImmutableSet.of("t2", "t1", "t_filtered_out"));
126+
// Every table is visible except "t_filtered_out", which SHOW privilege denies.
127+
Mockito.when(mocks.accessControllerManager.checkTblPriv(
128+
Mockito.eq(mocks.ctx), Mockito.eq(CATALOG_NAME), Mockito.eq(DB_NAME),
129+
Mockito.anyString(), Mockito.eq(PrivPredicate.SHOW)))
130+
.thenAnswer(invocation -> !"t_filtered_out".equals(invocation.getArgument(3)));
131+
132+
ShowTableCommand command = new ShowTableCommand(DB_NAME, CATALOG_NAME, false, PlanType.SHOW_TABLES);
133+
ShowResultSet result = runDoRun(mocks, command);
134+
135+
// Behavior: names come back sorted, and the privilege-denied table is excluded.
136+
List<List<String>> rows = result.getResultRows();
137+
Assertions.assertEquals(2, rows.size());
138+
Assertions.assertEquals(Lists.newArrayList("t1"), rows.get(0));
139+
Assertions.assertEquals(Lists.newArrayList("t2"), rows.get(1));
140+
141+
// Call counts: the fast path must list names, and must never load every table.
142+
// It uses getTableNamesWithLock() (not the exception-swallowing ...OrEmpty... variant)
143+
// so a name-conflict exception raised while listing is still propagated.
144+
Mockito.verify(mocks.dbIf, Mockito.times(1)).getTableNamesWithLock();
145+
Mockito.verify(mocks.dbIf, Mockito.times(0)).getTables();
146+
}
147+
148+
@Test
149+
public void testExternalCatalogVerboseShowTablesUsesSlowPath() throws Exception {
150+
CatalogIf<?> catalogIf = Mockito.mock(CatalogIf.class);
151+
ShowTableCommandMocks mocks = new ShowTableCommandMocks(catalogIf);
152+
Mockito.when(mocks.dbIf.getTables()).thenReturn(Lists.newArrayList());
153+
154+
// Verbose SHOW TABLES needs per-table metadata (storage format, etc.), so even on an
155+
// external catalog it must fall back to the slow path that loads every table.
156+
ShowTableCommand command = new ShowTableCommand(DB_NAME, CATALOG_NAME, true, PlanType.SHOW_TABLES);
157+
ShowResultSet result = runDoRun(mocks, command);
158+
159+
Assertions.assertTrue(result.getResultRows().isEmpty());
160+
Mockito.verify(mocks.dbIf, Mockito.times(1)).getTables();
161+
Mockito.verify(mocks.dbIf, Mockito.times(0)).getTableNamesWithLock();
162+
}
163+
164+
@Test
165+
public void testExternalCatalogShowViewsUsesSlowPath() throws Exception {
166+
CatalogIf<?> catalogIf = Mockito.mock(CatalogIf.class);
167+
ShowTableCommandMocks mocks = new ShowTableCommandMocks(catalogIf);
168+
Mockito.when(mocks.dbIf.getTables()).thenReturn(Lists.newArrayList());
169+
170+
// SHOW VIEWS needs the engine type of every table to filter views, so the name-only
171+
// fast path (guarded by PlanType.SHOW_TABLES) must not be taken here.
172+
ShowTableCommand command = new ShowTableCommand(DB_NAME, CATALOG_NAME, false, PlanType.SHOW_VIEWS);
173+
ShowResultSet result = runDoRun(mocks, command);
174+
175+
Assertions.assertTrue(result.getResultRows().isEmpty());
176+
Mockito.verify(mocks.dbIf, Mockito.times(1)).getTables();
177+
Mockito.verify(mocks.dbIf, Mockito.times(0)).getTableNamesWithLock();
178+
}
179+
180+
@Test
181+
public void testInternalCatalogNonVerboseShowTablesUsesSlowPath() throws Exception {
182+
InternalCatalog internalCatalog = Mockito.mock(InternalCatalog.class);
183+
ShowTableCommandMocks mocks = new ShowTableCommandMocks(internalCatalog);
184+
Mockito.when(mocks.dbIf.getTables()).thenReturn(Lists.newArrayList());
185+
186+
// The fast path is only for external catalogs; internal catalogs always take the
187+
// slow path regardless of verbosity.
188+
ShowTableCommand command = new ShowTableCommand(DB_NAME, InternalCatalog.INTERNAL_CATALOG_NAME, false,
189+
PlanType.SHOW_TABLES);
190+
ShowResultSet result = runDoRun(mocks, command);
191+
192+
Assertions.assertTrue(result.getResultRows().isEmpty());
193+
Mockito.verify(mocks.dbIf, Mockito.times(1)).getTables();
194+
Mockito.verify(mocks.dbIf, Mockito.times(0)).getTableNamesWithLock();
195+
}
66196
}

0 commit comments

Comments
 (0)