Skip to content

Commit

Permalink
Add more test cases on ShardingSQLFederationDecider (#33712)
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu authored Nov 18, 2024
1 parent 684605b commit 940540a
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,26 @@ public boolean decide(final SelectStatementContext selectStatementContext, final
if (tableNames.isEmpty()) {
return false;
}
includedDataNodes.addAll(getTableDataNodes(rule, tableNames, database));
includedDataNodes.addAll(getTableDataNodes(rule, database, tableNames));
if (selectStatementContext.isContainsSubquery() || selectStatementContext.isContainsHaving()
|| selectStatementContext.isContainsCombine() || selectStatementContext.isContainsPartialDistinctAggregation()) {
return true;
}
if (!selectStatementContext.isContainsJoinQuery() || rule.isAllTablesInSameDataSource(tableNames)) {
return false;
}
if (1 == tableNames.size() && selectStatementContext.isContainsJoinQuery() && !rule.isAllBindingTables(database, selectStatementContext, tableNames)) {
if (isSelfJoinWithoutShardingColumn(selectStatementContext, rule, database, tableNames)) {
return true;
}
return tableNames.size() > 1 && !rule.isAllBindingTables(database, selectStatementContext, tableNames);
}

private Collection<DataNode> getTableDataNodes(final ShardingRule rule, final Collection<String> tableNames, final ShardingSphereDatabase database) {
private boolean isSelfJoinWithoutShardingColumn(final SelectStatementContext selectStatementContext,
final ShardingRule rule, final ShardingSphereDatabase database, final Collection<String> tableNames) {
return 1 == tableNames.size() && selectStatementContext.isContainsJoinQuery() && !rule.isAllBindingTables(database, selectStatementContext, tableNames);
}

private Collection<DataNode> getTableDataNodes(final ShardingRule rule, final ShardingSphereDatabase database, final Collection<String> tableNames) {
Collection<DataNode> result = new HashSet<>();
for (String each : tableNames) {
rule.findShardingTable(each).ifPresent(optional -> result.addAll(new DataNodes(database.getRuleMetaData().getRules()).getDataNodes(each)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ public boolean isAllBindingTables(final Collection<String> logicTableNames) {
* @param logicTableNames logic table names
* @return whether logic table is all binding tables
*/
// TODO rename the method name, add sharding condition judgement in method name @duanzhengqiang
public boolean isAllBindingTables(final ShardingSphereDatabase database, final SQLStatementContext sqlStatementContext, final Collection<String> logicTableNames) {
if (!(sqlStatementContext instanceof SelectStatementContext && ((SelectStatementContext) sqlStatementContext).isContainsJoinQuery())) {
return isAllBindingTables(logicTableNames);
Expand Down
Loading

0 comments on commit 940540a

Please sign in to comment.