Skip to content

Commit

Permalink
[Enhancement] (nereids)implement DropEncryptKeyCommand in nereids (#4…
Browse files Browse the repository at this point in the history
…4506)

Issue Number: close #42623
  • Loading branch information
Vallishp authored Nov 26, 2024
1 parent 1b8f762 commit 95fdd45
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ supportedAlterStatement

supportedDropStatement
: DROP CATALOG RECYCLE BIN WHERE idType=STRING_LITERAL EQ id=INTEGER_VALUE #dropCatalogRecycleBin
| DROP ENCRYPTKEY (IF EXISTS)? name=multipartIdentifier #dropEncryptkey
| DROP ROLE (IF EXISTS)? name=identifier #dropRole
| DROP SQL_BLOCK_RULE (IF EXISTS)? identifierSeq #dropSqlBlockRule
| DROP USER (IF EXISTS)? userIdentify #dropUser
Expand Down Expand Up @@ -673,7 +674,6 @@ unsupportedDropStatement
| DROP INDEX (IF EXISTS)? name=identifier ON tableName=multipartIdentifier #dropIndex
| DROP RESOURCE (IF EXISTS)? name=identifierOrText #dropResource
| DROP WORKLOAD POLICY (IF EXISTS)? name=identifierOrText #dropWorkloadPolicy
| DROP ENCRYPTKEY (IF EXISTS)? name=multipartIdentifier #dropEncryptkey
| DROP ROW POLICY (IF EXISTS)? policyName=identifier
ON tableName=multipartIdentifier
(FOR (userIdentify | ROLE roleName=identifier))? #dropRowPolicy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.doris.common.io.Text;
import org.apache.doris.common.io.Writable;
import org.apache.doris.persist.gson.GsonUtils;
import org.apache.doris.qe.ConnectContext;

import com.google.common.base.Strings;
import com.google.gson.annotations.SerializedName;
Expand All @@ -34,6 +35,7 @@
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.List;
import java.util.Objects;

public class EncryptKeyName implements Writable {
Expand All @@ -52,6 +54,19 @@ public EncryptKeyName(String db, String keyName) {
}
}

/**
* EncryptKeyName
* @param parts like [db1,keyName] or [keyName]
*/
public EncryptKeyName(List<String> parts) {
int size = parts.size();
keyName = parts.get(size - 1);
keyName = keyName.toLowerCase();
if (size >= 2) {
db = parts.get(size - 2);
}
}

public EncryptKeyName(String keyName) {
this.db = null;
this.keyName = keyName.toLowerCase();
Expand All @@ -67,6 +82,16 @@ public void analyze(Analyzer analyzer) throws AnalysisException {
}
}

public void analyze(ConnectContext ctx) throws AnalysisException {
FeNameFormat.checkCommonName("EncryptKey", keyName);
if (db == null) {
db = ctx.getDatabase();
if (Strings.isNullOrEmpty(db)) {
ErrorReport.reportAnalysisException(ErrorCode.ERR_NO_DB_ERROR);
}
}
}

public String getDb() {
return db;
}
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.EncryptKeyName;
import org.apache.doris.analysis.PassVar;
import org.apache.doris.analysis.SetType;
import org.apache.doris.analysis.StorageBackend;
Expand Down Expand Up @@ -100,6 +101,7 @@
import org.apache.doris.nereids.DorisParser.DereferenceContext;
import org.apache.doris.nereids.DorisParser.DropCatalogRecycleBinContext;
import org.apache.doris.nereids.DorisParser.DropConstraintContext;
import org.apache.doris.nereids.DorisParser.DropEncryptkeyContext;
import org.apache.doris.nereids.DorisParser.DropMTMVContext;
import org.apache.doris.nereids.DorisParser.DropProcedureContext;
import org.apache.doris.nereids.DorisParser.DropRoleContext;
Expand Down Expand Up @@ -450,6 +452,7 @@
import org.apache.doris.nereids.trees.plans.commands.DropCatalogRecycleBinCommand;
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.DropEncryptkeyCommand;
import org.apache.doris.nereids.trees.plans.commands.DropJobCommand;
import org.apache.doris.nereids.trees.plans.commands.DropMTMVCommand;
import org.apache.doris.nereids.trees.plans.commands.DropProcedureCommand;
Expand Down Expand Up @@ -4375,6 +4378,12 @@ public LogicalPlan visitDropRole(DropRoleContext ctx) {
return new DropRoleCommand(ctx.name.getText(), ctx.EXISTS() != null);
}

@Override
public LogicalPlan visitDropEncryptkey(DropEncryptkeyContext ctx) {
List<String> nameParts = visitMultipartIdentifier(ctx.name);
return new DropEncryptkeyCommand(new EncryptKeyName(nameParts), ctx.EXISTS() != null);
}

@Override
public LogicalPlan visitDropSqlBlockRule(DropSqlBlockRuleContext ctx) {
return new DropSqlBlockRuleCommand(visitIdentifierSeq(ctx.identifierSeq()), ctx.EXISTS() != null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ public enum PlanType {
ALTER_STORAGE_VAULT,
ALTER_WORKLOAD_GROUP_COMMAND,
DROP_CATALOG_RECYCLE_BIN_COMMAND,
DROP_ENCRYPTKEY_COMMAND,
UNSET_VARIABLE_COMMAND,
UNSET_DEFAULT_STORAGE_VAULT_COMMAND,
UNSUPPORTED_COMMAND,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// 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;

import org.apache.doris.analysis.EncryptKeyName;
import org.apache.doris.catalog.Database;
import org.apache.doris.catalog.EncryptKeySearchDesc;
import org.apache.doris.catalog.Env;
import org.apache.doris.common.ErrorCode;
import org.apache.doris.common.ErrorReport;
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;

/**
* drop encrypt key command
*/
public class DropEncryptkeyCommand extends DropCommand {
private final boolean ifExists;
private final EncryptKeyName encryptKeyName;

/**
* constructor
*/
public DropEncryptkeyCommand(EncryptKeyName encryptKeyName, boolean ifExists) {
super(PlanType.DROP_ENCRYPTKEY_COMMAND);
this.encryptKeyName = encryptKeyName;
this.ifExists = ifExists;
}

@Override
public void doRun(ConnectContext ctx, StmtExecutor executor) throws Exception {
// check operation privilege
if (!Env.getCurrentEnv().getAccessManager().checkGlobalPriv(ConnectContext.get(), PrivPredicate.ADMIN)) {
ErrorReport.reportAnalysisException(ErrorCode.ERR_SPECIFIC_ACCESS_DENIED_ERROR, "ADMIN");
}
// analyze encryptkey name
encryptKeyName.analyze(ctx);
EncryptKeySearchDesc encryptKeySearchDesc = new EncryptKeySearchDesc(encryptKeyName);
Database db = Env.getCurrentInternalCatalog().getDbOrDdlException(encryptKeyName.getDb());
db.dropEncryptKey(encryptKeySearchDesc, ifExists);
}

@Override
public <R, C> R accept(PlanVisitor<R, C> visitor, C context) {
return visitor.visitDropEncryptKeyCommand(this, context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
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.DropEncryptkeyCommand;
import org.apache.doris.nereids.trees.plans.commands.DropJobCommand;
import org.apache.doris.nereids.trees.plans.commands.DropMTMVCommand;
import org.apache.doris.nereids.trees.plans.commands.DropProcedureCommand;
Expand Down Expand Up @@ -422,6 +423,10 @@ default R visitDropRoleCommand(DropRoleCommand dropRoleCommand, C context) {
return visitCommand(dropRoleCommand, context);
}

default R visitDropEncryptKeyCommand(DropEncryptkeyCommand dropEncryptkeyCommand, C context) {
return visitCommand(dropEncryptkeyCommand, context);
}

default R visitDropSqlBlockRuleCommand(DropSqlBlockRuleCommand dropSqlBlockRuleCommand, C context) {
return visitCommand(dropSqlBlockRuleCommand, context);
}
Expand Down
8 changes: 8 additions & 0 deletions regression-test/data/nereids_p0/test_nereids_encrypt_test.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- This file is automatically generated. You should know what you did if you want to edit this
-- !check_encrypt_1 --
test_nereids_encrypt_test_db.test_nereids_encrypt_test_key ABCD123456789

-- !check_encrypt_2 --

-- !check_encrypt_3 --

29 changes: 29 additions & 0 deletions regression-test/suites/nereids_p0/test_nereids_encrypt_test.groovy
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.
suite("test_nereids_encrypt_test") {
def dbName="test_nereids_encrypt_test_db"
def encryptkeyName="test_nereids_encrypt_test_key"
sql """ create database IF NOT EXISTS ${dbName}; """
sql """ use ${dbName}; """
checkNereidsExecute("drop encryptkey if exists ${encryptkeyName}")
sql """CREATE ENCRYPTKEY ${encryptkeyName} AS "ABCD123456789";"""
qt_check_encrypt_1("SHOW ENCRYPTKEYS FROM ${dbName}")
checkNereidsExecute("drop encryptkey ${encryptkeyName}")
qt_check_encrypt_2("SHOW ENCRYPTKEYS FROM ${dbName}")
checkNereidsExecute("drop encryptkey if exists ${encryptkeyName}")
qt_check_encrypt_3("SHOW ENCRYPTKEYS FROM ${dbName}")
}

0 comments on commit 95fdd45

Please sign in to comment.