Skip to content

Commit

Permalink
add case
Browse files Browse the repository at this point in the history
  • Loading branch information
starocean999 committed Oct 25, 2024
1 parent f010b34 commit 963a879
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3861,14 +3861,14 @@ public SetOptionsCommand visitSetOptions(SetOptionsContext ctx) {
List<SetVarOp> setVarOpList = new ArrayList<>(1);
for (Object child : ctx.children) {
if (child instanceof RuleNode) {
setVarOpList.add((SetVarOp) ((RuleNode) child).accept(this));
setVarOpList.add(typedVisit((RuleNode) child));
}
}
return new SetOptionsCommand(setVarOpList);
}

@Override
public SetSessionVarOp visitSetSystemVariable(SetSystemVariableContext ctx) {
public SetVarOp visitSetSystemVariable(SetSystemVariableContext ctx) {
SetType type = SetType.DEFAULT;
if (ctx.GLOBAL() != null) {
type = SetType.GLOBAL;
Expand All @@ -3881,7 +3881,7 @@ public SetSessionVarOp visitSetSystemVariable(SetSystemVariableContext ctx) {
}

@Override
public SetSessionVarOp visitSetVariableWithType(SetVariableWithTypeContext ctx) {
public SetVarOp visitSetVariableWithType(SetVariableWithTypeContext ctx) {
SetType type = SetType.DEFAULT;
if (ctx.GLOBAL() != null) {
type = SetType.GLOBAL;
Expand All @@ -3894,7 +3894,7 @@ public SetSessionVarOp visitSetVariableWithType(SetVariableWithTypeContext ctx)
}

@Override
public SetPassVarOp visitSetPassword(SetPasswordContext ctx) {
public SetVarOp visitSetPassword(SetPasswordContext ctx) {
String user;
String host;
boolean isDomain;
Expand All @@ -3913,32 +3913,32 @@ public SetPassVarOp visitSetPassword(SetPasswordContext ctx) {
}

@Override
public SetNamesVarOp visitSetNames(SetNamesContext ctx) {
public SetVarOp visitSetNames(SetNamesContext ctx) {
return new SetNamesVarOp();
}

@Override
public SetCharsetAndCollateVarOp visitSetCharset(SetCharsetContext ctx) {
public SetVarOp visitSetCharset(SetCharsetContext ctx) {
String charset = ctx.charsetName != null ? stripQuotes(ctx.charsetName.getText()) : null;
return new SetCharsetAndCollateVarOp(charset);
}

@Override
public SetCharsetAndCollateVarOp visitSetCollate(SetCollateContext ctx) {
public SetVarOp visitSetCollate(SetCollateContext ctx) {
String charset = ctx.charsetName != null ? stripQuotes(ctx.charsetName.getText()) : null;
String collate = ctx.collateName != null ? stripQuotes(ctx.collateName.getText()) : null;
return new SetCharsetAndCollateVarOp(charset, collate);
}

@Override
public SetLdapPassVarOp visitSetLdapAdminPassword(SetLdapAdminPasswordContext ctx) {
public SetVarOp visitSetLdapAdminPassword(SetLdapAdminPasswordContext ctx) {
String passwordText = stripQuotes(ctx.STRING_LITERAL().getText());
boolean isPlain = ctx.LEFT_PAREN() != null;
return new SetLdapPassVarOp(new PassVar(passwordText, isPlain));
}

@Override
public SetUserDefinedVarOp visitSetUserVariable(SetUserVariableContext ctx) {
public SetVarOp visitSetUserVariable(SetUserVariableContext ctx) {
String name = stripQuotes(ctx.identifier().getText());
Expression expression = typedVisit(ctx.expression());
return new SetUserDefinedVarOp(name, expression);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,5 +125,5 @@ public boolean needAuditEncryption() {
return false;
}

public void forwardToMaster(ConnectContext ctx) throws Exception {}
public void afterForwardToMaster(ConnectContext ctx) throws Exception {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// 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.nereids.trees.plans.commands;

/**
* NeedAuditEncryption
*/
public interface NeedAuditEncryption {

boolean needAuditEncryption();

String toSql();

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
/**
* SetOptionsCommand
*/
public class SetOptionsCommand extends Command implements ForwardWithSync {
public class SetOptionsCommand extends Command implements Forward, NeedAuditEncryption {
private final List<SetVarOp> setVarOpList;

public SetOptionsCommand(List<SetVarOp> setVarOpList) {
Expand Down Expand Up @@ -96,10 +96,10 @@ public String toSql() {
}

@Override
public void forwardToMaster(ConnectContext ctx) throws Exception {
public void afterForwardToMaster(ConnectContext ctx) throws Exception {
for (SetVarOp varOp : setVarOpList) {
varOp.validate(ctx);
varOp.forwardToMaster(ctx);
varOp.afterForwardToMaster(ctx);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,12 @@ public String toSql() {
return sb.toString();
}

public void forwardToMaster(ConnectContext ctx) throws Exception {
public void afterForwardToMaster(ConnectContext ctx) throws Exception {
setType(SetType.SESSION);
VariableMgr.setVarForNonMasterFE(ctx.getSessionVariable(), translateToLegacyVar(ctx));
}

// TODO delete this method after removing dependence of SetVar in VariableMgr
private SetVar translateToLegacyVar(ConnectContext ctx) {
LogicalEmptyRelation plan = new LogicalEmptyRelation(
ConnectContext.get().getStatementContext().getNextRelationId(), new ArrayList<>());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,5 @@ public boolean needAuditEncryption() {
return false;
}

public void forwardToMaster(ConnectContext ctx) throws Exception {}
public void afterForwardToMaster(ConnectContext ctx) throws Exception {}
}
10 changes: 6 additions & 4 deletions fe/fe-core/src/main/java/org/apache/doris/qe/AuditLogHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import org.apache.doris.nereids.analyzer.UnboundTableSink;
import org.apache.doris.nereids.glue.LogicalPlanAdapter;
import org.apache.doris.nereids.trees.plans.Plan;
import org.apache.doris.nereids.trees.plans.commands.Command;
import org.apache.doris.nereids.trees.plans.commands.NeedAuditEncryption;
import org.apache.doris.nereids.trees.plans.commands.insert.InsertIntoTableCommand;
import org.apache.doris.nereids.trees.plans.logical.LogicalInlineTable;
import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
Expand Down Expand Up @@ -262,9 +262,11 @@ private static void logAuditLogImpl(ConnectContext ctx, String origStmt, Stateme
// We put origin query stmt at the end of audit log, for parsing the log more convenient.
if (parsedStmt instanceof LogicalPlanAdapter) {
if (!ctx.getState().isQuery() && (parsedStmt != null
&& (((LogicalPlanAdapter) parsedStmt).getLogicalPlan() instanceof Command))
&& ((Command) ((LogicalPlanAdapter) parsedStmt).getLogicalPlan()).needAuditEncryption()) {
auditEventBuilder.setStmt(((Command) ((LogicalPlanAdapter) parsedStmt).getLogicalPlan()).toSql());
&& (((LogicalPlanAdapter) parsedStmt).getLogicalPlan() instanceof NeedAuditEncryption)
&& ((NeedAuditEncryption) ((LogicalPlanAdapter) parsedStmt).getLogicalPlan())
.needAuditEncryption())) {
auditEventBuilder
.setStmt(((NeedAuditEncryption) ((LogicalPlanAdapter) parsedStmt).getLogicalPlan()).toSql());
} else {
auditEventBuilder.setStmt(origStmt);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1204,7 +1204,7 @@ private void forwardToMaster() throws Exception {
// for nereids command
if (((LogicalPlanAdapter) parsedStmt).getLogicalPlan() instanceof Command) {
Command command = (Command) ((LogicalPlanAdapter) parsedStmt).getLogicalPlan();
command.forwardToMaster(context);
command.afterForwardToMaster(context);
}
} else if (parsedStmt instanceof SetStmt) {
SetStmt setStmt = (SetStmt) parsedStmt;
Expand Down
66 changes: 66 additions & 0 deletions regression-test/suites/query_p0/set/test_set_command.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// 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("test_set_command") {
// setSystemVariable
def default_value = sql """show variables where variable_name = 'insert_timeout';"""
sql """set insert_timeout=97531;"""
def modified_value = sql """show variables where variable_name = 'insert_timeout';"""
assertTrue(modified_value.toString().contains('97531'))
sql """set insert_timeout=DEFAULT;"""
def restored_value = sql """show variables where variable_name = 'insert_timeout';"""
assertEquals(default_value, restored_value)

default_value = sql """show variables where variable_name = 'insert_timeout';"""
sql """set @@insert_timeout=97531;"""
modified_value = sql """show variables where variable_name = 'insert_timeout';"""
assertTrue(modified_value.toString().contains('97531'))
sql """set @@insert_timeout=DEFAULT;"""
restored_value = sql """show variables where variable_name = 'insert_timeout';"""
assertEquals(default_value, restored_value)

default_value = sql """show variables where variable_name = 'insert_timeout';"""
sql """set @@session.insert_timeout=97531;"""
modified_value = sql """show variables where variable_name = 'insert_timeout';"""
assertTrue(modified_value.toString().contains('97531'))
sql """set @@session.insert_timeout=DEFAULT;"""
restored_value = sql """show variables where variable_name = 'insert_timeout';"""
assertEquals(default_value, restored_value)

// setVariableWithType
default_value = sql """show variables where variable_name = 'insert_timeout';"""
sql """set session insert_timeout=97531;"""
modified_value = sql """show variables where variable_name = 'insert_timeout';"""
assertTrue(modified_value.toString().contains('97531'))
sql """set session insert_timeout=DEFAULT;"""
restored_value = sql """show variables where variable_name = 'insert_timeout';"""
assertEquals(default_value, restored_value)

// setNames do nothing
sql """set names = utf8;"""

// setCollate do nothing
sql """set names default collate utf_8_ci;"""

// setTransaction do nothing
sql """set transaction read only;"""
sql """set transaction read write;"""

// setCharset do nothing
sql """set charset utf8;"""
sql """set charset default;"""
}

0 comments on commit 963a879

Please sign in to comment.