|
18 | 18 | package org.apache.doris.nereids.trees.plans.commands; |
19 | 19 |
|
20 | 20 | 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; |
21 | 24 | import org.apache.doris.common.AnalysisException; |
| 25 | +import org.apache.doris.datasource.CatalogIf; |
| 26 | +import org.apache.doris.datasource.CatalogMgr; |
22 | 27 | import org.apache.doris.datasource.InternalCatalog; |
| 28 | +import org.apache.doris.mysql.privilege.AccessControllerManager; |
| 29 | +import org.apache.doris.mysql.privilege.PrivPredicate; |
23 | 30 | import org.apache.doris.nereids.trees.plans.PlanType; |
24 | 31 | import org.apache.doris.qe.ConnectContext; |
| 32 | +import org.apache.doris.qe.ShowResultSet; |
| 33 | +import org.apache.doris.qe.StmtExecutor; |
25 | 34 | import org.apache.doris.utframe.TestWithFeService; |
26 | 35 |
|
| 36 | +import com.google.common.collect.ImmutableSet; |
| 37 | +import com.google.common.collect.Lists; |
27 | 38 | import org.junit.jupiter.api.Assertions; |
28 | 39 | import org.junit.jupiter.api.Test; |
| 40 | +import org.mockito.MockedStatic; |
| 41 | +import org.mockito.Mockito; |
29 | 42 |
|
30 | 43 | import java.io.IOException; |
| 44 | +import java.util.List; |
31 | 45 |
|
32 | 46 | public class ShowTableCommandTest extends TestWithFeService { |
| 47 | + private static final String CATALOG_NAME = "hive_catalog"; |
| 48 | + private static final String DB_NAME = "hive_db"; |
| 49 | + |
33 | 50 | private ConnectContext ctx; |
34 | 51 |
|
35 | 52 | private void runBefore() throws IOException { |
@@ -63,4 +80,117 @@ void testInvalidate() throws Exception { |
63 | 80 | "", false, PlanType.SHOW_TABLES); |
64 | 81 | Assertions.assertThrows(AnalysisException.class, () -> command2.validate(ctx)); |
65 | 82 | } |
| 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 | + } |
66 | 196 | } |
0 commit comments