Skip to content

Commit 8df0b80

Browse files
authored
[Test](nereids) add ut for AnalyzeTableCommand (#49171)
1 parent ed37804 commit 8df0b80

File tree

2 files changed

+90
-1
lines changed

2 files changed

+90
-1
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,10 @@ public void check() throws AnalysisException {
159159
}
160160
}
161161

162-
private void checkAnalyzePrivilege(TableNameInfo tableNameInfo) throws AnalysisException {
162+
/**
163+
* checkAnalyzePrivilege
164+
*/
165+
public void checkAnalyzePrivilege(TableNameInfo tableNameInfo) throws AnalysisException {
163166
ConnectContext ctx = ConnectContext.get();
164167
// means it a system analyze
165168
if (ctx == null) {
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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.AnalyzeProperties;
21+
import org.apache.doris.backup.CatalogMocker;
22+
import org.apache.doris.catalog.Env;
23+
import org.apache.doris.common.AnalysisException;
24+
import org.apache.doris.datasource.InternalCatalog;
25+
import org.apache.doris.mysql.privilege.AccessControllerManager;
26+
import org.apache.doris.mysql.privilege.PrivPredicate;
27+
import org.apache.doris.nereids.trees.plans.commands.info.PartitionNamesInfo;
28+
import org.apache.doris.nereids.trees.plans.commands.info.TableNameInfo;
29+
import org.apache.doris.qe.ConnectContext;
30+
31+
import com.google.common.collect.ImmutableList;
32+
import mockit.Expectations;
33+
import mockit.Mocked;
34+
import org.junit.jupiter.api.Assertions;
35+
import org.junit.jupiter.api.Test;
36+
37+
public class AnalyzeTableCommandTest {
38+
private static final String internalCtl = InternalCatalog.INTERNAL_CATALOG_NAME;
39+
@Mocked
40+
private Env env;
41+
@Mocked
42+
private AccessControllerManager accessManager;
43+
@Mocked
44+
private ConnectContext ctx;
45+
46+
@Test
47+
void testCheckAnalyzePrivilege() {
48+
new Expectations() {
49+
{
50+
Env.getCurrentEnv();
51+
minTimes = 0;
52+
result = env;
53+
54+
env.getAccessManager();
55+
minTimes = 0;
56+
result = accessManager;
57+
58+
accessManager.checkTblPriv(ctx, internalCtl, CatalogMocker.TEST_DB_NAME, CatalogMocker.TEST_TBL_NAME,
59+
PrivPredicate.SELECT);
60+
minTimes = 0;
61+
result = true;
62+
63+
accessManager.checkTblPriv(ctx, internalCtl, CatalogMocker.TEST_DB_NAME, CatalogMocker.TEST_TBL2_NAME,
64+
PrivPredicate.SELECT);
65+
minTimes = 0;
66+
result = false;
67+
}
68+
};
69+
TableNameInfo tableNameInfo = new TableNameInfo(internalCtl,
70+
CatalogMocker.TEST_DB_NAME, CatalogMocker.TEST_TBL_NAME);
71+
PartitionNamesInfo partitionNamesInfo = new PartitionNamesInfo(false,
72+
ImmutableList.of(CatalogMocker.TEST_PARTITION1_NAME));
73+
AnalyzeTableCommand analyzeTableCommand = new AnalyzeTableCommand(tableNameInfo,
74+
partitionNamesInfo, ImmutableList.of("k1"), AnalyzeProperties.DEFAULT_PROP);
75+
// normal
76+
Assertions.assertDoesNotThrow(() -> analyzeTableCommand.checkAnalyzePrivilege(tableNameInfo));
77+
78+
// no privilege
79+
TableNameInfo tableNameInfo2 = new TableNameInfo(internalCtl,
80+
CatalogMocker.TEST_DB_NAME, CatalogMocker.TEST_TBL2_NAME);
81+
Assertions.assertThrows(AnalysisException.class,
82+
() -> analyzeTableCommand.checkAnalyzePrivilege(tableNameInfo2),
83+
"ANALYZE command denied to user 'null'@'null' for table 'test_db: test_tbl2'");
84+
}
85+
}
86+

0 commit comments

Comments
 (0)