diff --git a/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisParser.g4 b/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisParser.g4 index ff155e7672ffbf..f57c4f726e416a 100644 --- a/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisParser.g4 +++ b/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisParser.g4 @@ -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 @@ -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 diff --git a/fe/fe-core/src/main/java/org/apache/doris/blockrule/SqlBlockRuleMgr.java b/fe/fe-core/src/main/java/org/apache/doris/blockrule/SqlBlockRuleMgr.java index 13df2eb9377b17..c6dd0af42109eb 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/blockrule/SqlBlockRuleMgr.java +++ b/fe/fe-core/src/main/java/org/apache/doris/blockrule/SqlBlockRuleMgr.java @@ -80,6 +80,13 @@ public boolean existRule(String name) { **/ public List getSqlBlockRule(ShowSqlBlockRuleStmt stmt) throws AnalysisException { String ruleName = stmt.getRuleName(); + return getSqlBlockRule(ruleName); + } + + /** + * Get SqlBlockRule by rulename. + **/ + public List getSqlBlockRule(String ruleName) throws AnalysisException { if (StringUtils.isNotEmpty(ruleName)) { if (nameToSqlBlockRuleMap.containsKey(ruleName)) { SqlBlockRule sqlBlockRule = nameToSqlBlockRuleMap.get(ruleName); diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java index 05399ab63e7fc1..f07c1e38a26c2a 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java @@ -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; @@ -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; @@ -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(); diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/PlanType.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/PlanType.java index 7763a85bd5e82e..acd76881dbda85 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/PlanType.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/PlanType.java @@ -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, diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/ShowSqlBlockRuleCommand.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/ShowSqlBlockRuleCommand.java new file mode 100644 index 00000000000000..e9a32cd091e544 --- /dev/null +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/ShowSqlBlockRuleCommand.java @@ -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> rows = Lists.newArrayList(); + List sqlBlockRules = Env.getCurrentEnv().getSqlBlockRuleMgr().getSqlBlockRule(ruleName); + sqlBlockRules.forEach(rule -> rows.add(rule.getShowInfo())); + return new ShowResultSet(META_DATA, rows); + } + + @Override + public R accept(PlanVisitor visitor, C context) { + return visitor.visitShowSqlBlockRuleCommand(this, context); + } +} diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/visitor/CommandVisitor.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/visitor/CommandVisitor.java index db75e0de41bb63..9802e71c04b4bc 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/visitor/CommandVisitor.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/visitor/CommandVisitor.java @@ -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; @@ -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); } diff --git a/regression-test/data/sql_block_rule_p0/test_sql_block_rule.out b/regression-test/data/sql_block_rule_p0/test_sql_block_rule.out index f958424e65c626..373ec1348d1b39 100644 --- a/regression-test/data/sql_block_rule_p0/test_sql_block_rule.out +++ b/regression-test/data/sql_block_rule_p0/test_sql_block_rule.out @@ -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 -- diff --git a/regression-test/suites/sql_block_rule_p0/test_sql_block_rule.groovy b/regression-test/suites/sql_block_rule_p0/test_sql_block_rule.groovy index 651ee946bd92bb..e0243e35a090a7 100644 --- a/regression-test/suites/sql_block_rule_p0/test_sql_block_rule.groovy +++ b/regression-test/suites/sql_block_rule_p0/test_sql_block_rule.groovy @@ -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 """ @@ -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 """