You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@doris.apache.org by mo...@apache.org on 2022/06/01 15:29:02 UTC
[incubator-doris] 07/22: [fix](sql-block-rule) sql block rule NPE (#9778)
This is an automated email from the ASF dual-hosted git repository.
morningman pushed a commit to branch dev-1.0.1
in repository https://gitbox.apache.org/repos/asf/incubator-doris.git
commit e57aa42fa7de132ee0bfd58306b42058a684bc46
Author: morningman <mo...@163.com>
AuthorDate: Wed Jun 1 18:21:54 2022 +0800
[fix](sql-block-rule) sql block rule NPE (#9778)
---
.../org/apache/doris/blockrule/SqlBlockRule.java | 37 +-
.../apache/doris/blockrule/SqlBlockRuleMgr.java | 84 +++--
.../java/org/apache/doris/qe/StmtExecutor.java | 7 +-
.../doris/blockrule/SqlBlockRuleMgrTest.java | 382 ---------------------
4 files changed, 92 insertions(+), 418 deletions(-)
diff --git a/fe/fe-core/src/main/java/org/apache/doris/blockrule/SqlBlockRule.java b/fe/fe-core/src/main/java/org/apache/doris/blockrule/SqlBlockRule.java
index 97304715dc..5601415ab3 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/blockrule/SqlBlockRule.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/blockrule/SqlBlockRule.java
@@ -21,6 +21,7 @@ import org.apache.doris.analysis.AlterSqlBlockRuleStmt;
import org.apache.doris.analysis.CreateSqlBlockRuleStmt;
import org.apache.doris.common.io.Text;
import org.apache.doris.common.io.Writable;
+import org.apache.doris.common.util.SqlBlockUtil;
import org.apache.doris.persist.gson.GsonUtils;
import com.google.common.collect.Lists;
@@ -34,12 +35,13 @@ import java.io.IOException;
import java.util.List;
import java.util.regex.Pattern;
+/**
+ * Use for block some sql by rule.
+ **/
public class SqlBlockRule implements Writable {
public static final String NAME_TYPE = "SQL BLOCK RULE NAME";
- public static final String DEFAULT_USER = "default";
-
// the rule name, cluster unique
@SerializedName(value = "name")
private String name;
@@ -73,11 +75,11 @@ public class SqlBlockRule implements Writable {
private Pattern sqlPattern;
- public SqlBlockRule(String name) {
- this.name = name;
- }
-
- public SqlBlockRule(String name, String sql, String sqlHash, Long partitionNum, Long tabletNum, Long cardinality, Boolean global, Boolean enable) {
+ /**
+ * Create SqlBlockRule.
+ **/
+ public SqlBlockRule(String name, String sql, String sqlHash, Long partitionNum, Long tabletNum, Long cardinality,
+ Boolean global, Boolean enable) {
this.name = name;
this.sql = sql;
this.sqlHash = sqlHash;
@@ -92,11 +94,13 @@ public class SqlBlockRule implements Writable {
}
public static SqlBlockRule fromCreateStmt(CreateSqlBlockRuleStmt stmt) {
- return new SqlBlockRule(stmt.getRuleName(), stmt.getSql(), stmt.getSqlHash(), stmt.getPartitionNum(), stmt.getTabletNum(), stmt.getCardinality(), stmt.isGlobal(), stmt.isEnable());
+ return new SqlBlockRule(stmt.getRuleName(), stmt.getSql(), stmt.getSqlHash(), stmt.getPartitionNum(),
+ stmt.getTabletNum(), stmt.getCardinality(), stmt.isGlobal(), stmt.isEnable());
}
public static SqlBlockRule fromAlterStmt(AlterSqlBlockRuleStmt stmt) {
- return new SqlBlockRule(stmt.getRuleName(), stmt.getSql(), stmt.getSqlHash(), stmt.getPartitionNum(), stmt.getTabletNum(), stmt.getCardinality(), stmt.getGlobal(), stmt.getEnable());
+ return new SqlBlockRule(stmt.getRuleName(), stmt.getSql(), stmt.getSqlHash(), stmt.getPartitionNum(),
+ stmt.getTabletNum(), stmt.getCardinality(), stmt.getGlobal(), stmt.getEnable());
}
public String getName() {
@@ -167,12 +171,15 @@ public class SqlBlockRule implements Writable {
this.enable = enable;
}
+ /**
+ * Show SqlBlockRule info.
+ **/
public List<String> getShowInfo() {
return Lists.newArrayList(this.name, this.sql, this.sqlHash,
this.partitionNum == null ? "0" : Long.toString(this.partitionNum),
this.tabletNum == null ? "0" : Long.toString(this.tabletNum),
- this.cardinality == null ? "0" : Long.toString(this.cardinality),
- String.valueOf(this.global), String.valueOf(this.enable));
+ this.cardinality == null ? "0" : Long.toString(this.cardinality), String.valueOf(this.global),
+ String.valueOf(this.enable));
}
@Override
@@ -180,10 +187,16 @@ public class SqlBlockRule implements Writable {
Text.writeString(out, GsonUtils.GSON.toJson(this));
}
+ /**
+ * Read data from file.
+ **/
public static SqlBlockRule read(DataInput in) throws IOException {
String json = Text.readString(in);
SqlBlockRule sqlBlockRule = GsonUtils.GSON.fromJson(json, SqlBlockRule.class);
- sqlBlockRule.setSqlPattern(Pattern.compile(sqlBlockRule.getSql()));
+ if (StringUtils.isNotEmpty(sqlBlockRule.getSql()) && !SqlBlockUtil.STRING_DEFAULT.equals(
+ sqlBlockRule.getSql())) {
+ sqlBlockRule.setSqlPattern(Pattern.compile(sqlBlockRule.getSql()));
+ }
return sqlBlockRule;
}
}
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 c1ddf633df..46ac87aaf9 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
@@ -47,6 +47,9 @@ import java.util.Map;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.stream.Collectors;
+/**
+ * Manage SqlBlockRule.
+ **/
public class SqlBlockRuleMgr implements Writable {
private static final Logger LOG = LogManager.getLogger(SqlBlockRuleMgr.class);
@@ -63,10 +66,16 @@ public class SqlBlockRuleMgr implements Writable {
lock.writeLock().unlock();
}
+ /**
+ * Judge whether exist rule by ruleName.
+ **/
public boolean existRule(String name) {
return nameToSqlBlockRuleMap.containsKey(name);
}
+ /**
+ * Get SqlBlockRule by show stmt.
+ **/
public List<SqlBlockRule> getSqlBlockRule(ShowSqlBlockRuleStmt stmt) throws AnalysisException {
String ruleName = stmt.getRuleName();
if (StringUtils.isNotEmpty(ruleName)) {
@@ -79,9 +88,11 @@ public class SqlBlockRuleMgr implements Writable {
return Lists.newArrayList(nameToSqlBlockRuleMap.values());
}
- // check limitation's effectiveness of a sql_block_rule
- public static void verifyLimitations(SqlBlockRule sqlBlockRule) throws DdlException {
- if (sqlBlockRule.getPartitionNum() < 0){
+ /**
+ * Check limitation's effectiveness of a SqlBlockRule.
+ **/
+ private static void verifyLimitations(SqlBlockRule sqlBlockRule) throws DdlException {
+ if (sqlBlockRule.getPartitionNum() < 0) {
throw new DdlException("the value of partition_num can't be a negative");
}
if (sqlBlockRule.getTabletNum() < 0){
@@ -92,6 +103,9 @@ public class SqlBlockRuleMgr implements Writable {
}
}
+ /**
+ * Create SqlBlockRule for create stmt.
+ **/
public void createSqlBlockRule(CreateSqlBlockRuleStmt stmt) throws UserException {
writeLock();
try {
@@ -108,11 +122,17 @@ public class SqlBlockRuleMgr implements Writable {
}
}
+ /**
+ * Add local cache when receive editLog.
+ **/
public void replayCreate(SqlBlockRule sqlBlockRule) {
unprotectedAdd(sqlBlockRule);
LOG.info("replay create sql block rule: {}", sqlBlockRule);
}
+ /**
+ * Alter SqlBlockRule for alter stmt.
+ **/
public void alterSqlBlockRule(AlterSqlBlockRuleStmt stmt) throws AnalysisException, DdlException {
writeLock();
try {
@@ -159,14 +179,17 @@ public class SqlBlockRuleMgr implements Writable {
LOG.info("replay alter sql block rule: {}", sqlBlockRule);
}
- public void unprotectedUpdate(SqlBlockRule sqlBlockRule) {
+ private void unprotectedUpdate(SqlBlockRule sqlBlockRule) {
nameToSqlBlockRuleMap.put(sqlBlockRule.getName(), sqlBlockRule);
}
- public void unprotectedAdd(SqlBlockRule sqlBlockRule) {
+ private void unprotectedAdd(SqlBlockRule sqlBlockRule) {
nameToSqlBlockRuleMap.put(sqlBlockRule.getName(), sqlBlockRule);
}
+ /**
+ * Drop SqlBlockRule for drop stmt.
+ **/
public void dropSqlBlockRule(DropSqlBlockRuleStmt stmt) throws DdlException {
writeLock();
try {
@@ -192,9 +215,13 @@ public class SqlBlockRuleMgr implements Writable {
ruleNames.forEach(name -> nameToSqlBlockRuleMap.remove(name));
}
+ /**
+ * Match SQL according to rules.
+ **/
public void matchSql(String originSql, String sqlHash, String user) throws AnalysisException {
// match global rule
- List<SqlBlockRule> globalRules = nameToSqlBlockRuleMap.values().stream().filter(SqlBlockRule::getGlobal).collect(Collectors.toList());
+ List<SqlBlockRule> globalRules =
+ nameToSqlBlockRuleMap.values().stream().filter(SqlBlockRule::getGlobal).collect(Collectors.toList());
for (SqlBlockRule rule : globalRules) {
matchSql(rule, originSql, sqlHash);
}
@@ -209,25 +236,30 @@ public class SqlBlockRuleMgr implements Writable {
}
}
- public void matchSql(SqlBlockRule rule, String originSql, String sqlHash) throws AnalysisException {
+ private void matchSql(SqlBlockRule rule, String originSql, String sqlHash) throws AnalysisException {
if (rule.getEnable()) {
- if (StringUtils.isNotEmpty(rule.getSqlHash()) &&
- (!CreateSqlBlockRuleStmt.STRING_NOT_SET.equals(rule.getSqlHash()) && rule.getSqlHash().equals(sqlHash))) {
+ if (StringUtils.isNotEmpty(rule.getSqlHash()) && !SqlBlockUtil.STRING_DEFAULT.equals(rule.getSqlHash())
+ && rule.getSqlHash().equals(sqlHash)) {
MetricRepo.COUNTER_HIT_SQL_BLOCK_RULE.increase(1L);
throw new AnalysisException("sql match hash sql block rule: " + rule.getName());
- } else if (StringUtils.isNotEmpty(rule.getSql()) &&
- (!CreateSqlBlockRuleStmt.STRING_NOT_SET.equals(rule.getSql()) && rule.getSqlPattern().matcher(originSql).find())) {
+ } else if (StringUtils.isNotEmpty(rule.getSql()) && !SqlBlockUtil.STRING_DEFAULT.equals(rule.getSql())
+ && rule.getSqlPattern() != null && rule.getSqlPattern().matcher(originSql).find()) {
MetricRepo.COUNTER_HIT_SQL_BLOCK_RULE.increase(1L);
throw new AnalysisException("sql match regex sql block rule: " + rule.getName());
}
}
}
- public void checkLimitaions(Long partitionNum, Long tabletNum, Long cardinality, String user) throws AnalysisException {
+ /**
+ * Check number whether legal by user.
+ **/
+ public void checkLimitations(Long partitionNum, Long tabletNum, Long cardinality, String user)
+ throws AnalysisException {
// match global rule
- List<SqlBlockRule> globalRules = nameToSqlBlockRuleMap.values().stream().filter(SqlBlockRule::getGlobal).collect(Collectors.toList());
+ List<SqlBlockRule> globalRules =
+ nameToSqlBlockRuleMap.values().stream().filter(SqlBlockRule::getGlobal).collect(Collectors.toList());
for (SqlBlockRule rule : globalRules) {
- checkLimitaions(rule, partitionNum, tabletNum, cardinality);
+ checkLimitations(rule, partitionNum, tabletNum, cardinality);
}
// match user rule
String[] bindSqlBlockRules = Catalog.getCurrentCatalog().getAuth().getSqlBlockRules(user);
@@ -236,24 +268,32 @@ public class SqlBlockRuleMgr implements Writable {
if (rule == null) {
continue;
}
- checkLimitaions(rule, partitionNum, tabletNum, cardinality);
+ checkLimitations(rule, partitionNum, tabletNum, cardinality);
}
}
- public void checkLimitaions(SqlBlockRule rule, Long partitionNum, Long tabletNum, Long cardinality) throws AnalysisException {
+ /**
+ * Check number whether legal by SqlBlockRule.
+ **/
+ private void checkLimitations(SqlBlockRule rule, Long partitionNum, Long tabletNum, Long cardinality)
+ throws AnalysisException {
if (rule.getPartitionNum() == 0 && rule.getTabletNum() == 0 && rule.getCardinality() == 0) {
return;
} else if (rule.getEnable()) {
- if ((rule.getPartitionNum() != 0 && rule.getPartitionNum() < partitionNum)
- || (rule.getTabletNum() != 0 && rule.getTabletNum() < tabletNum)
- || (rule.getCardinality() != 0 && rule.getCardinality() < cardinality)) {
+ if ((rule.getPartitionNum() != 0 && rule.getPartitionNum() < partitionNum) || (rule.getTabletNum() != 0
+ && rule.getTabletNum() < tabletNum) || (rule.getCardinality() != 0
+ && rule.getCardinality() < cardinality)) {
MetricRepo.COUNTER_HIT_SQL_BLOCK_RULE.increase(1L);
if (rule.getPartitionNum() < partitionNum && rule.getPartitionNum() != 0) {
- throw new AnalysisException("sql hits sql block rule: " + rule.getName() + ", reach partition_num : " + rule.getPartitionNum());
+ throw new AnalysisException(
+ "sql hits sql block rule: " + rule.getName() + ", reach partition_num : "
+ + rule.getPartitionNum());
} else if (rule.getTabletNum() < tabletNum && rule.getTabletNum() != 0) {
- throw new AnalysisException("sql hits sql block rule: " + rule.getName() + ", reach tablet_num : " + rule.getTabletNum());
+ throw new AnalysisException("sql hits sql block rule: " + rule.getName() + ", reach tablet_num : "
+ + rule.getTabletNum());
} else if (rule.getCardinality() < cardinality && rule.getCardinality() != 0) {
- throw new AnalysisException("sql hits sql block rule: " + rule.getName() + ", reach cardinality : " + rule.getCardinality());
+ throw new AnalysisException("sql hits sql block rule: " + rule.getName() + ", reach cardinality : "
+ + rule.getCardinality());
}
}
}
diff --git a/fe/fe-core/src/main/java/org/apache/doris/qe/StmtExecutor.java b/fe/fe-core/src/main/java/org/apache/doris/qe/StmtExecutor.java
index 6ba7364f1a..ec36508e0f 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/qe/StmtExecutor.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/qe/StmtExecutor.java
@@ -371,8 +371,11 @@ public class StmtExecutor implements ProfileWriter {
for (ScanNode scanNode : scanNodeList) {
if (scanNode instanceof OlapScanNode) {
OlapScanNode olapScanNode = (OlapScanNode) scanNode;
- Catalog.getCurrentCatalog().getSqlBlockRuleMgr().checkLimitaions(olapScanNode.getSelectedPartitionNum().longValue(),
- olapScanNode.getSelectedTabletsNum(), olapScanNode.getCardinality(), analyzer.getQualifiedUser());
+ Catalog.getCurrentCatalog().getSqlBlockRuleMgr().checkLimitations(
+ olapScanNode.getSelectedPartitionNum().longValue(),
+ olapScanNode.getSelectedTabletsNum(),
+ olapScanNode.getCardinality(),
+ context.getQualifiedUser());
}
}
}
diff --git a/fe/fe-core/src/test/java/org/apache/doris/blockrule/SqlBlockRuleMgrTest.java b/fe/fe-core/src/test/java/org/apache/doris/blockrule/SqlBlockRuleMgrTest.java
deleted file mode 100644
index bf93eae895..0000000000
--- a/fe/fe-core/src/test/java/org/apache/doris/blockrule/SqlBlockRuleMgrTest.java
+++ /dev/null
@@ -1,382 +0,0 @@
-// 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.blockrule;
-
-import org.apache.doris.analysis.AlterSqlBlockRuleStmt;
-import org.apache.doris.analysis.Analyzer;
-import org.apache.doris.analysis.CreateDbStmt;
-import org.apache.doris.analysis.CreateSqlBlockRuleStmt;
-import org.apache.doris.analysis.CreateTableStmt;
-import org.apache.doris.analysis.SetUserPropertyStmt;
-import org.apache.doris.analysis.ShowSqlBlockRuleStmt;
-import org.apache.doris.catalog.Catalog;
-import org.apache.doris.common.AnalysisException;
-import org.apache.doris.common.DdlException;
-import org.apache.doris.common.ExceptionChecker;
-import org.apache.doris.common.UserException;
-import org.apache.doris.common.jmockit.Deencapsulation;
-import org.apache.doris.metric.MetricRepo;
-import org.apache.doris.planner.OlapScanNode;
-import org.apache.doris.planner.Planner;
-import org.apache.doris.planner.ScanNode;
-import org.apache.doris.qe.ConnectContext;
-import org.apache.doris.qe.StmtExecutor;
-import org.apache.doris.utframe.UtFrameUtils;
-
-import org.apache.commons.codec.digest.DigestUtils;
-import org.junit.AfterClass;
-import org.junit.Assert;
-import org.junit.BeforeClass;
-import org.junit.Test;
-
-import java.io.File;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.UUID;
-import java.util.HashMap;
-import java.util.Map;
-
-public class SqlBlockRuleMgrTest {
-
- private static String runningDir = "fe/mocked/SqlBlockRuleMgrTest/" + UUID.randomUUID().toString() + "/";
-
- private static ConnectContext connectContext;
-
- @BeforeClass
- public static void beforeClass() throws Exception {
- UtFrameUtils.createDorisCluster(runningDir);
-
- // create connect context
- connectContext = UtFrameUtils.createDefaultCtx();
-
- // create database
- String createDbStmtStr = "create database test;";
- CreateDbStmt createDbStmt = (CreateDbStmt) UtFrameUtils.parseAndAnalyzeStmt(createDbStmtStr, connectContext);
- Catalog.getCurrentCatalog().createDb(createDbStmt);
-
- MetricRepo.init();
- createTable("create table test.table1\n" +
- "(k1 int, k2 int) distributed by hash(k1) buckets 1\n" +
- "properties(\"replication_num\" = \"1\");");
-
- createTable("create table test.table2\n" +
- "(k1 datetime, k2 int)\n" +
- "ENGINE=OLAP\n" +
- "PARTITION BY RANGE(k1)\n" +
- "(\n" +
- "PARTITION p20211213 VALUES [('2021-12-13 00:00:00'), ('2021-12-14 00:00:00')),\n" +
- "PARTITION p20211214 VALUES [('2021-12-14 00:00:00'), ('2021-12-15 00:00:00')),\n" +
- "PARTITION p20211215 VALUES [('2021-12-15 00:00:00'), ('2021-12-16 00:00:00')),\n" +
- "PARTITION p20211216 VALUES [('2021-12-16 00:00:00'), ('2021-12-17 00:00:00'))\n" +
- ")\n" +
- "DISTRIBUTED BY HASH(k1)\n" +
- "BUCKETS 10\n" +
- "PROPERTIES (\n" +
- "\"replication_num\" = \"1\"\n" +
- ");");
-
- }
-
- @AfterClass
- public static void tearDown() {
- File file = new File(runningDir);
- file.delete();
- }
-
- private static void createTable(String sql) throws Exception {
- CreateTableStmt createTableStmt = (CreateTableStmt) UtFrameUtils.parseAndAnalyzeStmt(sql, connectContext);
- Catalog.getCurrentCatalog().createTable(createTableStmt);
- }
-
- @Test
- public void testUserMatchSql() throws Exception {
- String sql = "select * from table1 limit 10";
- String sqlHash = DigestUtils.md5Hex(sql);
- SqlBlockRule sqlRule = new SqlBlockRule("test_rule1", null, sqlHash, 0L, 0L, 0L, false, true);
- SqlBlockRuleMgr mgr = Catalog.getCurrentCatalog().getSqlBlockRuleMgr();
- mgr.replayCreate(sqlRule);
- // sql block rules
- String setPropertyStr = "set property for \"root\" \"sql_block_rules\" = \"test_rule1\"";
- SetUserPropertyStmt setUserPropertyStmt = (SetUserPropertyStmt) UtFrameUtils.parseAndAnalyzeStmt(setPropertyStr, connectContext);
- Catalog.getCurrentCatalog().getAuth().updateUserProperty(setUserPropertyStmt);
- ExceptionChecker.expectThrowsWithMsg(AnalysisException.class, "sql match hash sql block rule: " + sqlRule.getName(),
- () -> mgr.matchSql(sql, sqlHash, "root"));
- }
-
- @Test
- public void testGlobalMatchSql() throws AnalysisException {
- String sql = "select * from test_table1 limit 10";
- String sqlHash = DigestUtils.md5Hex(sql);
- SqlBlockRule sqlRule = new SqlBlockRule("test_rule1", null, sqlHash, 0L, 0L, 0L, true, true);
- SqlBlockRuleMgr mgr = new SqlBlockRuleMgr();
- mgr.replayCreate(sqlRule);
- ExceptionChecker.expectThrowsWithMsg(AnalysisException.class, "sql match hash sql block rule: " + sqlRule.getName(),
- () -> mgr.matchSql(sql, sqlHash, "test"));
- }
-
- @Test
- public void testRegexMatchSql() throws AnalysisException {
- String sql = "select * from test_table1 tt1 join test_table2 tt2 on tt1.testId=tt2.testId limit 5";
- String sqlHash = DigestUtils.md5Hex(sql);
- SqlBlockRule sqlRule = new SqlBlockRule("test_rule1", ".* join .*", null, 0L, 0L, 0L, true, true);
- SqlBlockRuleMgr mgr = new SqlBlockRuleMgr();
- mgr.replayCreate(sqlRule);
- ExceptionChecker.expectThrowsWithMsg(AnalysisException.class, "sql match regex sql block rule: " + sqlRule.getName(),
- () -> mgr.matchSql(sqlRule, sql, sqlHash));
- }
-
- @Test
- public void testHashMatchSql() throws AnalysisException {
- String sql = "select * from test_table1 tt1 join test_table2 tt2 on tt1.testId=tt2.testId limit 5";
- String sqlHash = DigestUtils.md5Hex(sql);
- System.out.println(sqlHash);
- SqlBlockRule sqlRule = new SqlBlockRule("test_rule1", null, sqlHash, 0L, 0L, 0L, true, true);
- SqlBlockRuleMgr mgr = new SqlBlockRuleMgr();
- mgr.replayCreate(sqlRule);
- ExceptionChecker.expectThrowsWithMsg(AnalysisException.class, "sql match hash sql block rule: " + sqlRule.getName(),
- () -> mgr.matchSql(sqlRule, sql, sqlHash));
- }
-
- @Test
- public void testReachLimitations() throws AnalysisException, Exception {
- String sql = "select * from test.table2;";
- StmtExecutor executor = UtFrameUtils.getSqlStmtExecutor(connectContext, sql);
- Planner planner = executor.planner();
- List<ScanNode> scanNodeList = planner.getScanNodes();
- OlapScanNode olapScanNode = (OlapScanNode) scanNodeList.get(0);
- Integer selectedPartition = Deencapsulation.getField(olapScanNode, "selectedPartitionNum");
- long selectedPartitionLongValue = selectedPartition.longValue();
- long selectedTablet = Deencapsulation.getField(olapScanNode, "selectedTabletsNum");
- long cardinality = Deencapsulation.getField(olapScanNode, "cardinality");
- Assert.assertEquals(0L, selectedPartitionLongValue);
- Assert.assertEquals(0L, selectedTablet);
- Assert.assertEquals(0L, cardinality);
-
- SqlBlockRuleMgr mgr = Catalog.getCurrentCatalog().getSqlBlockRuleMgr();
-
- // test reach partition_num :
- // cuz there is no data in test.table2, so the selectedPartitionLongValue == 0;
- // set sqlBlockRule.partition_num = -1, so it can be blocked.
- SqlBlockRule sqlBlockRule = new SqlBlockRule("test_rule2", "NULL", "NULL", -1L, 0L, 0L, true, true);
- mgr.replayCreate(sqlBlockRule);
- Assert.assertEquals(true, mgr.existRule("test_rule2"));
- Assert.assertEquals("NULL", sqlBlockRule.getSql());
- Assert.assertEquals("NULL", sqlBlockRule.getSqlHash());
- Assert.assertEquals(-1L, (long) sqlBlockRule.getPartitionNum());
- Assert.assertEquals(0L, (long) sqlBlockRule.getTabletNum());
- Assert.assertEquals(0L, (long) sqlBlockRule.getCardinality());
-
- ExceptionChecker.expectThrowsWithMsg(AnalysisException.class, "errCode = 2, detailMessage = sql hits sql block rule: "
- + sqlBlockRule.getName() + ", reach partition_num : " + sqlBlockRule.getPartitionNum(),
- () -> mgr.checkLimitaions(sqlBlockRule, selectedPartitionLongValue, selectedTablet, cardinality));
-
- // test reach tablet_num :
- SqlBlockRule sqlBlockRule2 = new SqlBlockRule("test_rule3", "NULL", "NULL", 0L, -1L, 0L, true, true);
- mgr.replayCreate(sqlBlockRule2);
- Assert.assertEquals(true, mgr.existRule("test_rule3"));
- Assert.assertEquals("NULL", sqlBlockRule2.getSql());
- Assert.assertEquals("NULL", sqlBlockRule2.getSqlHash());
- Assert.assertEquals(0L, (long) sqlBlockRule2.getPartitionNum());
- Assert.assertEquals(-1L, (long) sqlBlockRule2.getTabletNum());
- Assert.assertEquals(0L, (long) sqlBlockRule2.getCardinality());
-
- ExceptionChecker.expectThrowsWithMsg(AnalysisException.class, "errCode = 2, detailMessage = sql hits sql block rule: "
- + sqlBlockRule2.getName() + ", reach tablet_num : " + sqlBlockRule2.getTabletNum(),
- () -> mgr.checkLimitaions(sqlBlockRule2, selectedPartitionLongValue, selectedTablet, cardinality));
-
- // test reach cardinality :
- SqlBlockRule sqlBlockRule3 = new SqlBlockRule("test_rule4", "NULL", "NULL", 0L, 0L, -1L, true, true);
- mgr.replayCreate(sqlBlockRule3);
- Assert.assertEquals(true, mgr.existRule("test_rule4"));
- Assert.assertEquals("NULL", sqlBlockRule3.getSql());
- Assert.assertEquals("NULL", sqlBlockRule3.getSqlHash());
- Assert.assertEquals(0L, (long) sqlBlockRule3.getPartitionNum());
- Assert.assertEquals(0L, (long) sqlBlockRule3.getTabletNum());
- Assert.assertEquals(-1L, (long) sqlBlockRule3.getCardinality());
-
- ExceptionChecker.expectThrowsWithMsg(AnalysisException.class, "errCode = 2, detailMessage = sql hits sql block rule: "
- + sqlBlockRule3.getName() + ", reach cardinality : " + sqlBlockRule3.getCardinality(),
- () -> mgr.checkLimitaions(sqlBlockRule3, selectedPartitionLongValue, selectedTablet, cardinality));
- }
-
- @Test
- public void testAlterInvalid() throws Exception {
- Analyzer analyzer = new Analyzer(Catalog.getCurrentCatalog(), connectContext);
- SqlBlockRuleMgr mgr = Catalog.getCurrentCatalog().getSqlBlockRuleMgr();
-
- // create : sql
- // alter : sqlHash
- // AnalysisException : Only sql or sqlHash can be configured
- SqlBlockRule sqlBlockRule = new SqlBlockRule("test_rule", "select \\* from test_table", "NULL", 0L, 0L, 0L, true, true);
- mgr.unprotectedAdd(sqlBlockRule);
- Assert.assertEquals(true, mgr.existRule("test_rule"));
-
- Map<String, String> properties = new HashMap<>();
- properties.put(CreateSqlBlockRuleStmt.SQL_HASH_PROPERTY, "xxxx");
- AlterSqlBlockRuleStmt stmt = new AlterSqlBlockRuleStmt("test_rule", properties);
- stmt.analyze(analyzer);
-
- ExceptionChecker.expectThrowsWithMsg(AnalysisException.class, "Only sql or sqlHash can be configured",
- () -> mgr.alterSqlBlockRule(stmt));
-
- // create : sql
- // alter : tabletNum
- // AnalysisException : sql/sqlHash and partition_num/tablet_num/cardinality cannot be set in one rule.
- Map<String, String> properties2 = new HashMap<>();
- properties2.put(CreateSqlBlockRuleStmt.SCANNED_TABLET_NUM, "4");
- AlterSqlBlockRuleStmt stmt2 = new AlterSqlBlockRuleStmt("test_rule", properties2);
-
- stmt2.analyze(analyzer);
- ExceptionChecker.expectThrowsWithMsg(AnalysisException.class, "sql/sqlHash and partition_num/tablet_num/cardinality cannot be set in one rule.",
- () -> mgr.alterSqlBlockRule(stmt2));
-
- // create : cardinality
- // alter : sqlHash
- // AnalysisException : sql/sqlHash and partition_num/tablet_num/cardinality cannot be set in one rule.
- SqlBlockRule sqlBlockRule2 = new SqlBlockRule("test_rule2", "NULL", "NULL", 0L, 0L, 10L, true, true);
- mgr.unprotectedAdd(sqlBlockRule2);
- Assert.assertEquals(true, mgr.existRule("test_rule2"));
-
- Map<String, String> properties3 = new HashMap<>();
- properties3.put(CreateSqlBlockRuleStmt.SQL_HASH_PROPERTY, "xxxx");
- AlterSqlBlockRuleStmt stmt3 = new AlterSqlBlockRuleStmt("test_rule2", properties3);
- stmt3.analyze(analyzer);
- ExceptionChecker.expectThrowsWithMsg(AnalysisException.class, "sql/sqlHash and partition_num/tablet_num/cardinality cannot be set in one rule.",
- () -> mgr.alterSqlBlockRule(stmt3));
- }
-
- @Test
- public void testNormalCreate() throws Exception {
- String createSql = "CREATE SQL_BLOCK_RULE test_rule PROPERTIES(\"sql\"=\"select \\\\* from test_table\",\"enable\"=\"true\")";
- CreateSqlBlockRuleStmt createSqlBlockRuleStmt = (CreateSqlBlockRuleStmt) UtFrameUtils.parseAndAnalyzeStmt(createSql, connectContext);
- }
-
- @Test
- public void testOnlyBlockQuery() throws DdlException, UserException {
- SqlBlockRuleMgr mgr = new SqlBlockRuleMgr();
- Analyzer analyzer = new Analyzer(Catalog.getCurrentCatalog(), connectContext);
-
- SqlBlockRule sqlRule = new SqlBlockRule("test_rule1", "test", null, 0L, 0L, 0L, true, true);
- mgr.replayCreate(sqlRule);
-
- Map<String, String> properties = new HashMap<>();
- properties.put(CreateSqlBlockRuleStmt.SQL_PROPERTY, "select \\* from test_table");
- AlterSqlBlockRuleStmt stmt = new AlterSqlBlockRuleStmt("test_rule1", properties);
-
- stmt.analyze(analyzer);
- mgr.alterSqlBlockRule(stmt);
-
- ShowSqlBlockRuleStmt showStmt = new ShowSqlBlockRuleStmt("test_rule1");
-
- Assert.assertEquals(1, mgr.getSqlBlockRule(showStmt).size());
- Assert.assertEquals("select \\* from test_table", mgr.getSqlBlockRule(showStmt).get(0).getSql());
- }
-
- @Test
- public void testLimitationsInvalid() throws Exception {
- SqlBlockRuleMgr mgr = new SqlBlockRuleMgr();
-
- // create sql_block_rule with partition_num = -1
- // DdlException: the value of partition_num can't be a negative
- String createSql = "CREATE SQL_BLOCK_RULE test_rule PROPERTIES(\"partition_num\"=\"-1\",\"enable\"=\"true\")";
- CreateSqlBlockRuleStmt stmt = (CreateSqlBlockRuleStmt) UtFrameUtils.parseAndAnalyzeStmt(createSql, connectContext);
- ExceptionChecker.expectThrowsWithMsg(DdlException.class, "the value of partition_num can't be a negative",
- () -> mgr.createSqlBlockRule(stmt));
-
- // create sql_block_rule with tablet_num = -1
- // DdlException: the value of tablet_num can't be a negative
- String createSql1 = "CREATE SQL_BLOCK_RULE test_rule PROPERTIES(\"tablet_num\"=\"-1\",\"enable\"=\"true\")";
- CreateSqlBlockRuleStmt stmt1 = (CreateSqlBlockRuleStmt) UtFrameUtils.parseAndAnalyzeStmt(createSql1, connectContext);
- ExceptionChecker.expectThrowsWithMsg(DdlException.class, "the value of tablet_num can't be a negative",
- () -> mgr.createSqlBlockRule(stmt1));
-
- // create sql_block_rule with cardinality = -1
- // DdlException: the value of cardinality can't be a negative
- String createSql2 = "CREATE SQL_BLOCK_RULE test_rule PROPERTIES(\"cardinality\"=\"-1\",\"enable\"=\"true\")";
- CreateSqlBlockRuleStmt stmt2 = (CreateSqlBlockRuleStmt) UtFrameUtils.parseAndAnalyzeStmt(createSql2, connectContext);
- ExceptionChecker.expectThrowsWithMsg(DdlException.class, "the value of cardinality can't be a negative",
- () -> mgr.createSqlBlockRule(stmt2));
- }
-
- @Test
- public void testUserPropertyInvalid() throws Exception {
- // sql block rules
- String ruleName = "test_rule_name";
- String setPropertyStr = String.format("set property for \"root\" \"sql_block_rules\" = \"%s\"", ruleName);
- SetUserPropertyStmt setUserPropertyStmt = (SetUserPropertyStmt) UtFrameUtils.parseAndAnalyzeStmt(setPropertyStr, connectContext);
-
- ExceptionChecker.expectThrowsWithMsg(DdlException.class, String.format("the sql block rule %s not exist", ruleName),
- () -> Catalog.getCurrentCatalog().getAuth().updateUserProperty(setUserPropertyStmt));
-
- }
-
- @Test
- public void testAlterSqlBlock() throws Exception{
- Analyzer analyzer = new Analyzer(Catalog.getCurrentCatalog(), connectContext);
- SqlBlockRuleMgr mgr = Catalog.getCurrentCatalog().getSqlBlockRuleMgr();
-
- // create : sql
- // alter : global
- SqlBlockRule sqlBlockRule = new SqlBlockRule("test_rule", "select \\* from test_table", "NULL", 0L, 0L, 0L, true, true);
- mgr.unprotectedAdd(sqlBlockRule);
- Assert.assertEquals(true, mgr.existRule("test_rule"));
-
- Map<String, String> properties = new HashMap<>();
- properties.put(CreateSqlBlockRuleStmt.GLOBAL_PROPERTY, "false");
- AlterSqlBlockRuleStmt stmt = new AlterSqlBlockRuleStmt("test_rule", properties);
- stmt.analyze(analyzer);
- mgr.alterSqlBlockRule(stmt);
-
- ShowSqlBlockRuleStmt showStmt = new ShowSqlBlockRuleStmt("test_rule");
- SqlBlockRule alteredSqlBlockRule = mgr.getSqlBlockRule(showStmt).get(0);
-
- Assert.assertEquals("select \\* from test_table", alteredSqlBlockRule.getSql());
- Assert.assertEquals("NULL", alteredSqlBlockRule.getSqlHash());
- Assert.assertEquals(0L, (long)alteredSqlBlockRule.getPartitionNum());
- Assert.assertEquals(0L, (long)alteredSqlBlockRule.getTabletNum());
- Assert.assertEquals(0L, (long)alteredSqlBlockRule.getCardinality());
- Assert.assertEquals(false, alteredSqlBlockRule.getGlobal());
- Assert.assertEquals(true, alteredSqlBlockRule.getEnable());
-
- // create : partitionNum
- // alter : tabletNum
- SqlBlockRule sqlBlockRule2 = new SqlBlockRule("test_rule2", "NULL", "NULL", 100L, 0L, 0L, true, true);
- mgr.unprotectedAdd(sqlBlockRule2);
- Assert.assertEquals(true, mgr.existRule("test_rule2"));
-
- Map<String, String> properties2 = new HashMap<>();
- properties2.put(CreateSqlBlockRuleStmt.SCANNED_TABLET_NUM, "500");
- AlterSqlBlockRuleStmt stmt2 = new AlterSqlBlockRuleStmt("test_rule2", properties2);
- stmt2.analyze(analyzer);
- mgr.alterSqlBlockRule(stmt2);
-
- ShowSqlBlockRuleStmt showStmt2 = new ShowSqlBlockRuleStmt("test_rule2");
- SqlBlockRule alteredSqlBlockRule2 = mgr.getSqlBlockRule(showStmt2).get(0);
-
- Assert.assertEquals("NULL", alteredSqlBlockRule2.getSql());
- Assert.assertEquals("NULL", alteredSqlBlockRule2.getSqlHash());
- Assert.assertEquals(100L, (long)alteredSqlBlockRule2.getPartitionNum());
- Assert.assertEquals(500L, (long)alteredSqlBlockRule2.getTabletNum());
- Assert.assertEquals(0L, (long)alteredSqlBlockRule2.getCardinality());
- Assert.assertEquals(true, alteredSqlBlockRule2.getGlobal());
- Assert.assertEquals(true, alteredSqlBlockRule2.getEnable());
-
-
- }
-}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org