[feature](cmd) add UNSET_VARIABLE statement to set back variables (#27552)

This commit is contained in:
Yulei-Yang
2023-11-27 20:30:04 +08:00
committed by GitHub
parent 36a528b6bc
commit 3d7d166355
7 changed files with 418 additions and 1 deletions

View File

@ -631,6 +631,7 @@ terminal String
KW_UNION,
KW_UNIQUE,
KW_UNLOCK,
KW_UNSET,
KW_UNSIGNED,
KW_UPDATE,
KW_USE,
@ -639,6 +640,7 @@ terminal String
KW_VALUE,
KW_VALUES,
KW_VARCHAR,
KW_VARIABLE,
KW_VARIABLES,
KW_VERBOSE,
KW_VERSION,
@ -683,7 +685,7 @@ nonterminal List<StatementBase> stmts;
nonterminal StatementBase stmt, show_stmt, show_param, help_stmt, load_stmt,
create_routine_load_stmt, pause_routine_load_stmt, resume_routine_load_stmt, stop_routine_load_stmt,
show_routine_load_stmt, show_routine_load_task_stmt, show_create_routine_load_stmt, show_create_load_stmt, show_create_reporitory_stmt,
describe_stmt, alter_stmt,
describe_stmt, alter_stmt, unset_var_stmt,
create_job_stmt,pause_job_stmt,resume_job_stmt,stop_job_stmt,show_job_stmt,
use_stmt, kill_stmt, drop_stmt, recover_stmt, grant_stmt, revoke_stmt, create_stmt, set_stmt, sync_stmt, cancel_stmt, cancel_param, delete_stmt,
switch_stmt, transaction_stmt, unsupported_stmt, export_stmt, admin_stmt, truncate_stmt,
@ -1109,6 +1111,8 @@ stmt ::=
{: RESULT = use; :}
| set_stmt:set
{: RESULT = set; :}
| unset_var_stmt:stmt
{: RESULT = stmt; :}
| kill_stmt:kill
{: RESULT = kill; :}
| kill_analysis_job_stmt: k
@ -4935,6 +4939,18 @@ set_stmt ::=
:}
;
// Unset variable statement
unset_var_stmt ::=
KW_UNSET opt_var_type:type KW_VARIABLE variable_name:variable
{:
RESULT = new UnsetVariableStmt(type, variable);
:}
| KW_UNSET opt_var_type:type KW_VARIABLE KW_ALL
{:
RESULT = new UnsetVariableStmt(type, true);
:}
;
user_property_list ::=
user_property:property
{:
@ -7701,6 +7717,8 @@ keyword ::=
{: RESULT = id; :}
| KW_USER:id
{: RESULT = id; :}
| KW_VARIABLE:id
{: RESULT = id; :}
| KW_VARIABLES:id
{: RESULT = id; :}
| KW_VALUE:id
@ -7807,6 +7825,8 @@ keyword ::=
{: RESULT = id; :}
| KW_CRON:id
{: RESULT = id; :}
| KW_UNSET:id
{: RESULT = id; :}
;
// Identifier that contain keyword

View File

@ -0,0 +1,97 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package org.apache.doris.analysis;
import org.apache.doris.common.AnalysisException;
import org.apache.doris.common.UserException;
import com.amazonaws.util.StringUtils;
// Unset variables statement
public class UnsetVariableStmt extends StatementBase {
private SetType setType;
// variables to restore
private String variable = null;
private boolean applyToAll = false;
public UnsetVariableStmt(SetType setType, String varName) {
this.setType = setType;
this.variable = varName;
}
public UnsetVariableStmt(SetType setType, boolean applyToAll) {
this.setType = setType;
this.applyToAll = applyToAll;
}
public SetType getSetType() {
return setType;
}
public String getVariable() {
return variable;
}
public boolean isApplyToAll() {
return applyToAll;
}
// change type global to session avoid to write in non-master node.
public void modifySetVarsForExecute() {
if (setType == SetType.GLOBAL) {
setType = SetType.SESSION;
}
}
@Override
public void analyze(Analyzer analyzer) throws UserException {
if (StringUtils.isNullOrEmpty(variable) && !applyToAll) {
throw new AnalysisException("You should specific the unset variable.");
}
}
@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;
}
}

View File

@ -57,6 +57,7 @@ import org.apache.doris.analysis.SelectStmt;
import org.apache.doris.analysis.SetOperationStmt;
import org.apache.doris.analysis.SetStmt;
import org.apache.doris.analysis.SetVar;
import org.apache.doris.analysis.SetVar.SetVarType;
import org.apache.doris.analysis.ShowStmt;
import org.apache.doris.analysis.SlotRef;
import org.apache.doris.analysis.SqlParser;
@ -72,6 +73,7 @@ import org.apache.doris.analysis.TransactionRollbackStmt;
import org.apache.doris.analysis.TransactionStmt;
import org.apache.doris.analysis.UnifiedLoadStmt;
import org.apache.doris.analysis.UnlockTablesStmt;
import org.apache.doris.analysis.UnsetVariableStmt;
import org.apache.doris.analysis.UnsupportedStmt;
import org.apache.doris.analysis.UpdateStmt;
import org.apache.doris.analysis.UseStmt;
@ -735,6 +737,8 @@ public class StmtExecutor {
handleQueryWithRetry(queryId);
} else if (parsedStmt instanceof SetStmt) {
handleSetStmt();
} else if (parsedStmt instanceof UnsetVariableStmt) {
handleUnsetVariableStmt();
} else if (parsedStmt instanceof SwitchStmt) {
handleSwitchStmt();
} else if (parsedStmt instanceof UseStmt) {
@ -1271,6 +1275,30 @@ public class StmtExecutor {
context.getState().setOk();
}
// Process unset variable statement.
private void handleUnsetVariableStmt() {
try {
UnsetVariableStmt unsetStmt = (UnsetVariableStmt) parsedStmt;
if (unsetStmt.isApplyToAll()) {
VariableMgr.setAllVarsToDefaultValue(context.getSessionVariable(), unsetStmt.getSetType());
} else {
String defaultValue = VariableMgr.getDefaultValue(unsetStmt.getVariable());
if (defaultValue == null) {
ErrorReport.reportDdlException(ErrorCode.ERR_UNKNOWN_SYSTEM_VARIABLE, unsetStmt.getVariable());
}
SetVar var = new SetVar(unsetStmt.getSetType(), unsetStmt.getVariable(),
new StringLiteral(defaultValue), SetVarType.SET_SESSION_VAR);
VariableMgr.setVar(context.getSessionVariable(), var);
}
} catch (DdlException e) {
LOG.warn("", e);
// Return error message to client.
context.getState().setError(ErrorCode.ERR_LOCAL_VARIABLE, e.getMessage());
return;
}
context.getState().setOk();
}
// send values from cache.
// return true if the meta fields has been sent, otherwise, return false.
// the meta fields must be sent right before the first batch of data(or eos flag).

View File

@ -20,6 +20,8 @@ package org.apache.doris.qe;
import org.apache.doris.analysis.LiteralExpr;
import org.apache.doris.analysis.SetType;
import org.apache.doris.analysis.SetVar;
import org.apache.doris.analysis.SetVar.SetVarType;
import org.apache.doris.analysis.StringLiteral;
import org.apache.doris.analysis.VariableExpr;
import org.apache.doris.catalog.Env;
import org.apache.doris.catalog.Type;
@ -648,6 +650,33 @@ public class VariableMgr {
return ImmutableMap.copyOf(result);
}
public static void setAllVarsToDefaultValue(SessionVariable sessionVariable, SetType setType)
throws DdlException {
for (Map.Entry<String, VarContext> entry : ctxByDisplayVarName.entrySet()) {
VarContext varCtx = entry.getValue();
SetType newSetType = null;
// some variables are GLOBAL only or SESSION only
if ((varCtx.getFlag() & GLOBAL) != 0) {
newSetType = SetType.GLOBAL;
} else if ((varCtx.getFlag() & SESSION_ONLY) != 0) {
newSetType = SetType.SESSION;
}
SetVar setVar = new SetVar(newSetType != null ? newSetType : setType, entry.getKey(),
new StringLiteral(varCtx.defaultValue), SetVarType.SET_SESSION_VAR);
//skip read only variables
if ((varCtx.getFlag() & READ_ONLY) == 0) {
setVar(sessionVariable, setVar);
}
}
}
public static String getDefaultValue(String key) {
VarContext varContext = ctxByDisplayVarName.get(key);
return varContext == null ? null : varContext.defaultValue;
}
// Dump all fields. Used for `show variables`
public static List<List<String>> dump(SetType type, SessionVariable sessionVar, PatternMatcher matcher) {
List<List<String>> rows = Lists.newArrayList();

View File

@ -514,6 +514,8 @@ import org.apache.doris.qe.SqlModeHelper;
keywordMap.put("expired", new Integer(SqlParserSymbols.KW_EXPIRED));
keywordMap.put("cron", new Integer(SqlParserSymbols.KW_CRON));
keywordMap.put("convert_light_schema_change_process", new Integer(SqlParserSymbols.KW_CONVERT_LSC));
keywordMap.put("unset", new Integer(SqlParserSymbols.KW_UNSET));
keywordMap.put("variable", new Integer(SqlParserSymbols.KW_VARIABLE));
}
// map from token id to token description

View File

@ -0,0 +1,157 @@
-- This file is automatically generated. You should know what you did if you want to edit this
-- !cmd --
0
-- !cmd --
wait_timeout 1000 28800 1
-- !cmd --
0
-- !cmd --
wait_timeout 28800 28800 0
-- !cmd --
0
-- !cmd --
runtime_filter_type BLOOM_FILTER 8 1
-- !cmd --
runtime_filter_type IN_OR_BLOOM_FILTER 8 1
-- !cmd --
0
-- !cmd --
runtime_filter_type IN_OR_BLOOM_FILTER 8 1
-- !cmd --
runtime_filter_type IN_OR_BLOOM_FILTER 8 1
-- !cmd --
0
-- !cmd --
runtime_filter_type BLOOM_FILTER 8 1
-- !cmd --
runtime_filter_type BLOOM_FILTER 8 1
-- !cmd --
0
-- !cmd --
runtime_filter_type IN_OR_BLOOM_FILTER 8 1
-- !cmd --
runtime_filter_type IN_OR_BLOOM_FILTER 8 1
-- !cmd --
0
-- !cmd --
experimental_enable_agg_state true - -
-- !cmd --
experimental_enable_agg_state false - -
-- !cmd --
0
-- !cmd --
experimental_enable_agg_state false - -
-- !cmd --
experimental_enable_agg_state false - -
-- !cmd --
0
-- !cmd --
experimental_enable_agg_state true - -
-- !cmd --
experimental_enable_agg_state true - -
-- !cmd --
0
-- !cmd --
experimental_enable_agg_state false - -
-- !cmd --
experimental_enable_agg_state false - -
-- !cmd --
0
-- !cmd --
deprecated_enable_local_exchange false - -
-- !cmd --
deprecated_enable_local_exchange true - -
-- !cmd --
0
-- !cmd --
deprecated_enable_local_exchange true - -
-- !cmd --
deprecated_enable_local_exchange true - -
-- !cmd --
0
-- !cmd --
0
-- !cmd --
0
-- !cmd --
0
-- !cmd --
0
-- !cmd --
runtime_filter_type IN_OR_BLOOM_FILTER 8 1
-- !cmd --
experimental_enable_agg_state false - -
-- !cmd --
deprecated_enable_local_exchange true - -
-- !cmd --
show_hidden_columns false false 0
-- !cmd --
0
-- !cmd --
0
-- !cmd --
0
-- !cmd --
0
-- !cmd --
0
-- !cmd --
runtime_filter_type IN_OR_BLOOM_FILTER 8 1
-- !cmd --
experimental_enable_agg_state false - -
-- !cmd --
deprecated_enable_local_exchange true - -
-- !cmd --
show_hidden_columns false false 0

View File

@ -0,0 +1,84 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
suite("set_and_unset_variable") {
qt_cmd """set wait_timeout = 1000"""
qt_cmd """show variables like 'wait_timeout'"""
qt_cmd """UNSET VARIABLE wait_timeout"""
qt_cmd """show variables like 'wait_timeout'"""
qt_cmd """set runtime_filter_type='BLOOM_FILTER'"""
qt_cmd """show session variables like 'runtime_filter_type'"""
qt_cmd """show global variables like 'runtime_filter_type'"""
qt_cmd """UNSET VARIABLE runtime_filter_type"""
qt_cmd """show session variables like 'runtime_filter_type'"""
qt_cmd """show global variables like 'runtime_filter_type'"""
qt_cmd """set global runtime_filter_type='BLOOM_FILTER'"""
qt_cmd """show session variables like 'runtime_filter_type'"""
qt_cmd """show global variables like 'runtime_filter_type'"""
qt_cmd """UNSET global VARIABLE runtime_filter_type"""
qt_cmd """show session variables like 'runtime_filter_type'"""
qt_cmd """show global variables like 'runtime_filter_type'"""
// test variables with experimental_ prefix in session scope
qt_cmd """set experimental_enable_agg_state='true'"""
qt_cmd """show session variables like 'experimental_enable_agg_state'"""
qt_cmd """show global variables like 'experimental_enable_agg_state'"""
qt_cmd """UNSET VARIABLE experimental_enable_agg_state"""
qt_cmd """show session variables like 'experimental_enable_agg_state'"""
qt_cmd """show global variables like 'experimental_enable_agg_state'"""
// test variables with experimental_ prefix in global scope
qt_cmd """set global experimental_enable_agg_state='true'"""
qt_cmd """show session variables like 'experimental_enable_agg_state'"""
qt_cmd """show global variables like 'experimental_enable_agg_state'"""
qt_cmd """UNSET global VARIABLE experimental_enable_agg_state"""
qt_cmd """show session variables like 'experimental_enable_agg_state'"""
qt_cmd """show global variables like 'experimental_enable_agg_state'"""
// test variables with deprecated_ prefix
qt_cmd """set deprecated_enable_local_exchange = false"""
qt_cmd """show session variables like 'deprecated_enable_local_exchange'"""
qt_cmd """show global variables like 'deprecated_enable_local_exchange'"""
qt_cmd """UNSET global VARIABLE deprecated_enable_local_exchange"""
qt_cmd """show session variables like 'deprecated_enable_local_exchange'"""
qt_cmd """show global variables like 'deprecated_enable_local_exchange'"""
// test UNSET VARIABLE ALL
qt_cmd """set runtime_filter_type='BLOOM_FILTER'"""
qt_cmd """set experimental_enable_agg_state='true'"""
qt_cmd """set deprecated_enable_local_exchange = false"""
qt_cmd """set show_hidden_columns=true"""
qt_cmd """UNSET VARIABLE ALL"""
qt_cmd """show session variables like 'runtime_filter_type'"""
qt_cmd """show session variables like 'experimental_enable_agg_state'"""
qt_cmd """show session variables like 'deprecated_enable_local_exchange'"""
qt_cmd """show session variables like 'show_hidden_columns'"""
// test UNSET GLOBAL VARIABLE ALL
qt_cmd """set global runtime_filter_type='BLOOM_FILTER'"""
qt_cmd """set global experimental_enable_agg_state='true'"""
qt_cmd """set global deprecated_enable_local_exchange = false"""
qt_cmd """set show_hidden_columns=true"""
qt_cmd """UNSET global VARIABLE ALL"""
qt_cmd """show global variables like 'runtime_filter_type'"""
qt_cmd """show global variables like 'experimental_enable_agg_state'"""
qt_cmd """show global variables like 'deprecated_enable_local_exchange'"""
qt_cmd """show global variables like 'show_hidden_columns'"""
}