Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Enhancement] (nereids)implement showSqlBlockRuleCommand in nereids #42979

Merged
merged 2 commits into from
Nov 19, 2024
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 @@ -208,6 +208,7 @@ supportedShowStatement
| SHOW PARTITION partitionId=INTEGER_VALUE #showPartitionId
| SHOW PROC path=STRING_LITERAL #showProc
| SHOW STORAGE? ENGINES #showStorageEngines
| SHOW SQL_BLOCK_RULE (FOR ruleName=identifier)? #showSqlBlockRule
| SHOW CREATE MATERIALIZED VIEW mvName=identifier
ON tableName=multipartIdentifier #showCreateMaterializedView
| SHOW BACKENDS #showBackends
Expand Down Expand Up @@ -244,8 +245,7 @@ lockTable


unsupportedShowStatement
: SHOW SQL_BLOCK_RULE (FOR ruleName=identifier)? #showSqlBlockRule
| SHOW ROW POLICY (FOR (userIdentify | (ROLE role=identifier)))? #showRowPolicy
: SHOW ROW POLICY (FOR (userIdentify | (ROLE role=identifier)))? #showRowPolicy
| SHOW STORAGE POLICY (USING (FOR policy=identifierOrText)?)? #showStoragePolicy
| SHOW STAGES #showStages
| SHOW STORAGE (VAULT | VAULTS) #showStorageVault
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ public boolean existRule(String name) {
**/
public List<SqlBlockRule> getSqlBlockRule(ShowSqlBlockRuleStmt stmt) throws AnalysisException {
String ruleName = stmt.getRuleName();
return getSqlBlockRule(ruleName);
}

/**
* Get SqlBlockRule by rulename.
**/
public List<SqlBlockRule> getSqlBlockRule(String ruleName) throws AnalysisException {
if (StringUtils.isNotEmpty(ruleName)) {
if (nameToSqlBlockRuleMap.containsKey(ruleName)) {
SqlBlockRule sqlBlockRule = nameToSqlBlockRuleMap.get(ruleName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@
import org.apache.doris.nereids.DorisParser.ShowProcedureStatusContext;
import org.apache.doris.nereids.DorisParser.ShowRepositoriesContext;
import org.apache.doris.nereids.DorisParser.ShowRolesContext;
import org.apache.doris.nereids.DorisParser.ShowSqlBlockRuleContext;
import org.apache.doris.nereids.DorisParser.ShowStorageEnginesContext;
import org.apache.doris.nereids.DorisParser.ShowTableIdContext;
import org.apache.doris.nereids.DorisParser.ShowVariablesContext;
Expand Down Expand Up @@ -455,6 +456,7 @@
import org.apache.doris.nereids.trees.plans.commands.ShowProcedureStatusCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowRepositoriesCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowRolesCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowSqlBlockRuleCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowStorageEnginesCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowTableIdCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowVariablesCommand;
Expand Down Expand Up @@ -4111,6 +4113,15 @@ public LogicalPlan visitShowPlugins(ShowPluginsContext ctx) {
return new ShowPluginsCommand();
}

@Override
public LogicalPlan visitShowSqlBlockRule(ShowSqlBlockRuleContext ctx) {
String ruleName = null;
if (ctx.ruleName != null) {
ruleName = ctx.ruleName.getText();
}
return new ShowSqlBlockRuleCommand(ruleName);
}

@Override
public LogicalPlan visitShowRepositories(ShowRepositoriesContext ctx) {
return new ShowRepositoriesCommand();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ public enum PlanType {
PREPARED_COMMAND,
EXECUTE_COMMAND,
SHOW_BACKENDS_COMMAND,
SHOW_BLOCK_RULE_COMMAND,
SHOW_CONFIG_COMMAND,
SHOW_CREATE_MATERIALIZED_VIEW_COMMAND,
SHOW_FRONTENDS_COMMAND,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// 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.blockrule.SqlBlockRule;
import org.apache.doris.catalog.Column;
import org.apache.doris.catalog.Env;
import org.apache.doris.catalog.ScalarType;
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.ShowResultSet;
import org.apache.doris.qe.ShowResultSetMetaData;
import org.apache.doris.qe.StmtExecutor;

import com.google.common.collect.Lists;

import java.util.List;

/**
* show sql block rule command
*/
public class ShowSqlBlockRuleCommand extends ShowCommand {
private static final ShowResultSetMetaData META_DATA =
ShowResultSetMetaData.builder()
.addColumn(new Column("Name", ScalarType.createVarchar(50)))
.addColumn(new Column("Sql", ScalarType.createVarchar(65535)))
.addColumn(new Column("SqlHash", ScalarType.createVarchar(65535)))
.addColumn(new Column("PartitionNum", ScalarType.createVarchar(10)))
.addColumn(new Column("TabletNum", ScalarType.createVarchar(10)))
.addColumn(new Column("Cardinality", ScalarType.createVarchar(20)))
.addColumn(new Column("Global", ScalarType.createVarchar(4)))
.addColumn(new Column("Enable", ScalarType.createVarchar(4)))
.build();
private final String ruleName; // optional

/**
* constructor
*/
public ShowSqlBlockRuleCommand(String ruleName) {
super(PlanType.SHOW_BLOCK_RULE_COMMAND);
this.ruleName = ruleName;
}

@Override
public ShowResultSet doRun(ConnectContext ctx, StmtExecutor executor) throws Exception {
// check auth
if (!Env.getCurrentEnv().getAccessManager().checkGlobalPriv(ConnectContext.get(), PrivPredicate.ADMIN)) {
ErrorReport.reportAnalysisException(ErrorCode.ERR_SPECIFIC_ACCESS_DENIED_ERROR, "ADMIN");
}

List<List<String>> rows = Lists.newArrayList();
List<SqlBlockRule> sqlBlockRules = Env.getCurrentEnv().getSqlBlockRuleMgr().getSqlBlockRule(ruleName);
sqlBlockRules.forEach(rule -> rows.add(rule.getShowInfo()));
return new ShowResultSet(META_DATA, rows);
}

@Override
public <R, C> R accept(PlanVisitor<R, C> visitor, C context) {
return visitor.visitShowSqlBlockRuleCommand(this, context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
import org.apache.doris.nereids.trees.plans.commands.ShowProcedureStatusCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowRepositoriesCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowRolesCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowSqlBlockRuleCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowStorageEnginesCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowTableIdCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowVariablesCommand;
Expand Down Expand Up @@ -274,6 +275,10 @@ default R visitShowBackendsCommand(ShowBackendsCommand showBackendsCommand, C co
return visitCommand(showBackendsCommand, context);
}

default R visitShowSqlBlockRuleCommand(ShowSqlBlockRuleCommand showblockruleCommand, C context) {
return visitCommand(showblockruleCommand, context);
}

default R visitShowPluginsCommand(ShowPluginsCommand showPluginsCommand, C context) {
return visitCommand(showPluginsCommand, context);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
-- This file is automatically generated. You should know what you did if you want to edit this
-- !select --
-- !select1 --
test_rule_sql SELECT abcd FROM table_2 NULL 0 0 0 true true

-- !select2 --
test_rule_sql SELECT abcd FROM table_2 NULL 0 0 0 true true

-- !select3 --

Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ suite("test_sql_block_rule", "nonConcurrent") {
exception "sql match regex sql block rule: test_rule_sql"
}

checkNereidsExecute("SHOW SQL_BLOCK_RULE")

qt_select1 """
SHOW SQL_BLOCK_RULE
"""

qt_select2 """
SHOW SQL_BLOCK_RULE FOR test_rule_sql
"""

sql """
DROP SQL_BLOCK_RULE if exists test_rule_sql
"""
Expand All @@ -100,7 +110,7 @@ suite("test_sql_block_rule", "nonConcurrent") {
exception "sql hits sql block rule: test_rule_num, reach tablet_num : 1"
}
*/
qt_select """
qt_select3 """
SHOW SQL_BLOCK_RULE
"""

Expand Down
Loading