Skip to content

Commit

Permalink
[Feat](Nereids) support unset command
Browse files Browse the repository at this point in the history
  • Loading branch information
LiBinfeng-01 committed Oct 30, 2024
1 parent e8e3fd7 commit 0a7db4f
Show file tree
Hide file tree
Showing 7 changed files with 224 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,13 @@ statementBase
| supportedJobStatement #supportedJobStatementAlias
| constraintStatement #constraintStatementAlias
| supportedDropStatement #supportedDropStatementAlias
| supportedUnsetVariableStatement #supportedUnsetVariableStatementAlias
| supportedUnsetDefaultStorageVaultStatement #supportedUnsetDefaultStorageVaultStatementAlias
| unsupportedStatement #unsupported
;

unsupportedStatement
: unsupportedSetStatement
| unsupoortedUnsetStatement
| unsupportedUseStatement
| unsupportedDmlStatement
| unsupportedKillStatement
Expand Down Expand Up @@ -839,9 +840,12 @@ isolationLevel
: ISOLATION LEVEL ((READ UNCOMMITTED) | (READ COMMITTED) | (REPEATABLE READ) | (SERIALIZABLE))
;

unsupoortedUnsetStatement
supportedUnsetVariableStatement
: UNSET (GLOBAL | SESSION | LOCAL)? VARIABLE (ALL | identifier)
| UNSET DEFAULT STORAGE VAULT
;

supportedUnsetDefaultStorageVaultStatement
: UNSET DEFAULT STORAGE VAULT
;

unsupportedUseStatement
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.doris.analysis.ArithmeticExpr.Operator;
import org.apache.doris.analysis.BrokerDesc;
import org.apache.doris.analysis.ColumnNullableType;
import org.apache.doris.analysis.SetType;
import org.apache.doris.analysis.StorageBackend;
import org.apache.doris.analysis.TableName;
import org.apache.doris.analysis.TableScanParams;
Expand Down Expand Up @@ -191,6 +192,8 @@
import org.apache.doris.nereids.DorisParser.StructLiteralContext;
import org.apache.doris.nereids.DorisParser.SubqueryContext;
import org.apache.doris.nereids.DorisParser.SubqueryExpressionContext;
import org.apache.doris.nereids.DorisParser.SupportedUnsetDefaultStorageVaultStatementContext;
import org.apache.doris.nereids.DorisParser.SupportedUnsetVariableStatementContext;
import org.apache.doris.nereids.DorisParser.SystemVariableContext;
import org.apache.doris.nereids.DorisParser.TableAliasContext;
import org.apache.doris.nereids.DorisParser.TableNameContext;
Expand Down Expand Up @@ -368,43 +371,9 @@
import org.apache.doris.nereids.trees.plans.PlanType;
import org.apache.doris.nereids.trees.plans.algebra.Aggregate;
import org.apache.doris.nereids.trees.plans.algebra.SetOperation.Qualifier;
import org.apache.doris.nereids.trees.plans.commands.AddConstraintCommand;
import org.apache.doris.nereids.trees.plans.commands.AlterMTMVCommand;
import org.apache.doris.nereids.trees.plans.commands.AlterStorageVaultCommand;
import org.apache.doris.nereids.trees.plans.commands.AlterViewCommand;
import org.apache.doris.nereids.trees.plans.commands.CallCommand;
import org.apache.doris.nereids.trees.plans.commands.CancelMTMVTaskCommand;
import org.apache.doris.nereids.trees.plans.commands.Command;
import org.apache.doris.nereids.trees.plans.commands.Constraint;
import org.apache.doris.nereids.trees.plans.commands.CreateJobCommand;
import org.apache.doris.nereids.trees.plans.commands.CreateMTMVCommand;
import org.apache.doris.nereids.trees.plans.commands.CreatePolicyCommand;
import org.apache.doris.nereids.trees.plans.commands.CreateProcedureCommand;
import org.apache.doris.nereids.trees.plans.commands.CreateTableCommand;
import org.apache.doris.nereids.trees.plans.commands.CreateTableLikeCommand;
import org.apache.doris.nereids.trees.plans.commands.CreateViewCommand;
import org.apache.doris.nereids.trees.plans.commands.DeleteFromCommand;
import org.apache.doris.nereids.trees.plans.commands.DeleteFromUsingCommand;
import org.apache.doris.nereids.trees.plans.commands.DropCatalogRecycleBinCommand;
import org.apache.doris.nereids.trees.plans.commands.*;
import org.apache.doris.nereids.trees.plans.commands.DropCatalogRecycleBinCommand.IdType;
import org.apache.doris.nereids.trees.plans.commands.DropConstraintCommand;
import org.apache.doris.nereids.trees.plans.commands.DropMTMVCommand;
import org.apache.doris.nereids.trees.plans.commands.DropProcedureCommand;
import org.apache.doris.nereids.trees.plans.commands.ExplainCommand;
import org.apache.doris.nereids.trees.plans.commands.ExplainCommand.ExplainLevel;
import org.apache.doris.nereids.trees.plans.commands.ExportCommand;
import org.apache.doris.nereids.trees.plans.commands.LoadCommand;
import org.apache.doris.nereids.trees.plans.commands.PauseMTMVCommand;
import org.apache.doris.nereids.trees.plans.commands.RefreshMTMVCommand;
import org.apache.doris.nereids.trees.plans.commands.ReplayCommand;
import org.apache.doris.nereids.trees.plans.commands.ResumeMTMVCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowConfigCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowConstraintsCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowCreateMTMVCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowCreateProcedureCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowProcedureStatusCommand;
import org.apache.doris.nereids.trees.plans.commands.UnsupportedCommand;
import org.apache.doris.nereids.trees.plans.commands.UpdateCommand;
import org.apache.doris.nereids.trees.plans.commands.info.AlterMTMVInfo;
import org.apache.doris.nereids.trees.plans.commands.info.AlterMTMVPropertyInfo;
import org.apache.doris.nereids.trees.plans.commands.info.AlterMTMVRefreshInfo;
Expand Down Expand Up @@ -3797,6 +3766,27 @@ public Object visitUnsupported(UnsupportedContext ctx) {
return UnsupportedCommand.INSTANCE;
}

@Override
public LogicalPlan visitSupportedUnsetVariableStatement(SupportedUnsetVariableStatementContext ctx) {
SetType type = SetType.DEFAULT;
if (ctx.GLOBAL() != null) {
type = SetType.GLOBAL;
} else if (ctx.LOCAL() != null || ctx.SESSION() != null) {
type = SetType.SESSION;
}
if (ctx.ALL() != null) {
return new UnsetVariableCommand(type, true);
} else if (ctx.identifier() != null) {
return new UnsetVariableCommand(type, ctx.identifier().getText());
}
return null;
}

@Override
public LogicalPlan visitSupportedUnsetDefaultStorageVaultStatement(SupportedUnsetDefaultStorageVaultStatementContext ctx) {
return new UnsetDefaultStorageVaultCommand();
}

@Override
public LogicalPlan visitCreateTableLike(CreateTableLikeContext ctx) {
List<String> nameParts = visitMultipartIdentifier(ctx.name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ public enum PlanType {
ALTER_VIEW_COMMAND,
ALTER_STORAGE_VAULT,
DROP_CATALOG_RECYCLE_BIN_COMMAND,
UNSET_VARIABLE_COMMAND,
UNSET_DEFAULT_STORAGE_VAULT_COMMAND,
UNSUPPORTED_COMMAND,
CREATE_TABLE_LIKE_COMMAND,

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package org.apache.doris.nereids.trees.plans.commands;

import org.apache.doris.analysis.Analyzer;
import org.apache.doris.catalog.Env;
import org.apache.doris.cloud.catalog.CloudEnv;
import org.apache.doris.common.*;
import org.apache.doris.mysql.privilege.PrivPredicate;
import org.apache.doris.nereids.trees.plans.PlanType;
import org.apache.doris.nereids.trees.plans.visitor.PlanVisitor;
import org.apache.doris.qe.ConnectContext;
import org.apache.doris.qe.StmtExecutor;

public class UnsetDefaultStorageVaultCommand extends Command implements ForwardWithSync {
public UnsetDefaultStorageVaultCommand() {
super(PlanType.UNSET_DEFAULT_STORAGE_VAULT_COMMAND);

}

// @Override
// public void analyze(Analyzer analyzer) throws UserException {
// if (Config.isNotCloudMode()) {
// throw new AnalysisException("Storage Vault is only supported for cloud mode");
// }
// if (!FeConstants.runningUnitTest) {
// // In legacy cloud mode, some s3 back-ended storage does need to use storage vault.
// if (!((CloudEnv) Env.getCurrentEnv()).getEnableStorageVault()) {
// throw new AnalysisException("Your cloud instance doesn't support storage vault");
// }
// }
//
// // check auth
// if (!Env.getCurrentEnv().getAccessManager().checkGlobalPriv(ConnectContext.get(), PrivPredicate.ADMIN)) {
// ErrorReport.reportAnalysisException(ErrorCode.ERR_SPECIFIC_ACCESS_DENIED_ERROR, "ADMIN");
// }
//
// super.analyze(analyzer);
// }
//
// @Override
// public String toSql() {
// final String stmt = "UNSET DEFAULT STORAGE VAULT";
// return stmt;
// }

@Override
public void run(ConnectContext ctx, StmtExecutor executor) throws Exception {

}

@Override
public <R, C> R accept(PlanVisitor<R, C> visitor, C context) {
return visitor.visitUnsetDefaultStorageVaultCommand(this, context);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package org.apache.doris.nereids.trees.plans.commands;

import com.amazonaws.util.StringUtils;
import org.apache.doris.analysis.SetType;
import org.apache.doris.analysis.SetVar;
import org.apache.doris.analysis.StringLiteral;
import org.apache.doris.catalog.Env;
import org.apache.doris.common.*;
import org.apache.doris.mysql.privilege.PrivPredicate;
import org.apache.doris.nereids.trees.plans.PlanType;
import org.apache.doris.nereids.trees.plans.visitor.PlanVisitor;
import org.apache.doris.qe.ConnectContext;
import org.apache.doris.qe.StmtExecutor;
import org.apache.doris.qe.VariableMgr;

import static org.apache.hadoop.fs.FileSystem.LOG;

public class UnsetVariableCommand extends Command implements ForwardWithSync {
private SetType setType;

// variables to restore
private String variable = null;

private boolean applyToAll = false;

public UnsetVariableCommand(SetType setType, String varName) {
super(PlanType.UNSET_VARIABLE_COMMAND);
this.setType = setType;
this.variable = varName;
}

public UnsetVariableCommand(SetType setType, boolean applyToAll) {
super(PlanType.UNSET_VARIABLE_COMMAND);
this.setType = setType;
this.applyToAll = applyToAll;
}

public SetType getSetType() {
return setType;
}

public String getVariable() {
return variable;
}

public boolean isApplyToAll() {
return applyToAll;
}

// todo: change type global to session avoid to write in non-master node.
// public void modifySetVarsForExecute() {
// if (setType == SetType.GLOBAL) {
// setType = SetType.SESSION;
// }
// }

private void validate() throws UserException {
if (StringUtils.isNullOrEmpty(variable) && !applyToAll) {
throw new AnalysisException("You should specific the unset variable.");
}

if (setType == SetType.GLOBAL) {
if (!Env.getCurrentEnv().getAccessManager().checkGlobalPriv(ConnectContext.get(), PrivPredicate.ADMIN)) {
ErrorReport.reportAnalysisException(ErrorCode.ERR_SPECIFIC_ACCESS_DENIED_ERROR,
"ADMIN");
}
}
}

// @Override
// public String toSql() {
// StringBuilder sb = new StringBuilder();
//
// sb.append("UNSET ");
// sb.append(setType).append(" VARIABLE ");
// if (!StringUtils.isNullOrEmpty(variable)) {
// sb.append(variable).append(" ");
// } else if (applyToAll) {
// sb.append("ALL");
// }
// return sb.toString();
// }
//
// @Override
// public String toString() {
// return toSql();
// }
//
// @Override
// public RedirectStatus getRedirectStatus() {
// if (setType == SetType.GLOBAL) {
// return RedirectStatus.FORWARD_WITH_SYNC;
// }
//
// return RedirectStatus.NO_FORWARD;
// }

@Override
public void run(ConnectContext ctx, StmtExecutor executor) throws Exception {
validate();
try {
if (isApplyToAll()) {
VariableMgr.setAllVarsToDefaultValue(ctx.getSessionVariable(), getSetType());
} else {
String defaultValue = VariableMgr.getDefaultValue(getVariable());
if (defaultValue == null) {
ErrorReport.reportDdlException(ErrorCode.ERR_UNKNOWN_SYSTEM_VARIABLE, getVariable());
}
SetVar var = new SetVar(getSetType(), getVariable(),
new StringLiteral(defaultValue), SetVar.SetVarType.SET_SESSION_VAR);
VariableMgr.setVar(ctx.getSessionVariable(), var);
}
} catch (DdlException e) {
LOG.warn("", e);
// Return error message to client.
ctx.getState().setError(ErrorCode.ERR_LOCAL_VARIABLE, e.getMessage());
return;
}
ctx.getState().setOk();
}

@Override
public <R, C> R accept(PlanVisitor<R, C> visitor, C context) {
return visitor.visitUnsetVariableCommand(this, context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,7 @@

package org.apache.doris.nereids.trees.plans.visitor;

import org.apache.doris.nereids.trees.plans.commands.AddConstraintCommand;
import org.apache.doris.nereids.trees.plans.commands.AlterMTMVCommand;
import org.apache.doris.nereids.trees.plans.commands.AlterViewCommand;
import org.apache.doris.nereids.trees.plans.commands.CallCommand;
import org.apache.doris.nereids.trees.plans.commands.CancelMTMVTaskCommand;
import org.apache.doris.nereids.trees.plans.commands.Command;
import org.apache.doris.nereids.trees.plans.commands.CreateJobCommand;
import org.apache.doris.nereids.trees.plans.commands.CreateMTMVCommand;
import org.apache.doris.nereids.trees.plans.commands.CreatePolicyCommand;
import org.apache.doris.nereids.trees.plans.commands.CreateProcedureCommand;
import org.apache.doris.nereids.trees.plans.commands.CreateTableCommand;
import org.apache.doris.nereids.trees.plans.commands.CreateTableLikeCommand;
import org.apache.doris.nereids.trees.plans.commands.CreateViewCommand;
import org.apache.doris.nereids.trees.plans.commands.DeleteFromCommand;
import org.apache.doris.nereids.trees.plans.commands.DeleteFromUsingCommand;
import org.apache.doris.nereids.trees.plans.commands.DropCatalogRecycleBinCommand;
import org.apache.doris.nereids.trees.plans.commands.DropConstraintCommand;
import org.apache.doris.nereids.trees.plans.commands.DropMTMVCommand;
import org.apache.doris.nereids.trees.plans.commands.DropProcedureCommand;
import org.apache.doris.nereids.trees.plans.commands.ExplainCommand;
import org.apache.doris.nereids.trees.plans.commands.ExportCommand;
import org.apache.doris.nereids.trees.plans.commands.LoadCommand;
import org.apache.doris.nereids.trees.plans.commands.PauseMTMVCommand;
import org.apache.doris.nereids.trees.plans.commands.RefreshMTMVCommand;
import org.apache.doris.nereids.trees.plans.commands.ReplayCommand;
import org.apache.doris.nereids.trees.plans.commands.ResumeMTMVCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowConfigCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowConstraintsCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowCreateMTMVCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowCreateProcedureCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowProcedureStatusCommand;
import org.apache.doris.nereids.trees.plans.commands.UnsupportedCommand;
import org.apache.doris.nereids.trees.plans.commands.UpdateCommand;
import org.apache.doris.nereids.trees.plans.commands.*;
import org.apache.doris.nereids.trees.plans.commands.insert.BatchInsertIntoTableCommand;
import org.apache.doris.nereids.trees.plans.commands.insert.InsertIntoTableCommand;
import org.apache.doris.nereids.trees.plans.commands.insert.InsertOverwriteTableCommand;
Expand Down Expand Up @@ -194,6 +162,14 @@ default R visitUnsupportedCommand(UnsupportedCommand unsupportedCommand, C conte
return visitCommand(unsupportedCommand, context);
}

default R visitUnsetVariableCommand(UnsetVariableCommand unsetVariableCommand, C context) {
return visitCommand(unsetVariableCommand, context);
}

default R visitUnsetDefaultStorageVaultCommand(UnsetDefaultStorageVaultCommand unsetDefaultStorageVaultCommand, C context) {
return visitCommand(unsetDefaultStorageVaultCommand, context);
}

default R visitCreateTableLikeCommand(CreateTableLikeCommand createTableLikeCommand, C context) {
return visitCommand(createTableLikeCommand, context);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1200,6 +1200,7 @@ private void forwardToMaster() throws Exception {
LOG.debug("need to transfer to Master. stmt: {}", context.getStmtId());
}
masterOpExecutor.execute();
// todo: need to deal with alter forward to master for unsetStmt
if (parsedStmt instanceof SetStmt) {
SetStmt setStmt = (SetStmt) parsedStmt;
setStmt.modifySetVarsForExecute();
Expand Down

0 comments on commit 0a7db4f

Please sign in to comment.