|
| 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 | +} |
0 commit comments