Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,7 @@ UNINSTALL: 'UNINSTALL';
UNION: 'UNION';
UNIQUE: 'UNIQUE';
UNLOCK: 'UNLOCK';
UNSET: 'UNSET';
UNSIGNED: 'UNSIGNED';
UPDATE: 'UPDATE';
USE: 'USE';
Expand All @@ -534,6 +535,7 @@ USING: 'USING';
VALUE: 'VALUE';
VALUES: 'VALUES';
VARCHAR: 'VARCHAR';
VARIABLE: 'VARIABLE';
VARIABLES: 'VARIABLES';
VERBOSE: 'VERBOSE';
VERSION: 'VERSION';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -833,9 +833,11 @@ nonReserved
| TYPES
| UNCOMMITTED
| UNLOCK
| UNSET
| USER
| VALUE
| VARCHAR
| VARIABLE
| VARIABLES
| VERBOSE
| VERSION
Expand Down
22 changes: 21 additions & 1 deletion fe/fe-core/src/main/cup/sql_parser.cup
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,7 @@ terminal String
KW_UNION,
KW_UNIQUE,
KW_UNLOCK,
KW_UNSET,
KW_UNSIGNED,
KW_UPDATE,
KW_USE,
Expand All @@ -631,6 +632,7 @@ terminal String
KW_VALUE,
KW_VALUES,
KW_VARCHAR,
KW_VARIABLE,
KW_VARIABLES,
KW_VERBOSE,
KW_VERSION,
Expand Down Expand Up @@ -675,7 +677,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,
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,
import_columns_stmt, import_delete_on_stmt, import_sequence_stmt, import_where_stmt, install_plugin_stmt, uninstall_plugin_stmt,
Expand Down Expand Up @@ -1105,6 +1107,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
Expand Down Expand Up @@ -5006,6 +5010,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
{:
Expand Down Expand Up @@ -7757,6 +7773,8 @@ keyword ::=
{: RESULT = id; :}
| KW_USER:id
{: RESULT = id; :}
| KW_VARIABLE:id
{: RESULT = id; :}
| KW_VARIABLES:id
{: RESULT = id; :}
| KW_VALUE:id
Expand Down Expand Up @@ -7793,6 +7811,8 @@ keyword ::=
{: RESULT = id; :}
| KW_FREE:id
{: RESULT = id; :}
| KW_UNSET:id
{: RESULT = id; :}
| KW_TASK:id
{: RESULT = id; :}
| KW_ROUTINE:id
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
42 changes: 42 additions & 0 deletions fe/fe-core/src/main/java/org/apache/doris/qe/StmtExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@
import org.apache.doris.analysis.SelectStmt;
import org.apache.doris.analysis.SetOperationStmt;
import org.apache.doris.analysis.SetStmt;
import org.apache.doris.analysis.SetType;
import org.apache.doris.analysis.SetVar;
import org.apache.doris.analysis.SetVar.SetVarType;
import org.apache.doris.analysis.ShowStmt;
import org.apache.doris.analysis.SqlParser;
import org.apache.doris.analysis.SqlScanner;
Expand All @@ -73,6 +75,7 @@
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;
Expand Down Expand Up @@ -773,6 +776,8 @@ public void executeByLegacy(TUniqueId queryId) throws Exception {
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) {
Expand Down Expand Up @@ -918,6 +923,19 @@ private void forwardToMaster() throws Exception {
for (SetVar var : setStmt.getSetVars()) {
VariableMgr.setVarForNonMasterFE(context.getSessionVariable(), var);
}
} else if (parsedStmt instanceof UnsetVariableStmt) {
UnsetVariableStmt unsetStmt = (UnsetVariableStmt) parsedStmt;
if (unsetStmt.isApplyToAll()) {
VariableMgr.setAllVarsToDefaultValue(context.getSessionVariable(), SetType.SESSION);
} else {
String defaultValue = VariableMgr.getDefaultValue(unsetStmt.getVariable());
if (defaultValue == null) {
ErrorReport.reportDdlException(ErrorCode.ERR_UNKNOWN_SYSTEM_VARIABLE, unsetStmt.getVariable());
}
SetVar var = new SetVar(SetType.SESSION, unsetStmt.getVariable(),
new StringLiteral(defaultValue), SetVarType.SET_SESSION_VAR);
VariableMgr.setVar(context.getSessionVariable(), var);
}
}
}

Expand Down Expand Up @@ -1302,6 +1320,30 @@ private void handleSetStmt() {
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).
Expand Down
23 changes: 23 additions & 0 deletions fe/fe-core/src/main/java/org/apache/doris/qe/VariableMgr.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
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;
Expand Down Expand Up @@ -651,6 +653,27 @@ private static ImmutableMap<String, VarContext> getDisplaySessionVars() {
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();
SetVar setVar = new SetVar(setType, entry.getKey(),
new StringLiteral(varCtx.defaultValue), SetVarType.SET_SESSION_VAR);
try {
checkUpdate(setVar, varCtx.getFlag());
} catch (DdlException e) {
LOG.debug("no need to set var for non master fe: {}", setVar.getVariable(), e);
continue;
}
setVarInternal(sessionVariable, setVar, varCtx);
}
}

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();
Expand Down
2 changes: 2 additions & 0 deletions fe/fe-core/src/main/jflex/sql_scanner.flex
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,8 @@ import org.apache.doris.qe.SqlModeHelper;
keywordMap.put("ignore", new Integer(SqlParserSymbols.KW_IGNORE));
keywordMap.put("expired", new Integer(SqlParserSymbols.KW_EXPIRED));
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
Expand Down
Loading