This is an automated email from the ASF dual-hosted git repository.
jianglongtao 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 d226b54a219 Add support for specifying table name in show shadow table
rule for shadow table feature (#29269)
d226b54a219 is described below
commit d226b54a2199c7399ba7b505b00b12592f75faa1
Author: Harsh Vinod Sawarkar <[email protected]>
AuthorDate: Fri Dec 8 13:01:30 2023 +0530
Add support for specifying table name in show shadow table rule for shadow
table feature (#29269)
* Added the support for specifying table name in Show shadow table rule
feature
* Added the support for specifying table name in Show shadow table rule
feature
* Added the support for specifying table name in Show shadow table rule
feature
* Added the support for specifying table name in Show shadow table rule
feature
* Added the support for specifying table name in Show shadow table rule
feature
---
.../query/ShowShadowTableRulesExecutor.java | 31 ++++++++++++++++------
.../src/main/antlr4/imports/shadow/BaseRule.g4 | 4 +++
.../src/main/antlr4/imports/shadow/RQLStatement.g4 | 6 ++++-
.../parser/core/ShadowDistSQLStatementVisitor.java | 2 +-
.../statement/ShowShadowTableRulesStatement.java | 5 +++-
.../it/parser/src/main/resources/case/rql/show.xml | 9 +++++++
6 files changed, 46 insertions(+), 11 deletions(-)
diff --git
a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/query/ShowShadowTableRulesExecutor.java
b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/query/ShowShadowTableRulesExecutor.java
index ef7268d8a14..b431be7cce4 100644
---
a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/query/ShowShadowTableRulesExecutor.java
+++
b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/query/ShowShadowTableRulesExecutor.java
@@ -49,7 +49,7 @@ public final class ShowShadowTableRulesExecutor implements
RQLExecutor<ShowShado
Optional<ShadowRule> rule =
database.getRuleMetaData().findSingleRule(ShadowRule.class);
Iterator<Map<String, String>> data = Collections.emptyIterator();
if (rule.isPresent()) {
- data = buildData((ShadowRuleConfiguration)
rule.get().getConfiguration()).iterator();
+ data = buildData((ShadowRuleConfiguration)
rule.get().getConfiguration(), sqlStatement).iterator();
}
Collection<LocalDataQueryResultRow> result = new LinkedList<>();
while (data.hasNext()) {
@@ -59,14 +59,25 @@ public final class ShowShadowTableRulesExecutor implements
RQLExecutor<ShowShado
return result;
}
- private List<Map<String, String>> buildData(final ShadowRuleConfiguration
shadowRuleConfig) {
+ private List<Map<String, String>> buildData(final ShadowRuleConfiguration
shadowRuleConfig, final ShowShadowTableRulesStatement sqlStatement) {
List<Map<String, String>> result = new ArrayList<>();
- shadowRuleConfig.getTables().forEach((key, value) -> {
- Map<String, String> map = new HashMap<>();
- map.put(SHADOW_TABLE, key);
- map.put(SHADOW_ALGORITHM_NAME,
convertToString(value.getShadowAlgorithmNames()));
- result.add(map);
- });
+ if (isSpecified(sqlStatement)) {
+ shadowRuleConfig.getTables().forEach((key, value) -> {
+ Map<String, String> map = new HashMap<>();
+ if (key.equalsIgnoreCase(sqlStatement.getTableName())) {
+ map.put(SHADOW_TABLE, key);
+ map.put(SHADOW_ALGORITHM_NAME,
convertToString(value.getShadowAlgorithmNames()));
+ }
+ result.add(map);
+ });
+ } else {
+ shadowRuleConfig.getTables().forEach((key, value) -> {
+ Map<String, String> map = new HashMap<>();
+ map.put(SHADOW_TABLE, key);
+ map.put(SHADOW_ALGORITHM_NAME,
convertToString(value.getShadowAlgorithmNames()));
+ result.add(map);
+ });
+ }
return result;
}
@@ -75,6 +86,10 @@ public final class ShowShadowTableRulesExecutor implements
RQLExecutor<ShowShado
return Arrays.asList(SHADOW_TABLE, SHADOW_ALGORITHM_NAME);
}
+ private boolean isSpecified(final ShowShadowTableRulesStatement
sqlStatement) {
+ return null != sqlStatement.getTableName() &&
!sqlStatement.getTableName().isEmpty();
+ }
+
private String convertToString(final Collection<String> shadowTables) {
return null == shadowTables ? "" : String.join(",", shadowTables);
}
diff --git
a/features/shadow/distsql/parser/src/main/antlr4/imports/shadow/BaseRule.g4
b/features/shadow/distsql/parser/src/main/antlr4/imports/shadow/BaseRule.g4
index 1c310335efe..2369297a291 100644
--- a/features/shadow/distsql/parser/src/main/antlr4/imports/shadow/BaseRule.g4
+++ b/features/shadow/distsql/parser/src/main/antlr4/imports/shadow/BaseRule.g4
@@ -49,6 +49,10 @@ property
: key=STRING_ EQ_ value=literal
;
+tableName
+ : IDENTIFIER_
+ ;
+
ruleName
: IDENTIFIER_
;
diff --git
a/features/shadow/distsql/parser/src/main/antlr4/imports/shadow/RQLStatement.g4
b/features/shadow/distsql/parser/src/main/antlr4/imports/shadow/RQLStatement.g4
index 0c20410e19a..cc64da260a6 100644
---
a/features/shadow/distsql/parser/src/main/antlr4/imports/shadow/RQLStatement.g4
+++
b/features/shadow/distsql/parser/src/main/antlr4/imports/shadow/RQLStatement.g4
@@ -24,7 +24,7 @@ showShadowRules
;
showShadowTableRules
- : SHOW SHADOW TABLE RULES (FROM databaseName)?
+ : SHOW SHADOW TABLE (tableRule | RULES) (FROM databaseName)?
;
showShadowAlgorithms
@@ -43,6 +43,10 @@ countShadowRule
: COUNT SHADOW RULE (FROM databaseName)?
;
+tableRule
+ : RULE tableName
+ ;
+
databaseName
: IDENTIFIER_
;
diff --git
a/features/shadow/distsql/parser/src/main/java/org/apache/shardingsphere/shadow/distsql/parser/core/ShadowDistSQLStatementVisitor.java
b/features/shadow/distsql/parser/src/main/java/org/apache/shardingsphere/shadow/distsql/parser/core/ShadowDistSQLStatementVisitor.java
index 6d8b9fb3d23..bceb7779e73 100644
---
a/features/shadow/distsql/parser/src/main/java/org/apache/shardingsphere/shadow/distsql/parser/core/ShadowDistSQLStatementVisitor.java
+++
b/features/shadow/distsql/parser/src/main/java/org/apache/shardingsphere/shadow/distsql/parser/core/ShadowDistSQLStatementVisitor.java
@@ -152,7 +152,7 @@ public final class ShadowDistSQLStatementVisitor extends
ShadowDistSQLStatementB
@Override
public ASTNode visitShowShadowTableRules(final ShowShadowTableRulesContext
ctx) {
- return new ShowShadowTableRulesStatement(null == ctx.databaseName() ?
null : (DatabaseSegment) visit(ctx.databaseName()));
+ return new ShowShadowTableRulesStatement(null == ctx.databaseName() ?
null : (DatabaseSegment) visit(ctx.databaseName()), ctx.TABLE().getText());
}
private String getIdentifierValue(final ParserRuleContext ctx) {
diff --git
a/features/shadow/distsql/statement/src/main/java/org/apache/shardingsphere/shadow/distsql/statement/ShowShadowTableRulesStatement.java
b/features/shadow/distsql/statement/src/main/java/org/apache/shardingsphere/shadow/distsql/statement/ShowShadowTableRulesStatement.java
index 0077a1d3fc6..814f968e47a 100644
---
a/features/shadow/distsql/statement/src/main/java/org/apache/shardingsphere/shadow/distsql/statement/ShowShadowTableRulesStatement.java
+++
b/features/shadow/distsql/statement/src/main/java/org/apache/shardingsphere/shadow/distsql/statement/ShowShadowTableRulesStatement.java
@@ -27,7 +27,10 @@ import
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.DatabaseS
@Getter
public final class ShowShadowTableRulesStatement extends ShowRulesStatement {
- public ShowShadowTableRulesStatement(final DatabaseSegment database) {
+ private final String tableName;
+
+ public ShowShadowTableRulesStatement(final DatabaseSegment database, final
String tableName) {
super(database);
+ this.tableName = tableName;
}
}
diff --git a/test/it/parser/src/main/resources/case/rql/show.xml
b/test/it/parser/src/main/resources/case/rql/show.xml
index 983f47e3f4c..54c4ee5270b 100644
--- a/test/it/parser/src/main/resources/case/rql/show.xml
+++ b/test/it/parser/src/main/resources/case/rql/show.xml
@@ -66,6 +66,15 @@
<database name="shadow_db" start-index="29" stop-index="37" />
</show-shadow-table-rules>
+ <show-shadow-table-rules sql-case-id="show-shadow-table-rule">
+ <table name="t_order" />
+ </show-shadow-table-rules>
+
+ <show-shadow-table-rules sql-case-id="show-shadow-table-rule-from">
+ <table name="t_order" />
+ <database name="shadow_db" start-index="29" stop-index="37" />
+ </show-shadow-table-rules>
+
<show-shadow-algorithms sql-case-id="show-shadow-algorithms">
<database name="shadow_db" start-index="28" stop-index="36" />
</show-shadow-algorithms>