Skip to content

Commit ee635a8

Browse files
authored
[Feat](Nereids) support unset command (apache#43103)
### What problem does this PR solve? <!-- You need to clearly describe your PR in this part: migrate unset commands from original planner to nereids planner The description of the PR needs to enable reviewers to quickly and clearly understand the logic of the code modification. --> <!-- If there are related issues, please fill in the issue number. - If you want the issue to be closed after the PR is merged, please use "close apache#12345". Otherwise, use "ref apache#12345" --> Issue Number: close apache#43102 Problem Summary: ### Check List (For Committer) - Test <!-- At least one of them must be included. --> - [x] Regression test - [ ] Unit Test - [ ] Manual test (add detailed scripts or steps below) - [ ] No need to test or manual test. Explain why: - [ ] This is a refactor/code format and no logic has been changed. - [ ] Previous test can cover this change. - [ ] No colde files have been changed. - [ ] Other reason <!-- Add your reason? --> - Behavior changed: - [x] No. - [ ] Yes. <!-- Explain the behavior change --> - Does this need documentation? - [x] No. - [ ] Yes. <!-- Add document PR link here. eg: apache/doris-website#1214 --> - Release note <!-- bugfix, feat, behavior changed need a release note --> <!-- Add one line release note for this PR. --> None ### Check List (For Reviewer who merge this PR) - [x] Confirm the release note - [x] Confirm test cases - [x] Confirm document - [ ] Add branch pick label <!-- Add branch pick label that this PR should merge into -->
1 parent 800f5c6 commit ee635a8

9 files changed

Lines changed: 523 additions & 3 deletions

File tree

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@ statementBase
5454
| constraintStatement #constraintStatementAlias
5555
| supportedDropStatement #supportedDropStatementAlias
5656
| supportedSetStatement #supportedSetStatementAlias
57+
| supportedUnsetStatement #supportedUnsetStatementAlias
5758
| unsupportedStatement #unsupported
5859
;
5960

6061
unsupportedStatement
61-
: unsupoortedUnsetStatement
62-
| unsupportedUseStatement
62+
: unsupportedUseStatement
6363
| unsupportedDmlStatement
6464
| unsupportedKillStatement
6565
| unsupportedDescribeStatement
@@ -839,7 +839,7 @@ isolationLevel
839839
: ISOLATION LEVEL ((READ UNCOMMITTED) | (READ COMMITTED) | (REPEATABLE READ) | (SERIALIZABLE))
840840
;
841841

842-
unsupoortedUnsetStatement
842+
supportedUnsetStatement
843843
: UNSET (GLOBAL | SESSION | LOCAL)? VARIABLE (ALL | identifier)
844844
| UNSET DEFAULT STORAGE VAULT
845845
;

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@
206206
import org.apache.doris.nereids.DorisParser.StructLiteralContext;
207207
import org.apache.doris.nereids.DorisParser.SubqueryContext;
208208
import org.apache.doris.nereids.DorisParser.SubqueryExpressionContext;
209+
import org.apache.doris.nereids.DorisParser.SupportedUnsetStatementContext;
209210
import org.apache.doris.nereids.DorisParser.SystemVariableContext;
210211
import org.apache.doris.nereids.DorisParser.TableAliasContext;
211212
import org.apache.doris.nereids.DorisParser.TableNameContext;
@@ -422,6 +423,8 @@
422423
import org.apache.doris.nereids.trees.plans.commands.ShowCreateMTMVCommand;
423424
import org.apache.doris.nereids.trees.plans.commands.ShowCreateProcedureCommand;
424425
import org.apache.doris.nereids.trees.plans.commands.ShowProcedureStatusCommand;
426+
import org.apache.doris.nereids.trees.plans.commands.UnsetDefaultStorageVaultCommand;
427+
import org.apache.doris.nereids.trees.plans.commands.UnsetVariableCommand;
425428
import org.apache.doris.nereids.trees.plans.commands.UnsupportedCommand;
426429
import org.apache.doris.nereids.trees.plans.commands.UpdateCommand;
427430
import org.apache.doris.nereids.trees.plans.commands.info.AlterMTMVInfo;
@@ -3834,6 +3837,25 @@ public Object visitUnsupported(UnsupportedContext ctx) {
38343837
return UnsupportedCommand.INSTANCE;
38353838
}
38363839

3840+
@Override
3841+
public LogicalPlan visitSupportedUnsetStatement(SupportedUnsetStatementContext ctx) {
3842+
if (ctx.DEFAULT() != null && ctx.STORAGE() != null && ctx.VAULT() != null) {
3843+
return new UnsetDefaultStorageVaultCommand();
3844+
}
3845+
SetType type = SetType.DEFAULT;
3846+
if (ctx.GLOBAL() != null) {
3847+
type = SetType.GLOBAL;
3848+
} else if (ctx.LOCAL() != null || ctx.SESSION() != null) {
3849+
type = SetType.SESSION;
3850+
}
3851+
if (ctx.ALL() != null) {
3852+
return new UnsetVariableCommand(type, true);
3853+
} else if (ctx.identifier() != null) {
3854+
return new UnsetVariableCommand(type, ctx.identifier().getText());
3855+
}
3856+
throw new AnalysisException("Should add 'ALL' or variable name");
3857+
}
3858+
38373859
@Override
38383860
public LogicalPlan visitCreateTableLike(CreateTableLikeContext ctx) {
38393861
List<String> nameParts = visitMultipartIdentifier(ctx.name);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ public enum PlanType {
164164
ALTER_VIEW_COMMAND,
165165
ALTER_STORAGE_VAULT,
166166
DROP_CATALOG_RECYCLE_BIN_COMMAND,
167+
UNSET_VARIABLE_COMMAND,
168+
UNSET_DEFAULT_STORAGE_VAULT_COMMAND,
167169
UNSUPPORTED_COMMAND,
168170
CREATE_TABLE_LIKE_COMMAND,
169171
SET_OPTIONS_COMMAND,
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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.cloud.catalog.CloudEnv;
22+
import org.apache.doris.common.AnalysisException;
23+
import org.apache.doris.common.Config;
24+
import org.apache.doris.common.ErrorCode;
25+
import org.apache.doris.common.ErrorReport;
26+
import org.apache.doris.common.FeConstants;
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.StmtExecutor;
32+
33+
/**
34+
* Unset Default Storage Vault Command
35+
*/
36+
public class UnsetDefaultStorageVaultCommand extends Command implements ForwardWithSync {
37+
public UnsetDefaultStorageVaultCommand() {
38+
super(PlanType.UNSET_DEFAULT_STORAGE_VAULT_COMMAND);
39+
}
40+
41+
public String toSql() {
42+
final String stmt = "UNSET DEFAULT STORAGE VAULT";
43+
return stmt;
44+
}
45+
46+
@Override
47+
public void run(ConnectContext ctx, StmtExecutor executor) throws Exception {
48+
if (Config.isNotCloudMode()) {
49+
throw new AnalysisException("Storage Vault is only supported for cloud mode");
50+
}
51+
if (!FeConstants.runningUnitTest) {
52+
// In legacy cloud mode, some s3 back-ended storage does need to use storage vault.
53+
if (!((CloudEnv) Env.getCurrentEnv()).getEnableStorageVault()) {
54+
throw new AnalysisException("Your cloud instance doesn't support storage vault");
55+
}
56+
}
57+
58+
// check auth
59+
if (!Env.getCurrentEnv().getAccessManager().checkGlobalPriv(ConnectContext.get(), PrivPredicate.ADMIN)) {
60+
ErrorReport.reportAnalysisException(ErrorCode.ERR_SPECIFIC_ACCESS_DENIED_ERROR, "ADMIN");
61+
}
62+
63+
ctx.getEnv().getStorageVaultMgr().unsetDefaultStorageVault();
64+
}
65+
66+
@Override
67+
public <R, C> R accept(PlanVisitor<R, C> visitor, C context) {
68+
return visitor.visitUnsetDefaultStorageVaultCommand(this, context);
69+
}
70+
}
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
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.RedirectStatus;
21+
import org.apache.doris.analysis.SetType;
22+
import org.apache.doris.analysis.SetVar;
23+
import org.apache.doris.analysis.StringLiteral;
24+
import org.apache.doris.catalog.Env;
25+
import org.apache.doris.common.AnalysisException;
26+
import org.apache.doris.common.DdlException;
27+
import org.apache.doris.common.ErrorCode;
28+
import org.apache.doris.common.ErrorReport;
29+
import org.apache.doris.common.UserException;
30+
import org.apache.doris.mysql.privilege.PrivPredicate;
31+
import org.apache.doris.nereids.trees.plans.PlanType;
32+
import org.apache.doris.nereids.trees.plans.visitor.PlanVisitor;
33+
import org.apache.doris.qe.ConnectContext;
34+
import org.apache.doris.qe.StmtExecutor;
35+
import org.apache.doris.qe.VariableMgr;
36+
37+
import org.apache.commons.lang3.StringUtils;
38+
import org.apache.logging.log4j.LogManager;
39+
import org.apache.logging.log4j.Logger;
40+
41+
/**
42+
* UnSetVarOp
43+
*/
44+
public class UnsetVariableCommand extends Command implements Forward {
45+
private static final Logger LOG = LogManager.getLogger(StmtExecutor.class);
46+
47+
private SetType setType;
48+
49+
// variable to restore
50+
private String variable = null;
51+
52+
private boolean applyToAll = false;
53+
54+
public UnsetVariableCommand(SetType setType, String varName) {
55+
super(PlanType.UNSET_VARIABLE_COMMAND);
56+
this.setType = setType;
57+
this.variable = varName;
58+
}
59+
60+
public UnsetVariableCommand(SetType setType, boolean applyToAll) {
61+
super(PlanType.UNSET_VARIABLE_COMMAND);
62+
this.setType = setType;
63+
this.applyToAll = applyToAll;
64+
}
65+
66+
public SetType getSetType() {
67+
return setType;
68+
}
69+
70+
public String getVariable() {
71+
return variable;
72+
}
73+
74+
public boolean isApplyToAll() {
75+
return applyToAll;
76+
}
77+
78+
private void validate() throws UserException {
79+
if (StringUtils.isEmpty(variable) && !applyToAll) {
80+
throw new AnalysisException("You should specific the unset variable.");
81+
}
82+
83+
if (setType == SetType.GLOBAL) {
84+
if (!Env.getCurrentEnv().getAccessManager().checkGlobalPriv(ConnectContext.get(), PrivPredicate.ADMIN)) {
85+
ErrorReport.reportAnalysisException(ErrorCode.ERR_SPECIFIC_ACCESS_DENIED_ERROR,
86+
"ADMIN");
87+
}
88+
}
89+
}
90+
91+
/**
92+
* return sql expression of this command
93+
* @return string of this command
94+
*/
95+
public String toSql() {
96+
StringBuilder sb = new StringBuilder();
97+
98+
sb.append("UNSET ");
99+
sb.append(setType).append(" VARIABLE ");
100+
if (!StringUtils.isEmpty(variable)) {
101+
sb.append(variable).append(" ");
102+
} else if (applyToAll) {
103+
sb.append("ALL");
104+
}
105+
return sb.toString();
106+
}
107+
108+
@Override
109+
public RedirectStatus toRedirectStatus() {
110+
if (setType == SetType.GLOBAL) {
111+
return RedirectStatus.FORWARD_WITH_SYNC;
112+
}
113+
114+
return RedirectStatus.NO_FORWARD;
115+
}
116+
117+
@Override
118+
public void afterForwardToMaster(ConnectContext context) throws Exception {
119+
if (isApplyToAll()) {
120+
VariableMgr.setAllVarsToDefaultValue(context.getSessionVariable(), SetType.SESSION);
121+
} else {
122+
String defaultValue = VariableMgr.getDefaultValue(getVariable());
123+
if (defaultValue == null) {
124+
ErrorReport.reportDdlException(ErrorCode.ERR_UNKNOWN_SYSTEM_VARIABLE, getVariable());
125+
}
126+
SetVar var = new SetVar(SetType.SESSION, getVariable(),
127+
new StringLiteral(defaultValue), SetVar.SetVarType.SET_SESSION_VAR);
128+
VariableMgr.setVar(context.getSessionVariable(), var);
129+
}
130+
}
131+
132+
@Override
133+
public void run(ConnectContext ctx, StmtExecutor executor) throws Exception {
134+
validate();
135+
try {
136+
if (isApplyToAll()) {
137+
VariableMgr.setAllVarsToDefaultValue(ctx.getSessionVariable(), getSetType());
138+
} else {
139+
String defaultValue = VariableMgr.getDefaultValue(getVariable());
140+
if (defaultValue == null) {
141+
ErrorReport.reportDdlException(ErrorCode.ERR_UNKNOWN_SYSTEM_VARIABLE, getVariable());
142+
}
143+
SetVar var = new SetVar(getSetType(), getVariable(),
144+
new StringLiteral(defaultValue), SetVar.SetVarType.SET_SESSION_VAR);
145+
VariableMgr.setVar(ctx.getSessionVariable(), var);
146+
}
147+
} catch (DdlException e) {
148+
LOG.warn("", e);
149+
// Return error message to client.
150+
ctx.getState().setError(ErrorCode.ERR_LOCAL_VARIABLE, e.getMessage() + toSql());
151+
return;
152+
}
153+
ctx.getState().setOk();
154+
}
155+
156+
@Override
157+
public <R, C> R accept(PlanVisitor<R, C> visitor, C context) {
158+
return visitor.visitUnsetVariableCommand(this, context);
159+
}
160+
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@
5252
import org.apache.doris.nereids.trees.plans.commands.ShowCreateMTMVCommand;
5353
import org.apache.doris.nereids.trees.plans.commands.ShowCreateProcedureCommand;
5454
import org.apache.doris.nereids.trees.plans.commands.ShowProcedureStatusCommand;
55+
import org.apache.doris.nereids.trees.plans.commands.UnsetDefaultStorageVaultCommand;
56+
import org.apache.doris.nereids.trees.plans.commands.UnsetVariableCommand;
5557
import org.apache.doris.nereids.trees.plans.commands.UnsupportedCommand;
5658
import org.apache.doris.nereids.trees.plans.commands.UpdateCommand;
5759
import org.apache.doris.nereids.trees.plans.commands.insert.BatchInsertIntoTableCommand;
@@ -198,6 +200,15 @@ default R visitUnsupportedCommand(UnsupportedCommand unsupportedCommand, C conte
198200
return visitCommand(unsupportedCommand, context);
199201
}
200202

203+
default R visitUnsetVariableCommand(UnsetVariableCommand unsetVariableCommand, C context) {
204+
return visitCommand(unsetVariableCommand, context);
205+
}
206+
207+
default R visitUnsetDefaultStorageVaultCommand(UnsetDefaultStorageVaultCommand unsetDefaultStorageVaultCommand,
208+
C context) {
209+
return visitCommand(unsetDefaultStorageVaultCommand, context);
210+
}
211+
201212
default R visitCreateTableLikeCommand(CreateTableLikeCommand createTableLikeCommand, C context) {
202213
return visitCommand(createTableLikeCommand, context);
203214
}

0 commit comments

Comments
 (0)