You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by su...@apache.org on 2022/10/15 16:30:10 UTC
[shardingsphere] branch master updated: Fix NPE when call findTableRule with subquery table (#21590)
This is an automated email from the ASF dual-hosted git repository.
sunnianjun pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 90191fb0528 Fix NPE when call findTableRule with subquery table (#21590)
90191fb0528 is described below
commit 90191fb0528832458860c9e2411229c64f8f0baa
Author: Zhengqiang Duan <du...@apache.org>
AuthorDate: Sun Oct 16 00:30:01 2022 +0800
Fix NPE when call findTableRule with subquery table (#21590)
---
.../org/apache/shardingsphere/sharding/rule/ShardingRule.java | 8 ++++++--
.../org/apache/shardingsphere/sharding/rule/ShardingRuleTest.java | 7 ++++++-
2 files changed, 12 insertions(+), 3 deletions(-)
diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java
index 55b1b99594f..542baeebf8f 100644
--- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java
+++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java
@@ -19,14 +19,15 @@ package org.apache.shardingsphere.sharding.rule;
import com.google.common.base.Preconditions;
import com.google.common.base.Splitter;
+import com.google.common.base.Strings;
import lombok.Getter;
import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.binder.statement.dml.SelectStatementContext;
-import org.apache.shardingsphere.infra.instance.InstanceContextAware;
import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
import org.apache.shardingsphere.infra.database.type.DatabaseTypeEngine;
import org.apache.shardingsphere.infra.datanode.DataNode;
import org.apache.shardingsphere.infra.instance.InstanceContext;
+import org.apache.shardingsphere.infra.instance.InstanceContextAware;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.schema.decorator.model.ShardingSphereSchema;
import org.apache.shardingsphere.infra.rule.identifier.scope.DatabaseRule;
@@ -357,7 +358,10 @@ public final class ShardingRule implements DatabaseRule, DataNodeContainedRule,
* @return table rule
*/
public Optional<TableRule> findTableRule(final String logicTableName) {
- return Optional.ofNullable(tableRules.get(logicTableName.toLowerCase()));
+ if (Strings.isNullOrEmpty(logicTableName) || !tableRules.containsKey(logicTableName.toLowerCase())) {
+ return Optional.empty();
+ }
+ return Optional.of(tableRules.get(logicTableName.toLowerCase()));
}
/**
diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rule/ShardingRuleTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rule/ShardingRuleTest.java
index 2032e5cd6d1..cce940cc6d9 100644
--- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rule/ShardingRuleTest.java
+++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rule/ShardingRuleTest.java
@@ -67,9 +67,9 @@ import java.util.TreeSet;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
-import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
@@ -115,6 +115,11 @@ public final class ShardingRuleTest {
assertFalse(createMaximumShardingRule().findTableRule("other_Table").isPresent());
}
+ @Test
+ public void assertNotFindTableRuleWhenTableNameIsNull() {
+ assertFalse(createMaximumShardingRule().findTableRule(null).isPresent());
+ }
+
@Test
public void assertFindTableRuleByActualTable() {
assertTrue(createMaximumShardingRule().findTableRuleByActualTable("table_0").isPresent());