Skip to content

Commit

Permalink
[Enhancement] (nereids)implement showEncryptKeysCommand in nereids (#…
Browse files Browse the repository at this point in the history
…44717)

Issue Number: close #42774
  • Loading branch information
msridhar78 authored Dec 3, 2024
1 parent ffd25c4 commit 43390ac
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,8 @@ supportedShowStatement
((FROM | IN) database=identifier)? #showView
| SHOW PLUGINS #showPlugins
| SHOW REPOSITORIES #showRepositories
| SHOW ENCRYPTKEYS ((FROM | IN) database=multipartIdentifier)?
(LIKE STRING_LITERAL)? #showEncryptKeys
| SHOW BRIEF? CREATE TABLE name=multipartIdentifier #showCreateTable
| SHOW FULL? PROCESSLIST #showProcessList
| SHOW ROLES #showRoles
Expand Down Expand Up @@ -352,7 +354,6 @@ unsupportedShowStatement
| SHOW TRANSACTION ((FROM | IN) database=multipartIdentifier)? wildWhere? #showTransaction
| SHOW QUERY PROFILE queryIdPath=STRING_LITERAL #showQueryProfile
| SHOW CACHE HOTSPOT tablePath=STRING_LITERAL #showCacheHotSpot
| SHOW ENCRYPTKEYS ((FROM | IN) database=multipartIdentifier)? wildWhere? #showEncryptKeys
| SHOW SYNC JOB ((FROM | IN) database=multipartIdentifier)? #showSyncJob
| SHOW TABLE CREATION ((FROM | IN) database=multipartIdentifier)? wildWhere? #showTableCreation
| SHOW CATALOG RECYCLE BIN wildWhere? #showCatalogRecycleBin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@
import org.apache.doris.nereids.DorisParser.ShowDeleteContext;
import org.apache.doris.nereids.DorisParser.ShowDiagnoseTabletContext;
import org.apache.doris.nereids.DorisParser.ShowDynamicPartitionContext;
import org.apache.doris.nereids.DorisParser.ShowEncryptKeysContext;
import org.apache.doris.nereids.DorisParser.ShowEventsContext;
import org.apache.doris.nereids.DorisParser.ShowFrontendsContext;
import org.apache.doris.nereids.DorisParser.ShowGrantsContext;
Expand Down Expand Up @@ -538,6 +539,7 @@
import org.apache.doris.nereids.trees.plans.commands.ShowDeleteCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowDiagnoseTabletCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowDynamicPartitionCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowEncryptKeysCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowEventsCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowFrontendsCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowGrantsCommand;
Expand Down Expand Up @@ -1903,6 +1905,21 @@ public static String stripQuotes(String str) {
return str;
}

@Override
public LogicalPlan visitShowEncryptKeys(ShowEncryptKeysContext ctx) {
String dbName = null;
if (ctx.database != null) {
List<String> nameParts = visitMultipartIdentifier(ctx.database);
dbName = nameParts.get(0); // only one entry possible
}

String likeString = null;
if (ctx.LIKE() != null) {
likeString = stripQuotes(ctx.STRING_LITERAL().getText());
}
return new ShowEncryptKeysCommand(dbName, likeString);
}

@Override
public LogicalPlan visitAliasedQuery(AliasedQueryContext ctx) {
if (ctx.tableAlias().getText().equals("")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ public enum PlanType {
SHOW_DELETE_COMMAND,
SHOW_DIAGNOSE_TABLET_COMMAND,
SHOW_DYNAMIC_PARTITION_COMMAND,
SHOW_ENCRYPT_KEYS_COMMAND,
SHOW_EVENTS_COMMAND,
SHOW_FRONTENDS_COMMAND,
SHOW_GRANTS_COMMAND,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
// 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.catalog.Column;
import org.apache.doris.catalog.Database;
import org.apache.doris.catalog.DatabaseIf;
import org.apache.doris.catalog.EncryptKey;
import org.apache.doris.catalog.Env;
import org.apache.doris.catalog.ScalarType;
import org.apache.doris.common.AnalysisException;
import org.apache.doris.common.ErrorCode;
import org.apache.doris.common.ErrorReport;
import org.apache.doris.common.util.ListComparator;
import org.apache.doris.common.util.OrderByPair;
import org.apache.doris.common.util.Util;
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.ShowResultSet;
import org.apache.doris.qe.ShowResultSetMetaData;
import org.apache.doris.qe.StmtExecutor;

import com.google.common.base.Strings;
import com.google.common.collect.Lists;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* Represents the command for SHOW ENCRYPTKEYS.
*/
public class ShowEncryptKeysCommand extends ShowCommand {
private static final ShowResultSetMetaData META_DATA =
ShowResultSetMetaData.builder()
.addColumn(new Column("EncryptKey Name", ScalarType.createVarchar(20)))
.addColumn(new Column("EncryptKey String", ScalarType.createVarchar(1024)))
.build();

private String dbName;
private final String likeString;

public ShowEncryptKeysCommand(String databaseName, String likeString) {
super(PlanType.SHOW_ENCRYPT_KEYS_COMMAND);
this.dbName = databaseName;
this.likeString = likeString;
}

private void validate(ConnectContext ctx) throws AnalysisException {
if (Strings.isNullOrEmpty(dbName)) {
dbName = ctx.getDatabase();
if (Strings.isNullOrEmpty(dbName)) {
ErrorReport.reportAnalysisException(ErrorCode.ERR_NO_DB_ERROR);
}
}

// check auth
if (!Env.getCurrentEnv().getAccessManager().checkGlobalPriv(ConnectContext.get(), PrivPredicate.ADMIN)) {
ErrorReport.reportAnalysisException(ErrorCode.ERR_SPECIFIC_ACCESS_DENIED_ERROR,
PrivPredicate.ADMIN.getPrivs().toString());
}
}

@Override
public ShowResultSet doRun(ConnectContext ctx, StmtExecutor executor) throws Exception {
validate(ctx);
Util.prohibitExternalCatalog(ctx.getDefaultCatalog(), this.getClass().getSimpleName());
DatabaseIf db = ctx.getCurrentCatalog().getDbOrAnalysisException(dbName);
List<List<String>> resultRowSet = Lists.newArrayList();
if (db instanceof Database) {
List<EncryptKey> encryptKeys = ((Database) db).getEncryptKeys();
List<List<Comparable>> rowSet = Lists.newArrayList();
for (EncryptKey encryptKey : encryptKeys) {
List<Comparable> row = encryptKey.getInfo();
// like predicate
if (likeString == null || like(encryptKey.getEncryptKeyName().getKeyName())) {
rowSet.add(row);
}

}

// sort function rows by first column asc
ListComparator<List<Comparable>> comparator = null;
OrderByPair orderByPair = new OrderByPair(0, false);
comparator = new ListComparator<>(orderByPair);
Collections.sort(rowSet, comparator);

Set<String> encryptKeyNameSet = new HashSet<>();
for (List<Comparable> row : rowSet) {
List<String> resultRow = Lists.newArrayList();
for (Comparable column : row) {
resultRow.add(column.toString());
}
resultRowSet.add(resultRow);
encryptKeyNameSet.add(resultRow.get(0));
}
}

return new ShowResultSet(META_DATA, resultRowSet);
}

private boolean like(String str) {
str = str.toLowerCase();
return str.matches(likeString.replace(".", "\\.").replace("?", ".").replace("%", ".*").toLowerCase());
}

@Override
public <R, C> R accept(PlanVisitor<R, C> visitor, C context) {
return visitor.visitShowEncryptKeysCommand(this, context);
}

public ShowResultSetMetaData getMetaData() {
return META_DATA;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
import org.apache.doris.nereids.trees.plans.commands.ShowDeleteCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowDiagnoseTabletCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowDynamicPartitionCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowEncryptKeysCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowEventsCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowFrontendsCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowGrantsCommand;
Expand Down Expand Up @@ -533,6 +534,10 @@ default R visitShowTableIdCommand(ShowTableIdCommand showTableIdCommand, C conte
return visitCommand(showTableIdCommand, context);
}

default R visitShowEncryptKeysCommand(ShowEncryptKeysCommand showEncryptKeysCommand, C context) {
return visitCommand(showEncryptKeysCommand, context);
}

default R visitSyncCommand(SyncCommand syncCommand, C context) {
return visitCommand(syncCommand, context);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ suite("test_nereids_encrypt_test") {
sql """ use ${dbName}; """
checkNereidsExecute("drop encryptkey if exists ${encryptkeyName}")
checkNereidsExecute("""CREATE ENCRYPTKEY ${encryptkeyName} AS "ABCD123456789";""")
checkNereidsExecute("SHOW ENCRYPTKEYS FROM ${dbName}")
qt_check_encrypt_1("SHOW ENCRYPTKEYS FROM ${dbName}")
checkNereidsExecute("drop encryptkey ${encryptkeyName}")
checkNereidsExecute("SHOW ENCRYPTKEYS FROM ${dbName}")
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 43390ac

Please sign in to comment.