Skip to content
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 @@ -79,6 +79,7 @@ public void run(ConnectContext ctx, StmtExecutor executor) throws Exception {
NereidsPlanner planner = new NereidsPlanner(ctx.getStatementContext());
planner.plan(logicalPlanAdapter, ctx.getSessionVariable().toThrift());
executor.setPlanner(planner);
executor.checkBlockRules();
executor.handleExplainStmt(planner.getExplainString(new ExplainOptions(level)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public void run(ConnectContext ctx, StmtExecutor executor) throws Exception {
LogicalPlanAdapter logicalPlanAdapter = new LogicalPlanAdapter(logicalQuery, ctx.getStatementContext());
planner = new NereidsPlanner(ctx.getStatementContext());
planner.plan(logicalPlanAdapter, ctx.getSessionVariable().toThrift());

executor.checkBlockRules();
if (ctx.getMysqlChannel() != null) {
ctx.getMysqlChannel().reset();
}
Expand Down
46 changes: 27 additions & 19 deletions fe/fe-core/src/main/java/org/apache/doris/qe/StmtExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -463,24 +463,32 @@ public void execute(TUniqueId queryId) throws Exception {
}
}

private void checkBlockRules() throws AnalysisException {
if (originStmt != null) {
Env.getCurrentEnv().getSqlBlockRuleMgr().matchSql(
originStmt.originStmt, context.getSqlHash(), context.getQualifiedUser());
}

// limitations: partition_num, tablet_num, cardinality
if (planner != null) {
List<ScanNode> scanNodeList = planner.getScanNodes();
for (ScanNode scanNode : scanNodeList) {
if (scanNode instanceof OlapScanNode) {
OlapScanNode olapScanNode = (OlapScanNode) scanNode;
Env.getCurrentEnv().getSqlBlockRuleMgr().checkLimitations(
olapScanNode.getSelectedPartitionNum().longValue(),
olapScanNode.getSelectedTabletsNum(),
olapScanNode.getCardinality(),
context.getQualifiedUser());
}
public void checkBlockRules() throws AnalysisException {
checkBlockRulesByRegex(originStmt);
checkBlockRulesByScan(planner);
}

public void checkBlockRulesByRegex(OriginStatement originStmt) throws AnalysisException {
if (originStmt == null) {
return;
}
Env.getCurrentEnv().getSqlBlockRuleMgr().matchSql(
originStmt.originStmt, context.getSqlHash(), context.getQualifiedUser());
}

public void checkBlockRulesByScan(Planner planner) throws AnalysisException {
if (planner == null) {
return;
}
List<ScanNode> scanNodeList = planner.getScanNodes();
for (ScanNode scanNode : scanNodeList) {
if (scanNode instanceof OlapScanNode) {
OlapScanNode olapScanNode = (OlapScanNode) scanNode;
Env.getCurrentEnv().getSqlBlockRuleMgr().checkLimitations(
olapScanNode.getSelectedPartitionNum().longValue(),
olapScanNode.getSelectedTabletsNum(),
olapScanNode.getCardinality(),
context.getQualifiedUser());
}
}
}
Expand All @@ -492,7 +500,6 @@ private void executeByNereids(TUniqueId queryId) throws Exception {
profile.getSummaryProfile().setQueryBeginTime();
context.setStmtId(STMT_ID_GENERATOR.incrementAndGet());

checkBlockRules();
parseByNereids();
Preconditions.checkState(parsedStmt instanceof LogicalPlanAdapter,
"Nereids only process LogicalPlanAdapter, but parsedStmt is " + parsedStmt.getClass().getName());
Expand Down Expand Up @@ -546,6 +553,7 @@ private void executeByNereids(TUniqueId queryId) throws Exception {
planner = new NereidsPlanner(statementContext);
try {
planner.plan(parsedStmt, context.getSessionVariable().toThrift());
checkBlockRules();
} catch (Exception e) {
LOG.debug("Nereids plan query failed:\n{}", originStmt.originStmt);
throw new NereidsException(new AnalysisException(e.getMessage(), e));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@
// under the License.

suite("test_sql_block_rule") {

sql """
DROP SQL_BLOCK_RULE if exists test_rule_partition
"""

sql """
DROP SQL_BLOCK_RULE if exists test_rule_tablet
"""

sql """
DROP SQL_BLOCK_RULE if exists test_rule_num
"""
Expand All @@ -27,7 +36,7 @@ suite("test_sql_block_rule") {
`abcd` varchar(150) NULL COMMENT "",
`create_time` datetime NULL COMMENT ""
) ENGINE=OLAP
UNIQUE KEY(`abcd`)
DUPLICATE KEY(`abcd`)
COMMENT "OLAP"
DISTRIBUTED BY HASH(`abcd`) BUCKETS 3
PROPERTIES (
Expand All @@ -47,6 +56,15 @@ suite("test_sql_block_rule") {
sql("SELECT * FROM table_2", false)
exception "sql match regex sql block rule: test_rule_sql"
}
test {
sql("EXPLAIN SELECT * FROM table_2", false)
exception "sql match regex sql block rule: test_rule_sql"
}

test {
sql("INSERT INTO table_2 SELECT * FROM table_2", false)
exception "sql match regex sql block rule: test_rule_sql"
}

sql """
DROP SQL_BLOCK_RULE if exists test_rule_sql
Expand Down Expand Up @@ -126,4 +144,55 @@ suite("test_sql_block_rule") {
" PROPERTIES(\"sql\"=\"create\", \"global\"= \"true\", \"enable\"= \"true\")", false)
exception "sql of SQL_BLOCK_RULE should not match its name"
}

sql """DROP TABLE IF EXISTS a_partitioned_table_for_sql_block_rule"""

sql """
CREATE TABLE a_partitioned_table_for_sql_block_rule (
id BIGINT,
val BIGINT,
str VARCHAR(114)
) DUPLICATE KEY(`id`)
PARTITION BY RANGE(`id`)
(
PARTITION `p1` VALUES LESS THAN ('5'),
PARTITION `p2` VALUES LESS THAN ('10'),
PARTITION `p3` VALUES LESS THAN ('15')
)
DISTRIBUTED BY HASH(`id`) BUCKETS 3
PROPERTIES (
"replication_num"="1"
);
"""

sql """
INSERT INTO a_partitioned_table_for_sql_block_rule VALUES(1, 5, 11),(6,1,5),(11,8,5);
"""

sql """
CREATE SQL_BLOCK_RULE if not exists test_rule_partition PROPERTIES ( "partition_num" = "1", "global" = "true",
"enable"="true");
"""

test {
sql("""SELECT * FROM a_partitioned_table_for_sql_block_rule;""", false)

exception """sql hits sql block rule"""

}

sql """
CREATE SQL_BLOCK_RULE if not exists test_rule_tablet PROPERTIES ( "tablet_num" = "3", "global" = "true",
"enable"="true");
"""

test {
sql("""SELECT * FROM a_partitioned_table_for_sql_block_rule;""", false)

exception """sql hits sql block rule"""

}



}